mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 08:34:55 +00:00
article api changes
This commit is contained in:
@ -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):
|
def send_notification(notification_type, from_user_id, to_user_id, extra_data):
|
||||||
""" used to send the push for the given notification type """
|
""" 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)
|
(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()
|
user_notification_type = UserNotification.objects.filter(user=to_user).first()
|
||||||
# data = notification_data.data
|
# data = notification_data.data
|
||||||
data = notification_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):
|
def send_push(user, data):
|
||||||
""" used to send push notification to specific user """
|
""" used to send push notification to specific user """
|
||||||
# if user.push_notification:
|
|
||||||
notification_data = data.pop('data', None)
|
notification_data = data.pop('data', None)
|
||||||
user.fcmdevice_set.filter(active=True).send_message(
|
user.fcmdevice_set.filter(active=True).send_message(
|
||||||
Message(notification=FirebaseNotification(data['title'], data['body']), data=notification_data)
|
Message(notification=FirebaseNotification(data['title'], data['body']), data=notification_data)
|
||||||
|
|||||||
@ -4,6 +4,7 @@ web_admin views file
|
|||||||
# django imports
|
# django imports
|
||||||
from rest_framework.viewsets import GenericViewSet, mixins
|
from rest_framework.viewsets import GenericViewSet, mixins
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.filters import OrderingFilter, SearchFilter
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
@ -22,13 +23,19 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
|||||||
article api
|
article api
|
||||||
"""
|
"""
|
||||||
serializer_class = ArticleSerializer
|
serializer_class = ArticleSerializer
|
||||||
# permission_classes = [IsAuthenticated, AdminPermission]
|
permission_classes = [IsAuthenticated, AdminPermission]
|
||||||
queryset = Article.objects.prefetch_related('article_cards',
|
queryset = Article
|
||||||
'article_survey',
|
filter_backends = (OrderingFilter, SearchFilter,)
|
||||||
'article_survey__survey_options').order_by('-created_at')
|
search_fields = ['title']
|
||||||
|
|
||||||
http_method_names = ['get', 'post', 'put', 'delete']
|
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):
|
def create(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
article create api method
|
article create api method
|
||||||
@ -50,7 +57,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
|||||||
:param kwargs:
|
:param kwargs:
|
||||||
:return: success message
|
:return: success message
|
||||||
"""
|
"""
|
||||||
article = self.queryset.filter(id=kwargs['pk']).first()
|
article = self.get_object()
|
||||||
serializer = self.serializer_class(article, data=request.data)
|
serializer = self.serializer_class(article, data=request.data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
serializer.save()
|
serializer.save()
|
||||||
@ -64,7 +71,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
|||||||
:param kwargs:
|
:param kwargs:
|
||||||
:return: list of article
|
:return: list of article
|
||||||
"""
|
"""
|
||||||
queryset = self.queryset.filter(is_deleted=False)
|
queryset = self.get_queryset()
|
||||||
paginator = self.pagination_class()
|
paginator = self.pagination_class()
|
||||||
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
paginated_queryset = paginator.paginate_queryset(queryset, request)
|
||||||
serializer = self.serializer_class(paginated_queryset, many=True)
|
serializer = self.serializer_class(paginated_queryset, many=True)
|
||||||
@ -78,8 +85,8 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
|||||||
:param kwargs:
|
:param kwargs:
|
||||||
:return: article detail data
|
:return: article detail data
|
||||||
"""
|
"""
|
||||||
queryset = self.queryset.filter(id=kwargs['pk'], is_deleted=False)
|
queryset = self.get_object()
|
||||||
serializer = self.serializer_class(queryset, many=True)
|
serializer = self.serializer_class(queryset, many=False)
|
||||||
return custom_response(None, data=serializer.data)
|
return custom_response(None, data=serializer.data)
|
||||||
|
|
||||||
def destroy(self, request, *args, **kwargs):
|
def destroy(self, request, *args, **kwargs):
|
||||||
@ -90,25 +97,13 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
|||||||
:param kwargs:
|
:param kwargs:
|
||||||
:return: success message
|
: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:
|
if article:
|
||||||
return custom_response(SUCCESS_CODE["3029"])
|
return custom_response(SUCCESS_CODE["3029"])
|
||||||
return custom_error_response(ERROR_CODE["2041"], status.HTTP_400_BAD_REQUEST)
|
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',
|
@action(methods=['get'], url_name='remove_card', url_path='remove_card',
|
||||||
detail=True, serializer_class=None)
|
detail=True, serializer_class=None)
|
||||||
def remove_article_card(self, request, *args, **kwargs):
|
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',
|
@action(methods=['post'], url_name='test-add-card', url_path='test-add-card',
|
||||||
detail=False, serializer_class=ArticleCardSerializer)
|
detail=False, serializer_class=ArticleCardSerializer)
|
||||||
def add_card(self, request):
|
def add_card(self, request):
|
||||||
print(request.data)
|
"""
|
||||||
|
:param request:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
serializer.save()
|
serializer.save()
|
||||||
|
|||||||
Reference in New Issue
Block a user