Skip to content

Commit

Permalink
Issue Apis (#999)
Browse files Browse the repository at this point in the history
* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Create README.md

* Update README.md

* Codebase restructured | removed unused files

* Added upvote,flag count to issue api, list and retrive api updated
  • Loading branch information
AtmegaBuzz authored Dec 13, 2022
1 parent 4385c9f commit c6b0a03
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
57 changes: 56 additions & 1 deletion website/api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.core.mail import send_mail
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.response import Response
Expand All @@ -21,7 +22,8 @@
from website.models import (
UserProfile,
User,
Points
Points,
IssueScreenshot
)


Expand Down Expand Up @@ -74,6 +76,59 @@ class IssueViewSet(viewsets.ModelViewSet):
search_fields = ("url", "description", "user__id")
http_method_names = ["get", "post", "head"]

def list(self, request, *args, **kwargs):

issues = []

for issue in self.queryset:

screenshot = None
if issue.screenshot:
screenshot = issue.screenshot.url
elif issue.screenshots.first():
screenshot = issue.screenshots.first().image.url

issues.append({
**Issue.objects.values().filter(id=issue.id).first(),
"screenshot":screenshot
})

return Response(issues)


def retrieve(self, request, pk,*args, **kwargs):
issue = self.queryset.filter(id=pk).values().first()
issue_obj = Issue.objects.filter(id=pk).first()

screenshots = [
# replacing space with url space notation
screenshot["image"].replace(" ","%20")
for screenshot in
issue_obj.screenshots.values("image").all()
] + ( [issue_obj.screenshot.url] if issue_obj.screenshot else [] )

if issue == None:
return Response({})

upvotes = issue_obj.upvoted.all().__len__()
flags = issue_obj.flaged.all().__len__()
upvotted = False
flagged = False

if type(request.user) != AnonymousUser:

upvotted = bool(request.user.userprofile.issue_upvoted.filter(id=pk).first())
flagged = bool(request.user.userprofile.issue_flaged.filter(id=pk).first())


return Response({
**issue,
"upvotes": upvotes,
"flags": flags,
"upvotted": upvotted,
"flagged": flagged,
"screenshots": screenshots
})


class LikeIssueApiView(APIView):
Expand Down
23 changes: 23 additions & 0 deletions website/migrations/0071_alter_issuescreenshot_issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.1 on 2022-12-13 14:24

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("website", "0070_alter_issue_label_issuescreenshot"),
]

operations = [
migrations.AlterField(
model_name="issuescreenshot",
name="issue",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="screenshots",
to="website.issue",
),
),
]
2 changes: 1 addition & 1 deletion website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class Meta:

class IssueScreenshot(models.Model):
image = models.ImageField(upload_to="screenshots", validators=[validate_image])
issue = models.ForeignKey(Issue,on_delete=models.CASCADE)
issue = models.ForeignKey(Issue,on_delete=models.CASCADE,related_name="screenshots")

TWITTER_MAXLENGTH = getattr(settings, "TWITTER_MAXLENGTH", 140)

Expand Down

0 comments on commit c6b0a03

Please sign in to comment.