diff --git a/base/messages.py b/base/messages.py index f1d5daa..87f5e9a 100644 --- a/base/messages.py +++ b/base/messages.py @@ -150,12 +150,16 @@ SUCCESS_CODE = { "3038": "Status has been changed successfully.", # notification read "3039": "Notification read successfully", + # start article "3040": "Start article successfully", # complete article "3041": "Article completed successfully", # submit assessment successfully "3042": "Assessment completed successfully", - "3043": "Read article card successfully" + # read article + "3043": "Read article card successfully", + # remove guardian code request + "3044": "Remove guardian code request successfully", } """status code error""" diff --git a/guardian/serializers.py b/guardian/serializers.py index 12d5383..1dabc8d 100644 --- a/guardian/serializers.py +++ b/guardian/serializers.py @@ -355,6 +355,7 @@ class ApproveJuniorSerializer(serializers.ModelSerializer): instance = self.context['junior'] instance.guardian_code = [self.context['guardian_code']] instance.guardian_code_approved = True + instance.guardian_code_status = str(NUMBER['two']) instance.save() return instance diff --git a/guardian/views.py b/guardian/views.py index 8d38395..c67037c 100644 --- a/guardian/views.py +++ b/guardian/views.py @@ -311,6 +311,7 @@ class ApproveJuniorAPIView(viewsets.ViewSet): return custom_response(SUCCESS_CODE['3023'], serializer.data, response_status=status.HTTP_200_OK) else: queryset[1].guardian_code = None + queryset[1].guardian_code_status = str(NUMBER['one']) queryset[1].save() return custom_response(SUCCESS_CODE['3024'], response_status=status.HTTP_200_OK) except Exception as e: diff --git a/junior/models.py b/junior/models.py index 180b425..12d8476 100644 --- a/junior/models.py +++ b/junior/models.py @@ -167,7 +167,8 @@ class JuniorArticle(models.Model): Survey Options model """ # associated junior with the task - junior = models.OneToOneField(Junior, on_delete=models.CASCADE, related_name='juniors_article', verbose_name='Junior') + junior = models.OneToOneField(Junior, on_delete=models.CASCADE, related_name='juniors_article', + verbose_name='Junior') article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='junior_articles_details') # article completed""" is_completed = models.BooleanField(default=False) @@ -184,7 +185,8 @@ class JuniorArticleCard(models.Model): Survey Options model """ # associated junior with the task - junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='juniors_article_card', verbose_name='Junior') + junior = models.ForeignKey(Junior, on_delete=models.CASCADE, related_name='juniors_article_card', + verbose_name='Junior') article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='junior_articles_detail') article_card = models.ForeignKey(ArticleCard, on_delete=models.CASCADE, related_name='junior_article_card') diff --git a/junior/serializers.py b/junior/serializers.py index 1db1f44..003e563 100644 --- a/junior/serializers.py +++ b/junior/serializers.py @@ -492,3 +492,15 @@ class ReAssignTaskSerializer(serializers.ModelSerializer): instance.save() return instance + + +class RemoveGuardianCodeSerializer(serializers.ModelSerializer): + """User task Serializer""" + class Meta(object): + """Meta class""" + model = Junior + fields = ('id', ) + def update(self, instance, validated_data): + instance.guardian_code_status = str(NUMBER['one']) + instance.save() + return instance diff --git a/junior/urls.py b/junior/urls.py index e494a3d..b145d4f 100644 --- a/junior/urls.py +++ b/junior/urls.py @@ -6,7 +6,7 @@ from .views import (UpdateJuniorProfile, ValidateGuardianCode, JuniorListAPIView CompleteJuniorTaskAPIView, JuniorPointsListAPIView, ValidateReferralCode, InviteGuardianAPIView, StartTaskAPIView, ReAssignJuniorTaskAPIView, StartArticleAPIView, StartAssessmentAPIView, CheckAnswerAPIView, CompleteArticleAPIView, ReadArticleCardAPIView, - CreateArticleCardAPIView) + CreateArticleCardAPIView, RemoveGuardianCodeAPIView) """Third party import""" from rest_framework import routers @@ -60,4 +60,5 @@ urlpatterns = [ path('api/v1/reassign-task/', ReAssignJuniorTaskAPIView.as_view()), path('api/v1/complete-article/', CompleteArticleAPIView.as_view()), path('api/v1/read-article-card/', ReadArticleCardAPIView.as_view()), + path('api/v1/remove-guardian-code-request/', RemoveGuardianCodeAPIView.as_view()), ] diff --git a/junior/utils.py b/junior/utils.py index 621a6dd..1afd8d5 100644 --- a/junior/utils.py +++ b/junior/utils.py @@ -50,7 +50,7 @@ def junior_approval_mail(guardian, full_name): def update_positions_based_on_points(): """Update position of the junior""" # First, retrieve all the JuniorPoints instances ordered by total_points in descending order. - juniors_points = JuniorPoints.objects.order_by('-total_points', 'updated_at') + juniors_points = JuniorPoints.objects.order_by('-total_points', 'created_at') # Now, iterate through the queryset and update the position field based on the order. position = 1 diff --git a/junior/views.py b/junior/views.py index 5926c47..b41d2aa 100644 --- a/junior/views.py +++ b/junior/views.py @@ -33,8 +33,8 @@ from junior.models import (Junior, JuniorPoints, JuniorGuardianRelationship, Jun JuniorArticleCard) from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer, RemoveJuniorSerializer, CompleteTaskSerializer, JuniorPointsSerializer, - AddGuardianSerializer, StartTaskSerializer, ReAssignTaskSerializer - ) + AddGuardianSerializer, StartTaskSerializer, ReAssignTaskSerializer, + RemoveGuardianCodeSerializer) from guardian.models import Guardian, JuniorTask from guardian.serializers import TaskDetailsSerializer, TaskDetailsjuniorSerializer from base.messages import ERROR_CODE, SUCCESS_CODE @@ -260,7 +260,8 @@ class RemoveJuniorAPIView(views.APIView): junior_id = self.request.GET.get('id') guardian = Guardian.objects.filter(user__email=self.request.user).last() # fetch junior query - junior_queryset = Junior.objects.filter(id=junior_id, guardian_code__icontains=str(guardian.guardian_code)).last() + junior_queryset = Junior.objects.filter(id=junior_id, + guardian_code__icontains=str(guardian.guardian_code)).last() if junior_queryset: # use RemoveJuniorSerializer serializer serializer = RemoveJuniorSerializer(junior_queryset, data=request.data, partial=True) @@ -621,3 +622,25 @@ class CreateArticleCardAPIView(viewsets.ModelViewSet): return custom_response(None, response_status=status.HTTP_200_OK) except Exception as e: return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST) + +class RemoveGuardianCodeAPIView(views.APIView): + """Update junior task API""" + serializer_class = RemoveGuardianCodeSerializer + permission_classes = [IsAuthenticated] + + def put(self, request, format=None): + try: + junior_queryset = Junior.objects.filter(auth=self.request.user).last() + if junior_queryset: + # use RemoveGuardianCodeSerializer serializer + serializer = RemoveGuardianCodeSerializer(junior_queryset, data=request.data, partial=True) + if serializer.is_valid(): + # save serializer + serializer.save() + return custom_response(SUCCESS_CODE['3044'], response_status=status.HTTP_200_OK) + return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST) + else: + # task in another state + return custom_error_response(ERROR_CODE['2047'], response_status=status.HTTP_400_BAD_REQUEST) + except Exception as e: + return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)