Skip to content

Commit

Permalink
Add unsubscribe functionality at backend (djangoindia#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
prakkhar03 committed Nov 27, 2024
1 parent 1df4925 commit b4b0679
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion backend/djangoindia/api/urls/communication.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.urls import path

from djangoindia.api.views.communication import ContactUsAPIView, SubscriberAPIView
from djangoindia.api.views.communication import ContactUsAPIView, SubscriberAPIView,UnsubscribeAPIView


urlpatterns = [
path("subscriber/", SubscriberAPIView.as_view(), name="subscriber"),
path("unsubscribe/", UnsubscribeAPIView.as_view(), name="unsubscribe"),
path("contact-us/", ContactUsAPIView.as_view(), name="contact-us"),
]
24 changes: 24 additions & 0 deletions backend/djangoindia/api/views/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ def post(self, request, *args, **kwargs):
return Response(
{"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
class UnsubscribeAPIView(generics.GenericAPIView):
def delete(self, request, *args, **kwargs):
try:
email = request.data.get("email")
if not email:
return Response(
{"message": "Email address is required."},
status=status.HTTP_400_BAD_REQUEST,
)
subscriber = Subscriber.objects.filter(email=email).first()
if not subscriber:
return Response(
{"message": "No subscription found for this email."},
status=status.HTTP_404_NOT_FOUND,
)
subscriber.delete()
return Response(
{"message": "You have been unsubscribed. We're sad to see you go. 😢"},
status=status.HTTP_200_OK,
)
except Exception as e:
return Response(
{"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
)


class ContactUsAPIView(generics.GenericAPIView, CreateModelMixin):
Expand Down

0 comments on commit b4b0679

Please sign in to comment.