From 2d3c2c16c85118f798c350eec5b603d709812274 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Sun, 17 Dec 2023 01:16:49 +0300 Subject: [PATCH 1/9] Update views.py --- project/backend/api/urls.py | 1 + project/backend/api/views.py | 19 +++++++++++++++++++ project/backend/backend/__init__.py | 1 + project/backend/backend/settings.py | 9 ++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/project/backend/api/urls.py b/project/backend/api/urls.py index 7b4bb1e6..8b3f2760 100644 --- a/project/backend/api/urls.py +++ b/project/backend/api/urls.py @@ -37,5 +37,6 @@ path('get_semantic_suggestion/', get_semantic_suggestion, name='get_semantic_suggestion'), path('ask_question/', AskQuestion.as_view(), name='ask_question'), path('answer_question/', AnswerQuestion.as_view(), name='answer_question'), + path('send_email/', send_notification.as_view(), name='answer_question'), path('update_content_status/', update_content_status, name='update_content_status'), ] diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 5bc87d50..f111bf04 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -12,8 +12,11 @@ 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 @@ -989,6 +992,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,) @@ -1044,6 +1049,20 @@ def has_permission(self, request, view): return False return True +class send_notification(APIView): + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdmin,) + def post(self, request): + receiver = request.data.get('receiver') + receiver = receiver.split(",") + subject = request.data.get('subject') + content = request.data.get('content') + send_mail(subject = subject, message = content,from_email = settings.EMAIL_HOST_USER,recipient_list = receiver) + print(receiver) + return Response({"message": "Notification sent successfully."}, status=201) + + + @authentication_classes((TokenAuthentication,)) @permission_classes((IsAuthenticated, IsAdmin)) @api_view(['PUT']) diff --git a/project/backend/backend/__init__.py b/project/backend/backend/__init__.py index e69de29b..b5649b05 100644 --- a/project/backend/backend/__init__.py +++ b/project/backend/backend/__init__.py @@ -0,0 +1 @@ +default_app_config = 'blog.apps.BlogConfig' \ No newline at end of file diff --git a/project/backend/backend/settings.py b/project/backend/backend/settings.py index ee9b565f..d563a892 100644 --- a/project/backend/backend/settings.py +++ b/project/backend/backend/settings.py @@ -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 = 'sciencecollabplatform@gmail.com' +EMAIL_HOST_PASSWORD = 'scew rrfr trmb ulkw' +EMAIL_USE_TLS = True +ADMIN_EMAIL='sciencecollabplatform@gmail.com' +EMAIL_USE_SSL = False # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases From d133595a9913ca0d441119804bdca70d0ec68807 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Sun, 17 Dec 2023 03:53:58 +0300 Subject: [PATCH 2/9] Update views.py --- project/backend/api/views.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/project/backend/api/views.py b/project/backend/api/views.py index f111bf04..0fc3efe4 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -1057,12 +1057,13 @@ def post(self, request): receiver = receiver.split(",") subject = request.data.get('subject') content = request.data.get('content') - send_mail(subject = subject, message = content,from_email = settings.EMAIL_HOST_USER,recipient_list = receiver) - print(receiver) + 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) - @authentication_classes((TokenAuthentication,)) @permission_classes((IsAuthenticated, IsAdmin)) @api_view(['PUT']) From 8e221de98060ca0aca9a531e9a84ab23bf24b247 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Sun, 17 Dec 2023 03:55:30 +0300 Subject: [PATCH 3/9] Update __init__.py --- project/backend/backend/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project/backend/backend/__init__.py b/project/backend/backend/__init__.py index b5649b05..e69de29b 100644 --- a/project/backend/backend/__init__.py +++ b/project/backend/backend/__init__.py @@ -1 +0,0 @@ -default_app_config = 'blog.apps.BlogConfig' \ No newline at end of file From 55324677d442171250952671a2d6576f212095d0 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Sun, 17 Dec 2023 03:55:51 +0300 Subject: [PATCH 4/9] Update urls.py --- project/backend/api/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/backend/api/urls.py b/project/backend/api/urls.py index 8b3f2760..14fd27f4 100644 --- a/project/backend/api/urls.py +++ b/project/backend/api/urls.py @@ -37,6 +37,6 @@ path('get_semantic_suggestion/', get_semantic_suggestion, name='get_semantic_suggestion'), path('ask_question/', AskQuestion.as_view(), name='ask_question'), path('answer_question/', AnswerQuestion.as_view(), name='answer_question'), - path('send_email/', send_notification.as_view(), name='answer_question'), + path('send_email/', send_notification.as_view(), name='send_email'), path('update_content_status/', update_content_status, name='update_content_status'), ] From 1879baf9120dfa43d8606026dc9ff43191d4b1c7 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Sun, 17 Dec 2023 21:59:39 +0300 Subject: [PATCH 5/9] Update settings.py --- project/backend/backend/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/backend/backend/settings.py b/project/backend/backend/settings.py index d563a892..f81b6193 100644 --- a/project/backend/backend/settings.py +++ b/project/backend/backend/settings.py @@ -81,7 +81,7 @@ EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = 'sciencecollabplatform@gmail.com' -EMAIL_HOST_PASSWORD = 'scew rrfr trmb ulkw' +EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD') EMAIL_USE_TLS = True ADMIN_EMAIL='sciencecollabplatform@gmail.com' EMAIL_USE_SSL = False From b99ae9cd5d72282b119e4a697ef89362cbcb7242 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Tue, 19 Dec 2023 15:30:11 +0300 Subject: [PATCH 6/9] Update views.py --- project/backend/api/views.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 0fc3efe4..8576c754 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -17,6 +17,8 @@ from backend import settings +from project.backend.database.models import BasicUser + # from nltk.corpus import wordnet as wn # import nltk @@ -127,7 +129,17 @@ def post(self, request): serializer.errors, status=400 ) - +class send_notification(APIView): + def post(self, request): + receiver = request.data.get('receiver') + receiver = receiver.split(",") + subject = request.data.get('subject') + content = request.data.get('content') + 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) def search(request): search = request.GET.get("query") @@ -880,6 +892,10 @@ def send_review_request(request): response_data = {'reviewer1': '', 'reviewer2': ''} data = {'sender': request.data.get('sender'), 'receiver': reviewers[0].id, 'workspace': request.data.get('workspace')} + receiver = BasicUser.objects.get(id=reviewers[0].id)[0] + if receiver.email_notification_preference: + data = {'subject':'Incoming review request', 'content':'You have received a review request!','receiver':receiver.user} + send_notification.post(send_notification.self,data) serializer = ReviewRequestSerializer(data=data) if serializer.is_valid(): @@ -1049,21 +1065,6 @@ def has_permission(self, request, view): return False return True -class send_notification(APIView): - authentication_classes = (TokenAuthentication,) - permission_classes = (IsAdmin,) - def post(self, request): - receiver = request.data.get('receiver') - receiver = receiver.split(",") - subject = request.data.get('subject') - content = request.data.get('content') - 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) - - @authentication_classes((TokenAuthentication,)) @permission_classes((IsAuthenticated, IsAdmin)) @api_view(['PUT']) From e5136cf9883877e43dc2937e1d5f915834be8296 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Tue, 19 Dec 2023 15:31:58 +0300 Subject: [PATCH 7/9] Update views.py --- project/backend/api/views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 8576c754..7184c2f6 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -6,7 +6,7 @@ from rest_framework.permissions import IsAuthenticated, AllowAny, BasePermission from database.serializers import * from django.contrib.auth import update_session_auth_hash -from django.contrib.auth.models import User +from django.contrib.auth.models import User, BasicUser from rest_framework.authentication import TokenAuthentication from django.http import JsonResponse from rest_framework import generics, status @@ -17,7 +17,6 @@ from backend import settings -from project.backend.database.models import BasicUser # from nltk.corpus import wordnet as wn From 3043ece47f8494acd33055e56dde2ba62aa1fdf7 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Tue, 19 Dec 2023 15:33:01 +0300 Subject: [PATCH 8/9] Update views.py --- project/backend/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 7184c2f6..15d88412 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -6,7 +6,7 @@ from rest_framework.permissions import IsAuthenticated, AllowAny, BasePermission from database.serializers import * from django.contrib.auth import update_session_auth_hash -from django.contrib.auth.models import User, BasicUser +from django.contrib.auth.models import User from rest_framework.authentication import TokenAuthentication from django.http import JsonResponse from rest_framework import generics, status From 5b08569622860736e36aa984b7a0a8afe2320422 Mon Sep 17 00:00:00 2001 From: Ali Mert Geben Date: Tue, 19 Dec 2023 16:11:17 +0300 Subject: [PATCH 9/9] Update views.py --- project/backend/api/urls.py | 1 - project/backend/api/views.py | 23 ++++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/project/backend/api/urls.py b/project/backend/api/urls.py index 14fd27f4..7b4bb1e6 100644 --- a/project/backend/api/urls.py +++ b/project/backend/api/urls.py @@ -37,6 +37,5 @@ path('get_semantic_suggestion/', get_semantic_suggestion, name='get_semantic_suggestion'), path('ask_question/', AskQuestion.as_view(), name='ask_question'), path('answer_question/', AnswerQuestion.as_view(), name='answer_question'), - path('send_email/', send_notification.as_view(), name='send_email'), path('update_content_status/', update_content_status, name='update_content_status'), ] diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 15d88412..baba60d6 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -128,12 +128,8 @@ def post(self, request): serializer.errors, status=400 ) -class send_notification(APIView): - def post(self, request): - receiver = request.data.get('receiver') +def send_notification(receiver,subject,content): receiver = receiver.split(",") - subject = request.data.get('subject') - content = request.data.get('content') try: send_mail(subject = subject, message = content,from_email = settings.EMAIL_HOST_USER,recipient_list = receiver) except: @@ -842,6 +838,12 @@ def has_permission(self, request, view): 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() @@ -890,11 +892,14 @@ def send_review_request(request): reviewers = random.sample(all_reviewers, 2) response_data = {'reviewer1': '', 'reviewer2': ''} - data = {'sender': request.data.get('sender'), 'receiver': reviewers[0].id, 'workspace': request.data.get('workspace')} - receiver = BasicUser.objects.get(id=reviewers[0].id)[0] + 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: - data = {'subject':'Incoming review request', 'content':'You have received a review request!','receiver':receiver.user} - send_notification.post(send_notification.self,data) + subject = 'Incoming review request' + content = 'You have received a review request!' + receiver = receiver.user + send_notification(receiver,subject,content) serializer = ReviewRequestSerializer(data=data) if serializer.is_valid():