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
25 changes: 19 additions & 6 deletions website/api/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import requests.exceptions


from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.db.models import Sum

from rest_framework import viewsets, filters
from rest_framework.response import Response
Expand All @@ -20,7 +18,11 @@
DomainSerializer,
)

from website.models import UserProfile
from website.models import (
UserProfile,
User,
Points
)


# API's
Expand Down Expand Up @@ -49,7 +51,6 @@ class UserProfileViewSet(viewsets.ModelViewSet):
search_fields = ("id", "user__id", "user__username")
http_method_names = ["get", "post", "head"]


class DomainViewSet(viewsets.ModelViewSet):
"""
Domain View Set
Expand Down Expand Up @@ -152,4 +153,16 @@ def post(self,request,id,format=None,*args,**kwargs):
else:
userprof.issue_flaged.add(issue)
userprof.save()
return Response({"issue":"flagged"})
return Response({"issue":"flagged"})



class UserScoreApiView(APIView):

authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get(self,request,id,format=None,*args, **kwargs):
total_score = Points.objects.filter(user__id=id).annotate(total_score=Sum('score'))

return Response({"total_score":total_score})
33 changes: 30 additions & 3 deletions website/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from website.models import Issue, User, UserProfile, Domain
from website.models import Issue, Points, User, UserProfile, Domain
from rest_framework import serializers

from django.db.models import Sum

class UserSerializer(serializers.ModelSerializer):
"""
Expand All @@ -16,11 +16,38 @@ class UserProfileSerializer(serializers.ModelSerializer):
"""
Serializer for user model
"""
def get_total_score(self,instance):
score = Points.objects.filter(user=instance.user).aggregate(total_score=Sum('score')).get("total_score")
if score==None: return 0
return score

def get_activities(self,instance):

issues = Points.objects.filter(user=instance.user,score__gt=0).values("issue__id")
return [ issue["issue__id"] for issue in issues ]


user = UserSerializer(read_only=True)
total_score = serializers.SerializerMethodField(method_name="get_total_score")
activities = serializers.SerializerMethodField(method_name="get_activities")

class Meta:
model = UserProfile
fields = '__all__'
fields = (
"id",
"title",
"follows",
"user",
"user_avatar",
"description",
"winnings",
"follows",
"issue_upvoted",
"issue_saved",
"issue_flaged",
"total_score",
"activities"
)


class IssueSerializer(serializers.ModelSerializer):
Expand Down
230 changes: 117 additions & 113 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1946,119 +1946,6 @@ def get_scoreboard(request):
)


# class CreateIssue(CronJobBase):
# RUN_EVERY_MINS = 1

# schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
# code = "bugheist.create_issue" # a unique code

# def do(self):
# from django.conf import settings
# import imaplib

# mail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
# error = False
# mail.login(settings.REPORT_EMAIL, settings.REPORT_EMAIL_PASSWORD)
# mail.list()
# # Out: list of "folders" aka labels in gmail.
# mail.select("inbox") # connect to inbox.
# typ, data = mail.search(None, "ALL", "UNSEEN")
# import email

# for num in data[0].split():
# image = False
# screenshot_base64 = ""
# url = ""
# label = ""
# token = "None"
# typ, data = mail.fetch(num, "(RFC822)")
# raw_email = (data[0][1]).decode("utf-8")
# email_message = email.message_from_string(raw_email)
# maintype = email_message.get_content_maintype()
# error = False
# for part in email_message.walk():
# if part.get_content_type() == "text/plain": # ignore attachments/html
# body = part.get_payload(decode=True)
# body_text = body.decode("utf-8")
# words = body_text.split()
# flag_word = False
# for word in words:
# if word.lower() == ":":
# continue
# if word.lower() == "url":
# continue
# if word.lower() == "type":
# flag_word = True
# continue
# if flag_word == False:
# url = word
# continue
# if flag_word == True:
# label = word
# if part.get_content_maintype() == "multipart":
# continue
# if part.get("Content-Disposition") is None:
# continue
# image = True
# screenshot_base64 = part
# sender = email_message["From"].split()[-1]
# address = re.sub(r"[<>]", "", sender)
# for user in User.objects.all():
# if user.email == address:
# token = Token.objects.get(user_id=user.id).key
# break
# if label.lower() == "general":
# label = 0
# elif label.lower() == "number error":
# label = 1
# elif label.lower() == "functional":
# label = 2
# elif label.lower() == "performance":
# label = 3
# elif label.lower() == "security":
# label = 4
# elif label.lower() == "typo":
# label = 5
# elif label.lower() == "design":
# label = 6
# else:
# error = True
# if token == "None":
# error = "TokenTrue"
# if image == False:
# error = True
# if error == True:
# send_mail(
# "Error In Your Report",
# "There was something wrong with the mail you sent regarding the issue to be created. Please check the content and try again later !",
# "Bugheist <[email protected]>",
# [address],
# fail_silently=False,
# )
# elif error == "TokenTrue":
# send_mail(
# "You are not a user of Bugheist",
# "You are not a Registered user at Bugheist .Please first Signup at Bugheist and Try Again Later ! .",
# "Bugheist <[email protected]>",
# [address],
# fail_silently=False,
# )
# else:
# data = {
# "url": url,
# "description": email_message["Subject"],
# "file": str(screenshot_base64.get_payload(decode=False)),
# "token": token,
# "label": label,
# "type": "jpg",
# }
# headers = {"Content-Type": "application/x-www-form-urlencoded"}
# requests.post(
# "https://www.bugheist.com/api/v1/createissues/",
# data=json.dumps(data),
# headers=headers,
# )
# mail.logout()


def throw_error(request):
Expand Down Expand Up @@ -3053,3 +2940,120 @@ def handler404(request, exception):

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



# class CreateIssue(CronJobBase):
# RUN_EVERY_MINS = 1

# schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
# code = "bugheist.create_issue" # a unique code

# def do(self):
# from django.conf import settings
# import imaplib

# mail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
# error = False
# mail.login(settings.REPORT_EMAIL, settings.REPORT_EMAIL_PASSWORD)
# mail.list()
# # Out: list of "folders" aka labels in gmail.
# mail.select("inbox") # connect to inbox.
# typ, data = mail.search(None, "ALL", "UNSEEN")
# import email

# for num in data[0].split():
# image = False
# screenshot_base64 = ""
# url = ""
# label = ""
# token = "None"
# typ, data = mail.fetch(num, "(RFC822)")
# raw_email = (data[0][1]).decode("utf-8")
# email_message = email.message_from_string(raw_email)
# maintype = email_message.get_content_maintype()
# error = False
# for part in email_message.walk():
# if part.get_content_type() == "text/plain": # ignore attachments/html
# body = part.get_payload(decode=True)
# body_text = body.decode("utf-8")
# words = body_text.split()
# flag_word = False
# for word in words:
# if word.lower() == ":":
# continue
# if word.lower() == "url":
# continue
# if word.lower() == "type":
# flag_word = True
# continue
# if flag_word == False:
# url = word
# continue
# if flag_word == True:
# label = word
# if part.get_content_maintype() == "multipart":
# continue
# if part.get("Content-Disposition") is None:
# continue
# image = True
# screenshot_base64 = part
# sender = email_message["From"].split()[-1]
# address = re.sub(r"[<>]", "", sender)
# for user in User.objects.all():
# if user.email == address:
# token = Token.objects.get(user_id=user.id).key
# break
# if label.lower() == "general":
# label = 0
# elif label.lower() == "number error":
# label = 1
# elif label.lower() == "functional":
# label = 2
# elif label.lower() == "performance":
# label = 3
# elif label.lower() == "security":
# label = 4
# elif label.lower() == "typo":
# label = 5
# elif label.lower() == "design":
# label = 6
# else:
# error = True
# if token == "None":
# error = "TokenTrue"
# if image == False:
# error = True
# if error == True:
# send_mail(
# "Error In Your Report",
# "There was something wrong with the mail you sent regarding the issue to be created. Please check the content and try again later !",
# "Bugheist <[email protected]>",
# [address],
# fail_silently=False,
# )
# elif error == "TokenTrue":
# send_mail(
# "You are not a user of Bugheist",
# "You are not a Registered user at Bugheist .Please first Signup at Bugheist and Try Again Later ! .",
# "Bugheist <[email protected]>",
# [address],
# fail_silently=False,
# )
# else:
# data = {
# "url": url,
# "description": email_message["Subject"],
# "file": str(screenshot_base64.get_payload(decode=False)),
# "token": token,
# "label": label,
# "type": "jpg",
# }
# headers = {"Content-Type": "application/x-www-form-urlencoded"}
# requests.post(
# "https://www.bugheist.com/api/v1/createissues/",
# data=json.dumps(data),
# headers=headers,
# )
# mail.logout()