Skip to content

Commit

Permalink
Merge pull request #918 from intuitem/cli_support_attachment
Browse files Browse the repository at this point in the history
Support attachment upload with CLI
  • Loading branch information
ab-smith authored Oct 10, 2024
2 parents 840ac2c + fd5bf02 commit feaa13f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_403_FORBIDDEN
from rest_framework.utils.serializer_helpers import ReturnDict
from rest_framework.views import APIView

from weasyprint import HTML

from core.helpers import *
Expand Down Expand Up @@ -1385,7 +1386,7 @@ class EvidenceViewSet(BaseModelViewSet):
"""

model = Evidence
filterset_fields = ["folder", "applied_controls", "requirement_assessments"]
filterset_fields = ["folder", "applied_controls", "requirement_assessments", "name"]
search_fields = ["name"]
ordering_fields = ["name", "description"]

Expand Down
39 changes: 37 additions & 2 deletions cli/clica.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,48 @@ def evidences_templates(file):
print(f"✅ {row['name']} created")


# Add commands to the CLI group
@click.command()
@click.option("--file", required=True, help="Path to the attachment to upload")
@click.option("--name", required=True, help="Name of the evidence")
def upload_evidence(file, name):
"""Upload attachment as evidence"""

headers = {
"Authorization": f"Token {TOKEN}",
}
# Get evidence ID by name
url = f"{API_URL}/evidences/"
res = requests.get(url, headers=headers, params={"name": name})
data = res.json()
if res.status_code != 200:
print(data)
print(f"Error: check credentials or filename.")
return
if not data["results"]:
print(f"Error: No evidence found with name '{name}'")
return

evidence_id = data["results"][0]["id"]

# Upload file
url = f"{API_URL}/evidences/{evidence_id}/upload/"
filename = Path(file).name
headers = {
"Authorization": f"Token {TOKEN}",
"Content-Disposition": f'attachment;filename="{filename}"',
}
with open(file, "rb") as f:
res = requests.post(url, headers=headers, data=f)
print(res)
print(res.text)


cli.add_command(get_folders)
cli.add_command(auth)
cli.add_command(import_assets)
cli.add_command(import_controls)
cli.add_command(evidences_templates)
cli.add_command(init_config)

cli.add_command(upload_evidence)
if __name__ == "__main__":
cli()
Binary file added cli/smiling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit feaa13f

Please sign in to comment.