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

adding user update on application status change signal #48

Merged
merged 4 commits into from
Apr 30, 2023
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
14 changes: 14 additions & 0 deletions agriwise/agriculture_specialist/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.core.mail import EmailMessage
from rest_framework.exceptions import APIException


def send_email(subject, message, recipient_list):
try:
email = EmailMessage(
subject=subject,
body=message,
to=recipient_list,
)
email.send()
except Exception as e:
raise APIException(detail=str(e))
37 changes: 36 additions & 1 deletion agriwise/agriculture_specialist/signals/handlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os

from django.db.models.signals import pre_delete
from agriculture_specialist.helpers import send_email
from django.contrib.auth import get_user_model
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver

from agriwise.agriculture_specialist.models import ProfileUpgradeApplication
Expand All @@ -15,3 +17,36 @@ def _delete_document_file(path):
def delete_document_file(sender, instance, *args, **kwargs):
if instance.documents:
_delete_document_file(instance.documents.path)


@receiver(post_save, sender=ProfileUpgradeApplication)
def update_user_on_application_acceptance(sender, instance, *args, **kwargs):
user = (
get_user_model().objects.filter(profileupgradeapplication=instance.id).first()
)
if not kwargs["created"]:
if instance.status == "A":
user.is_agriculture_specialist = True
user.save()
# sending acceptance email to the user
subject = "Congratulations on Upgrading Your Account"
message = (
f"Dear {user.username},\n\nCongratulations on upgrading your account with us! "
f"With your new account, you'll have access to a variety of features and capabilities "
f"that will help you get the most out of our platform. Thank you for choosing us, and "
f"we look forward to continuing to serve you.\n\nBest regards,\n[Agriwise Team]"
)
recipient_list = [user.email]
send_email(subject=subject, message=message, recipient_list=recipient_list)

elif instance.status == "R":
# sending reject email to the user
subject = "Rejection of Upgrading Your Account"
message = (
f"Dear {user.username},\n\nUnfortunately, We regret to inform you that we are"
f" unable to process your request to upgrade your account at this time due to "
f"not enough application. Please contact us for more information."
f"\n\nBest regards,\n[Agriwise Team]"
)
recipient_list = [user.email]
send_email(subject=subject, message=message, recipient_list=recipient_list)
14 changes: 4 additions & 10 deletions agriwise/agriculture_specialist/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib.auth import get_user_model
from django.http import Http404
from django.shortcuts import get_object_or_404
from rest_framework import permissions, status
Expand Down Expand Up @@ -46,14 +45,6 @@ def put(self, request, pk, *args, **kwargs):
)
application_serializer.is_valid(raise_exception=True)
application_serializer.save()
if application_serializer.data["status"] == "A":
user = (
get_user_model()
.objects.filter(profileupgradeapplication=application.id)
.first()
)
user.is_agriculture_specialist = True
user.save()
return Response(application_serializer.data, status=status.HTTP_202_ACCEPTED)


Expand All @@ -71,7 +62,10 @@ def get(self, request, *args, **kwargs):

def post(self, request, *args, **kwargs):
application_serializer = ProfileUpgradeApplicationUserSerializer(
data={"user": request.user.id, "documents": request.data["documents"]}
data={
"user": request.user.id,
"documents": request.data.get("documents", ""),
}
)
application_serializer.is_valid(raise_exception=True)
application_serializer.save()
Expand Down
Loading