Skip to content

Commit

Permalink
Allow /api/v1/issues to have multiple screenshots (#1208)
Browse files Browse the repository at this point in the history
* Allow /api/v1/issues to have multiple screenshots

* Fix raising error
  • Loading branch information
letsintegreat authored Apr 7, 2023
1 parent a34f09f commit eb0403a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion website/api/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,6 +19,7 @@
from website.models import (
Issue,
Domain,
IssueScreenshot,
)
from website.serializers import (
IssueSerializer,
Expand Down Expand Up @@ -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))


Expand Down
2 changes: 1 addition & 1 deletion website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit eb0403a

Please sign in to comment.