Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of E-Mail Notification Triggers #689

Closed
wants to merge 10 commits into from
Closed
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
34 changes: 34 additions & 0 deletions project/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
from rest_framework import generics, status
from django.contrib.postgres.search import SearchVector
from database.models import *
from django.core.mail import *
import random, json, datetime

from backend import settings



# from nltk.corpus import wordnet as wn
# import nltk
Expand Down Expand Up @@ -132,6 +136,15 @@ def post(self, request):
serializer.errors, status=400
)


def send_notification(receiver,subject,content):
receiver = receiver.split(",")
try:
send_mail(subject = subject, message = content,from_email = settings.EMAIL_HOST_USER,recipient_list = receiver)
except:
return Response({"message": "A mistake occured while sending notification."}, status=400)
return Response({"message": "Notification sent successfully."}, status=201)

class SemanticTagAPIView(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated, IsContributorAndWorkspace)
Expand Down Expand Up @@ -181,6 +194,7 @@ def remove_workspace_tag(request):
else:
return JsonResponse({'message': "You don't have permission to do this!"}, status=403)


def search(request):
res = BasicUserDetailAPI.as_view()(request)
try:
Expand Down Expand Up @@ -1263,6 +1277,12 @@ def get_random_node_id(request):
def send_collaboration_request(request):
if not request.user.basicuser.contributor.workspaces.filter(workspace_id=request.data.get('workspace')).exists():
return Response({"message": "This contributor is not allowed to access this workspace."}, status=403)
receiver = BasicUser.objects.filter(id=request.data.get('receiver'))[0]
if receiver.email_notification_preference:
subject = 'Incoming collaboration request'
content = 'You have received a collaboration request!'
receiver = receiver.user
send_notification(receiver, subject, content)
serializer = CollaborationRequestSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
Expand Down Expand Up @@ -1314,6 +1334,17 @@ def send_review_request(request):
if rv not in reviewers:
reviewers.append(rv)
response_data = {'reviewer1': '', 'reviewer2': ''}


data = {'sender': request.data.get('sender'), 'receiver': reviewers[0].id ,'workspace': request.data.get('workspace')}

receiver = BasicUser.objects.filter(id=reviewers[0].id)[0]
if receiver.email_notification_preference:
subject = 'Incoming review request'
content = 'You have received a review request!'
receiver = receiver.user
send_notification(receiver,subject,content)

workspace = request.user.basicuser.contributor.workspaces.filter(workspace_id=request.data.get('workspace'))[0]
if workspace.is_in_review:
return Response({"message": "This workspace is already under review."}, status=403)
Expand All @@ -1323,6 +1354,7 @@ def send_review_request(request):
workspace.is_rejected = False
workspace.save()
data = {'sender': request.data.get('sender'), 'receiver': reviewers[0].id, 'workspace': request.data.get('workspace')}

serializer = ReviewRequestSerializer(data=data)

if serializer.is_valid():
Expand Down Expand Up @@ -1477,6 +1509,8 @@ def update_review_request_status(request):
serializer = ReviewRequestSerializer(req)
return Response(serializer.data, status=200)



class AskQuestion(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
Expand Down
9 changes: 8 additions & 1 deletion project/backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@

WSGI_APPLICATION = 'backend.wsgi.application'


EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD')
EMAIL_USE_TLS = True
ADMIN_EMAIL='[email protected]'
EMAIL_USE_SSL = False
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

Expand Down