Skip to content

Commit

Permalink
Support attachment upload with CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-smith committed Oct 10, 2024
1 parent 92e57a8 commit e67fdab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
23 changes: 23 additions & 0 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,29 @@ def delete_attachment(self, request, pk):
response = Response(status=status.HTTP_200_OK)
return response

@action(detail=False, methods=["get"])
def get_by_name(self, request):
name = request.query_params.get("name")
if not name:
return Response(
{"error": "Name parameter is required"},
status=status.HTTP_400_BAD_REQUEST,
)

viewable_objects_ids, *_ = RoleAssignment.get_accessible_object_ids(
Folder.get_root_folder(), request.user, Evidence
)
evidence = Evidence.objects.filter(
id__in=viewable_objects_ids, name=name
).first()

if evidence:
return Response({"id": str(evidence.id)})
else:
return Response(
{"error": "Evidence not found"}, status=status.HTTP_404_NOT_FOUND
)


class UploadAttachmentView(APIView):
parser_classes = (FileUploadParser,)
Expand Down
35 changes: 33 additions & 2 deletions cli/clica.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import requests
import yaml
from rich import print
import mimetypes

cli_cfg = dict()
auth_data = dict()
Expand Down Expand Up @@ -230,13 +231,43 @@ 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/get_by_name/"
res = requests.get(url, headers=headers, params={"name": name})
data = res.json()

if "id" not in data:
print(f"Error: {data.get('error', 'Unable to find evidence')}")
return

id = data["id"]
url = f"{API_URL}/evidences/{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 e67fdab

Please sign in to comment.