mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-10-23 08:27:56 +00:00
+2
-1
@@ -74,7 +74,8 @@ ERROR_CODE = {
|
||||
"2041": "Article with given id doesn't exist.",
|
||||
"2042": "Article Card with given id doesn't exist.",
|
||||
"2043": "Article Survey with given id doesn't exist.",
|
||||
"2044": "Task does not exist"
|
||||
"2044": "Task does not exist",
|
||||
"2045": "Invalid guardian"
|
||||
}
|
||||
"""Success message code"""
|
||||
SUCCESS_CODE = {
|
||||
|
||||
+13
-1
@@ -3,7 +3,8 @@
|
||||
from django.conf import settings
|
||||
"""Third party Django app"""
|
||||
from templated_email import send_templated_mail
|
||||
|
||||
from .models import JuniorPoints
|
||||
from django.db.models import F
|
||||
# junior notification
|
||||
# email for sending email
|
||||
# when guardian create junior profile
|
||||
@@ -45,3 +46,14 @@ def junior_approval_mail(guardian, full_name):
|
||||
}
|
||||
)
|
||||
return full_name
|
||||
|
||||
def update_positions_based_on_points():
|
||||
# First, retrieve all the JuniorPoints instances ordered by total_points in descending order.
|
||||
juniors_points = JuniorPoints.objects.order_by('-total_points')
|
||||
|
||||
# Now, iterate through the queryset and update the position field based on the order.
|
||||
position = 1
|
||||
for junior_point in juniors_points:
|
||||
junior_point.position = position
|
||||
junior_point.save()
|
||||
position += 1
|
||||
|
||||
+12
-16
@@ -19,7 +19,7 @@ import requests
|
||||
# Import account's serializer
|
||||
# Import account's task
|
||||
# import junior serializer
|
||||
from junior.models import Junior
|
||||
from junior.models import Junior, JuniorPoints
|
||||
from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer,\
|
||||
RemoveJuniorSerializer, CompleteTaskSerializer, JuniorPointsSerializer)
|
||||
from guardian.models import Guardian, JuniorTask
|
||||
@@ -28,6 +28,7 @@ from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import NUMBER
|
||||
from account.utils import custom_response, custom_error_response
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from .utils import update_positions_based_on_points
|
||||
|
||||
""" Define APIs """
|
||||
# Define validate guardian code API,
|
||||
@@ -97,17 +98,15 @@ class JuniorListAPIView(viewsets.ModelViewSet):
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" junior list"""
|
||||
auth_token = self.request.META['HTTP_AUTHORIZATION']
|
||||
headers_token = {
|
||||
'Authorization': auth_token
|
||||
}
|
||||
# requests.get('https://qa-api.zodqaapp.com/api/v1/top-junior/', headers=headers_token)
|
||||
update_positions_based_on_points()
|
||||
guardian_data = Guardian.objects.filter(user__email=request.user).last()
|
||||
# fetch junior object
|
||||
queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code))
|
||||
# use JuniorDetailListSerializer serializer
|
||||
serializer = JuniorDetailListSerializer(queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
if guardian_data:
|
||||
queryset = Junior.objects.filter(guardian_code__icontains=str(guardian_data.guardian_code))
|
||||
# use JuniorDetailListSerializer serializer
|
||||
serializer = JuniorDetailListSerializer(queryset, many=True)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(ERROR_CODE['2045'], response_status=status.HTTP_200_OK)
|
||||
|
||||
class AddJuniorAPIView(viewsets.ModelViewSet):
|
||||
"""Add Junior by guardian"""
|
||||
@@ -138,7 +137,7 @@ class InvitedJuniorAPIView(viewsets.ModelViewSet):
|
||||
"""Get the queryset for the view"""
|
||||
guardian = Guardian.objects.filter(user__email=self.request.user).last()
|
||||
junior_queryset = Junior.objects.filter(guardian_code__icontains=str(guardian.guardian_code),
|
||||
is_invited=True)
|
||||
is_invited=True)
|
||||
return junior_queryset
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" junior list"""
|
||||
@@ -281,11 +280,8 @@ class JuniorPointsListAPIView(viewsets.ModelViewSet):
|
||||
"""profile view"""
|
||||
queryset = self.get_queryset()
|
||||
# update position of junior
|
||||
token = self.request.META['HTTP_AUTHORIZATION']
|
||||
headers = {
|
||||
'Authorization': token
|
||||
}
|
||||
# requests.get('https://qa-api.zodqaapp.com/api/v1/top-junior/', headers=headers)
|
||||
update_positions_based_on_points()
|
||||
|
||||
serializer = JuniorPointsSerializer(queryset)
|
||||
return custom_response(None, serializer.data, response_status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
@@ -51,7 +51,6 @@ def get_basic_detail(notification_type, from_user_id, to_user_id):
|
||||
def send_notification(notification_type, from_user_id, to_user_id, extra_data):
|
||||
""" used to send the push for the given notification type """
|
||||
(notification_data, from_user, to_user) = get_basic_detail(notification_type, from_user_id, to_user_id)
|
||||
print(notification_data, to_user)
|
||||
user_notification_type = UserNotification.objects.filter(user=to_user).first()
|
||||
# data = notification_data.data
|
||||
data = notification_data
|
||||
@@ -64,7 +63,6 @@ def send_notification(notification_type, from_user_id, to_user_id, extra_data):
|
||||
|
||||
def send_push(user, data):
|
||||
""" used to send push notification to specific user """
|
||||
# if user.push_notification:
|
||||
notification_data = data.pop('data', None)
|
||||
user.fcmdevice_set.filter(active=True).send_message(
|
||||
Message(notification=FirebaseNotification(data['title'], data['body']), data=notification_data)
|
||||
|
||||
+23
-25
@@ -4,6 +4,7 @@ web_admin views file
|
||||
# django imports
|
||||
from rest_framework.viewsets import GenericViewSet, mixins
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.filters import OrderingFilter, SearchFilter
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
@@ -22,13 +23,19 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
article api
|
||||
"""
|
||||
serializer_class = ArticleSerializer
|
||||
# permission_classes = [IsAuthenticated, AdminPermission]
|
||||
queryset = Article.objects.prefetch_related('article_cards',
|
||||
'article_survey',
|
||||
'article_survey__survey_options').order_by('-created_at')
|
||||
|
||||
permission_classes = [IsAuthenticated, AdminPermission]
|
||||
queryset = Article
|
||||
filter_backends = (OrderingFilter, SearchFilter,)
|
||||
search_fields = ['title']
|
||||
http_method_names = ['get', 'post', 'put', 'delete']
|
||||
|
||||
def get_queryset(self):
|
||||
article = self.queryset.objects.filter(is_deleted=False).prefetch_related(
|
||||
'article_cards', 'article_survey', 'article_survey__survey_options'
|
||||
).order_by('-created_at')
|
||||
queryset = self.filter_queryset(article)
|
||||
return queryset
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""
|
||||
article create api method
|
||||
@@ -50,7 +57,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
:param kwargs:
|
||||
:return: success message
|
||||
"""
|
||||
article = self.queryset.filter(id=kwargs['pk']).first()
|
||||
article = self.get_object()
|
||||
serializer = self.serializer_class(article, data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
@@ -64,7 +71,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
:param kwargs:
|
||||
:return: list of article
|
||||
"""
|
||||
queryset = self.queryset.filter(is_deleted=False)
|
||||
queryset = self.get_queryset()
|
||||
paginator = self.pagination_class()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
serializer = self.serializer_class(paginated_queryset, many=True)
|
||||
@@ -78,8 +85,8 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
:param kwargs:
|
||||
:return: article detail data
|
||||
"""
|
||||
queryset = self.queryset.filter(id=kwargs['pk'], is_deleted=False)
|
||||
serializer = self.serializer_class(queryset, many=True)
|
||||
queryset = self.get_object()
|
||||
serializer = self.serializer_class(queryset, many=False)
|
||||
return custom_response(None, data=serializer.data)
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
@@ -90,25 +97,13 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
:param kwargs:
|
||||
:return: success message
|
||||
"""
|
||||
article = self.queryset.filter(id=kwargs['pk']).update(is_deleted=True)
|
||||
article = self.get_object()
|
||||
article.is_deleted = True
|
||||
article.save()
|
||||
if article:
|
||||
return custom_response(SUCCESS_CODE["3029"])
|
||||
return custom_error_response(ERROR_CODE["2041"], status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(methods=['get'], url_name='', url_path='', detail=False)
|
||||
def search_article(self, request):
|
||||
"""
|
||||
article search api method
|
||||
:param request:
|
||||
:return: searched article
|
||||
"""
|
||||
search = request.GET.get('search')
|
||||
queryset = self.queryset.filter(title__icontains=search)
|
||||
paginator = self.pagination_class()
|
||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||
serializer = self.serializer_class(paginated_queryset, many=True)
|
||||
return custom_response(None, data=serializer.data)
|
||||
|
||||
@action(methods=['get'], url_name='remove_card', url_path='remove_card',
|
||||
detail=True, serializer_class=None)
|
||||
def remove_article_card(self, request, *args, **kwargs):
|
||||
@@ -144,7 +139,10 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
@action(methods=['post'], url_name='test-add-card', url_path='test-add-card',
|
||||
detail=False, serializer_class=ArticleCardSerializer)
|
||||
def add_card(self, request):
|
||||
print(request.data)
|
||||
"""
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
|
||||
Reference in New Issue
Block a user