Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
5 changes: 5 additions & 0 deletions bugheist/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from posixpath import basename
from django.conf import settings
from django.urls import path
from django.conf.urls import include
Expand All @@ -21,6 +22,8 @@
InboundParseWebhookView,
LeaderboardView,
LeaderboardApiViewSet,
LikeIssueApiView,
FlagIssueApiView,
MonthlyLeaderboardView,
IssueView,
AllIssuesView,
Expand Down Expand Up @@ -277,6 +280,8 @@
re_path(r"^leaderboard/$", LeaderboardView.as_view(), name="leaderboard"),
re_path(r"^leaderboard/monthly/$", MonthlyLeaderboardView.as_view(), name="leaderboard_monthly"),
re_path(r"^leaderboard/api/$", LeaderboardApiViewSet.as_view(), name="leaderboard_api"),
re_path(r"^api/v1/issue/like/(?P<id>\w+)/$", LikeIssueApiView.as_view(), name="like_issue"),
re_path(r"^api/v1/issue/flag/(?P<id>\w+)/$", FlagIssueApiView.as_view(), name="flag_issue"),
re_path(r"^scoreboard/$", ScoreboardView.as_view(), name="scoreboard"),
re_path(r"^issue/$", IssueCreate.as_view(), name="issue"),
re_path(
Expand Down
84 changes: 83 additions & 1 deletion website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import action

from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from dj_rest_auth.registration.views import SocialLoginView

from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
Expand Down Expand Up @@ -3070,3 +3073,82 @@ def handler404(request, exception):

def handler500(request, exception=None):
return render(request, "500.html", {}, status=500)

class LikeIssueApiView(APIView):

authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get(self,request,id,format=None,*args, **kwargs):
return Response({
"likes":UserProfile.objects.filter(issue_upvoted__id=id).count(),
})

def post(self,request,id,format=None,*args,**kwargs):

issue = Issue.objects.get(id=id)
userprof = UserProfile.objects.get(user=request.user)
if userprof in UserProfile.objects.filter(issue_upvoted=issue):
userprof.issue_upvoted.remove(issue)
userprof.save()
return Response({"issue":"unliked"})
else:
userprof.issue_upvoted.add(issue)
userprof.save()

liked_user = issue.user
liker_user = request.user
issue_pk = issue.pk
msg_plain = render_to_string(
"email/issue_liked.txt",
{
"liker_user": liker_user.username,
"liked_user": liked_user.username,
"issue_pk": issue_pk,
},
)
msg_html = render_to_string(
"email/issue_liked.txt",
{
"liker_user": liker_user.username,
"liked_user": liked_user.username,
"issue_pk": issue_pk,
},
)

send_mail(
"Your issue got an upvote!!",
msg_plain,
"Bugheist <[email protected]>",
[liked_user.email],
html_message=msg_html,
)

return Response({"issue":"liked"})

class FlagIssueApiView(APIView):
'''
api for Issue like,flag and bookmark
'''

authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get(self,request,id,format=None,*args, **kwargs):
return Response({
"flags":UserProfile.objects.filter(issue_flaged__id=id).count(),
})

def post(self,request,id,format=None,*args,**kwargs):


issue = Issue.objects.get(id=id)
userprof = UserProfile.objects.get(user=request.user)
if userprof in UserProfile.objects.filter(issue_flaged=issue):
userprof.issue_flaged.remove(issue)
userprof.save()
return Response({"issue":"unflagged"})
else:
userprof.issue_flaged.add(issue)
userprof.save()
return Response({"issue":"flagged"})