From eb0403ac267f7afe6f5aa8cd69bf9a21c2d9602a Mon Sep 17 00:00:00 2001 From: Harshit Seksaria <37345795+letsintegreat@users.noreply.github.com> Date: Fri, 7 Apr 2023 10:46:58 +0530 Subject: [PATCH] Allow /api/v1/issues to have multiple screenshots (#1208) * Allow /api/v1/issues to have multiple screenshots * Fix raising error --- website/api/views.py | 21 ++++++++++++++++++++- website/models.py | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/website/api/views.py b/website/api/views.py index f1f5907e5..8c3e503dd 100644 --- a/website/api/views.py +++ b/website/api/views.py @@ -1,11 +1,14 @@ from datetime import datetime +import uuid from django.core.mail import send_mail +from django.core.files.storage import default_storage +from django.forms import ValidationError from django.template.loader import render_to_string from django.db.models import Sum from django.contrib.auth.models import AnonymousUser -from rest_framework import viewsets, filters +from rest_framework import viewsets, filters, status from rest_framework.response import Response from rest_framework.views import APIView from rest_framework.authentication import TokenAuthentication @@ -16,6 +19,7 @@ from website.models import ( Issue, Domain, + IssueScreenshot, ) from website.serializers import ( IssueSerializer, @@ -174,9 +178,24 @@ def retrieve(self, request, pk,*args, **kwargs): return Response(self.get_issue_info(request,issue)) def create(self, request, *args, **kwargs): + if len(self.request.FILES.getlist("screenshots")) > 5: + return Response({"error": "Max limit of 5 images!"}, status=status.HTTP_400_BAD_REQUEST) + + if len(self.request.FILES.getlist("screenshots")) == 0: + return Response({"error": "Upload atleast one image!"}, status=status.HTTP_400_BAD_REQUEST) + response = super().create(request, *args, **kwargs) data = response.data issue = Issue.objects.filter(id=data["id"]).first() + + + for screenshot in self.request.FILES.getlist("screenshots"): + filename = screenshot.name + extension = filename.split(".")[-1] + screenshot.name = filename[:99] + str(uuid.uuid4()) + "." + extension + default_storage.save(f"screenshots/{screenshot.name}",screenshot) + IssueScreenshot.objects.create(image=f"screenshots/{screenshot.name}",issue=issue) + return Response(self.get_issue_info(request,issue)) diff --git a/website/models.py b/website/models.py index d1e13ead9..cc4da8641 100644 --- a/website/models.py +++ b/website/models.py @@ -211,7 +211,7 @@ class Issue(models.Model): status = models.CharField(max_length=10, default="open", null=True, blank=True) user_agent = models.CharField(max_length=255, default="", null=True, blank=True) ocr = models.TextField(default="", null=True, blank=True) - screenshot = models.ImageField(upload_to="screenshots", validators=[validate_image]) + screenshot = models.ImageField(upload_to="screenshots", null=True, blank=True, validators=[validate_image]) closed_by = models.ForeignKey( User, null=True, blank=True, related_name="closed_by", on_delete=models.CASCADE )