mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 18:07:02 +00:00
added cors Allow specific origins setting, unpublish article api, pagination in notification list
This commit is contained in:
@ -11,6 +11,7 @@ from rest_framework import viewsets, status, views
|
|||||||
# local imports
|
# local imports
|
||||||
from account.utils import custom_response, custom_error_response
|
from account.utils import custom_response, custom_error_response
|
||||||
from base.messages import SUCCESS_CODE, ERROR_CODE
|
from base.messages import SUCCESS_CODE, ERROR_CODE
|
||||||
|
from base.pagination import CustomPageNumberPagination
|
||||||
from base.tasks import notify_task_expiry, notify_top_junior
|
from base.tasks import notify_task_expiry, notify_top_junior
|
||||||
from notifications.constants import TEST_NOTIFICATION
|
from notifications.constants import TEST_NOTIFICATION
|
||||||
from notifications.serializers import RegisterDevice, NotificationListSerializer, ReadNotificationSerializer
|
from notifications.serializers import RegisterDevice, NotificationListSerializer, ReadNotificationSerializer
|
||||||
@ -33,10 +34,10 @@ class NotificationViewSet(viewsets.GenericViewSet):
|
|||||||
"""
|
"""
|
||||||
queryset = Notification.objects.filter(notification_to_id=request.auth.payload['user_id']
|
queryset = Notification.objects.filter(notification_to_id=request.auth.payload['user_id']
|
||||||
).select_related('notification_to').order_by('-id')
|
).select_related('notification_to').order_by('-id')
|
||||||
paginator = self.pagination_class()
|
paginator = CustomPageNumberPagination()
|
||||||
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)
|
||||||
return custom_response(None, serializer.data, count=queryset.count())
|
return paginator.get_paginated_response(serializer.data)
|
||||||
|
|
||||||
@action(methods=['post'], detail=False, url_path='device', url_name='device', serializer_class=RegisterDevice)
|
@action(methods=['post'], detail=False, url_path='device', url_name='device', serializer_class=RegisterDevice)
|
||||||
def fcm_registration(self, request):
|
def fcm_registration(self, request):
|
||||||
|
@ -81,7 +81,7 @@ class ArticleSerializer(serializers.ModelSerializer):
|
|||||||
meta class
|
meta class
|
||||||
"""
|
"""
|
||||||
model = Article
|
model = Article
|
||||||
fields = ('id', 'title', 'description', 'article_cards', 'article_survey')
|
fields = ('id', 'title', 'description', 'is_published', 'article_cards', 'article_survey')
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
"""
|
"""
|
||||||
|
@ -130,6 +130,22 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
|||||||
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='status-change', url_path='status-change',
|
||||||
|
detail=True)
|
||||||
|
def article_status_change(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
article un-publish or publish api method
|
||||||
|
:param request: article id
|
||||||
|
:return: success message
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
article = Article.objects.filter(id=kwargs['pk']).first()
|
||||||
|
article.is_published = False if article.is_published else True
|
||||||
|
article.save(update_fields=['is_published'])
|
||||||
|
return custom_response(SUCCESS_CODE["3038"])
|
||||||
|
except AttributeError:
|
||||||
|
return custom_error_response(ERROR_CODE["2041"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
@action(methods=['get'], url_name='remove-card', url_path='remove-card',
|
@action(methods=['get'], url_name='remove-card', url_path='remove-card',
|
||||||
detail=True)
|
detail=True)
|
||||||
def remove_article_card(self, request, *args, **kwargs):
|
def remove_article_card(self, request, *args, **kwargs):
|
||||||
|
@ -37,7 +37,17 @@ SECRET_KEY = os.getenv('SECRET_KEY')
|
|||||||
DEBUG = os.getenv('DEBUG')
|
DEBUG = os.getenv('DEBUG')
|
||||||
|
|
||||||
# cors allow setting
|
# cors allow setting
|
||||||
CORS_ORIGIN_ALLOW_ALL = True
|
CORS_ORIGIN_ALLOW_ALL = False
|
||||||
|
|
||||||
|
# Allow specific origins
|
||||||
|
CORS_ALLOWED_ORIGINS = [
|
||||||
|
"https://dev-api.zodqaapp.com",
|
||||||
|
"https://qa-api.zodqaapp.com",
|
||||||
|
"https://stage-api.zodqaapp.com",
|
||||||
|
# Add more trusted origins as needed
|
||||||
|
]
|
||||||
|
# if DEBUG:
|
||||||
|
# CORS_ALLOWED_ORIGINS += ["http://localhost:3000"]
|
||||||
|
|
||||||
# allow all host
|
# allow all host
|
||||||
ALLOWED_HOSTS = ['*']
|
ALLOWED_HOSTS = ['*']
|
||||||
@ -53,7 +63,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
# Add Django rest frame work apps here
|
# Add Django rest framework apps here
|
||||||
'django_extensions',
|
'django_extensions',
|
||||||
'storages',
|
'storages',
|
||||||
'drf_yasg',
|
'drf_yasg',
|
||||||
|
@ -20,12 +20,11 @@ from django.urls import path, include
|
|||||||
from drf_yasg import openapi
|
from drf_yasg import openapi
|
||||||
from drf_yasg.views import get_schema_view
|
from drf_yasg.views import get_schema_view
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
schema_view = get_schema_view(openapi.Info(title="Zod Bank API", default_version='v1'), public=True, )
|
schema_view = get_schema_view(openapi.Info(title="Zod Bank API", default_version='v1'), public=True, )
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('apidoc/', schema_view.with_ui('swagger', cache_timeout=None), name='schema-swagger-ui'),
|
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('', include(('account.urls', 'account'), namespace='account')),
|
path('', include(('account.urls', 'account'), namespace='account')),
|
||||||
path('', include('guardian.urls')),
|
path('', include('guardian.urls')),
|
||||||
@ -33,3 +32,6 @@ urlpatterns = [
|
|||||||
path('', include(('notifications.urls', 'notifications'), namespace='notifications')),
|
path('', include(('notifications.urls', 'notifications'), namespace='notifications')),
|
||||||
path('', include(('web_admin.urls', 'web_admin'), namespace='web_admin')),
|
path('', include(('web_admin.urls', 'web_admin'), namespace='web_admin')),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if settings.DEBUG:
|
||||||
|
urlpatterns += [(path('apidoc/', schema_view.with_ui('swagger', cache_timeout=None), name='schema-swagger-ui'))]
|
||||||
|
Reference in New Issue
Block a user