From 52067934153e624de470241c88e11ad8976a7a94 Mon Sep 17 00:00:00 2001 From: Parsa Nazer Date: Wed, 19 Mar 2025 20:58:23 +0330 Subject: [PATCH] clean code and add unsubscribe --- backend/account/urls.py | 3 +++ backend/account/views.py | 32 ++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/backend/account/urls.py b/backend/account/urls.py index f3cb639..958cb0c 100644 --- a/backend/account/urls.py +++ b/backend/account/urls.py @@ -2,6 +2,8 @@ from django.urls import path from . import views from djoser.urls.jwt import views as djoser_jwt_views + + urlpatterns = [ path('profile', views.ProfileView.as_view()), path('verify', djoser_jwt_views.TokenVerifyView.as_view(), name='jwt-verify'), @@ -13,6 +15,7 @@ urlpatterns = [ path('address/list', views.GetUserAddressesView.as_view(), name='list-addresses'), path('address/', views.GetIDUserAddressView.as_view(), name='get-ID-address'), path('subscribe', views.SubscribeView.as_view(), name='subscibe'), + path('unsubscribe', views.UnsubscribeView.as_view(), name='unsubscibe'), path('attack/view/', views.ChangeViewAttack.as_view(), name='attack-view'), path('logout', views.LogoutView.as_view(), name='logout'), ] \ No newline at end of file diff --git a/backend/account/views.py b/backend/account/views.py index 2b52687..4c37a3b 100644 --- a/backend/account/views.py +++ b/backend/account/views.py @@ -11,12 +11,11 @@ from django.shortcuts import get_object_or_404, redirect from rest_framework_simplejwt.tokens import RefreshToken import ghasedak_sms from django.views import View -# this works only need to be used -# class APIView(APIView): -# def __init__(self, *args, **kwargs): -# super().__init__(*args, **kwargs) -# if AllowAny in self.permission_classes or not self.permission_classes: -# self.authentication_classes = [] +from rest_framework import serializers +from rest_framework_simplejwt.tokens import RefreshToken + + + class SendOTPView(APIView): permission_classes = [AllowAny] @extend_schema( @@ -190,6 +189,25 @@ class SubscribeView(APIView): return Response(status=status.HTTP_400_BAD_REQUEST) +class UnsubscribeSerializer(serializers.Serializer): + end_point = serializers.CharField() + +class UnsubscribeView(APIView): + permission_classes = [IsAuthenticated] + serializer_class = UnsubscribeSerializer + def post(self, request): + endpoint = request.data.get("end_point") + if not endpoint: + return Response({"detail": "اند پوینت لازم است"}, status=status.HTTP_400_BAD_REQUEST) + + deleted, _ = PushSubscription.objects.filter(user=request.user, endpoint=endpoint).delete() + + if deleted: + return Response({"detail": "با موفقیت اشتراک پاک شد"}, status=status.HTTP_200_OK) + + return Response({"detail": "اند پوینت پیدا نشد"}, status=status.HTTP_404_NOT_FOUND) + + class ChangeViewAttack(View): def get(self, request, pk): attack = get_object_or_404(SecurityBreachAttemptModel, pk=pk) @@ -198,8 +216,6 @@ class ChangeViewAttack(View): return redirect('admin:account_securitybreachattemptmodel_changelist') -from rest_framework import serializers -from rest_framework_simplejwt.tokens import RefreshToken class LogoutSerializer(serializers.Serializer): refresh_token = serializers.CharField(help_text="Refresh token to be blacklisted")