Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support attachment upload with CLI #918

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading