mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-10-23 08:27:56 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ee01af825 | ||
|
|
532b56c687 |
@@ -96,3 +96,10 @@ BYTE_IMAGE_SIZE = 1024
|
||||
|
||||
# validate file size
|
||||
MAX_FILE_SIZE = 1024 * 1024 * 5
|
||||
|
||||
ARTICLE_SURVEY_POINTS = 5
|
||||
MAX_ARTICLE_CARD = 6
|
||||
|
||||
# min and max survey
|
||||
MIN_ARTICLE_SURVEY = 5
|
||||
MAX_ARTICLE_SURVEY = 10
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-20 11:51
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('web_admin', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='articlecard',
|
||||
name='image',
|
||||
field=models.URLField(blank=True, default=None, null=True),
|
||||
),
|
||||
]
|
||||
+1
-1
@@ -28,7 +28,7 @@ class ArticleCard(models.Model):
|
||||
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='article_cards')
|
||||
title = models.CharField(max_length=255)
|
||||
description = models.TextField()
|
||||
image = models.ImageField(upload_to='card_images/')
|
||||
image = models.URLField(null=True, blank=True, default=None)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
||||
@@ -3,10 +3,11 @@ web_admin serializers file
|
||||
"""
|
||||
# django imports
|
||||
from rest_framework import serializers
|
||||
from django.conf import settings
|
||||
|
||||
from base.constants import ARTICLE_SURVEY_POINTS, MAX_ARTICLE_CARD, MIN_ARTICLE_SURVEY, MAX_ARTICLE_SURVEY
|
||||
# local imports
|
||||
from base.messages import ERROR_CODE
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from web_admin.models import Article, ArticleCard, SurveyOption, ArticleSurvey
|
||||
from web_admin.utils import pop_id
|
||||
|
||||
@@ -16,14 +17,23 @@ class ArticleCardSerializer(serializers.ModelSerializer):
|
||||
Article Card serializer
|
||||
"""
|
||||
id = serializers.IntegerField(required=False)
|
||||
image = serializers.FileField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = ArticleCard
|
||||
fields = ('id', 'title', 'description')
|
||||
fields = ('id', 'title', 'description', 'image')
|
||||
|
||||
def create(self, validated_data):
|
||||
image = validated_data.get('image', '')
|
||||
filename = f"article/{image.name}"
|
||||
# upload image on ali baba
|
||||
validated_data['image'] = upload_image_to_alibaba(image, filename)
|
||||
|
||||
article_card = ArticleCard.objects.create(article_id='1', **validated_data)
|
||||
return article_card
|
||||
|
||||
class SurveyOptionSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
@@ -51,7 +61,7 @@ class ArticleSurveySerializer(serializers.ModelSerializer):
|
||||
meta class
|
||||
"""
|
||||
model = ArticleSurvey
|
||||
fields = ('id', 'question', 'points', 'survey_options')
|
||||
fields = ('id', 'question', 'survey_options')
|
||||
|
||||
|
||||
class ArticleSerializer(serializers.ModelSerializer):
|
||||
@@ -76,10 +86,10 @@ class ArticleSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
article_cards = attrs.get('article_cards', None)
|
||||
article_survey = attrs.get('article_survey', None)
|
||||
if article_cards is None or len(article_cards) > int(settings.MAX_ARTICLE_CARD):
|
||||
if article_cards is None or len(article_cards) > int(MAX_ARTICLE_CARD):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2039']})
|
||||
if article_survey is None or len(article_survey) < int(settings.MIN_ARTICLE_SURVEY) or int(
|
||||
settings.MAX_ARTICLE_SURVEY) < len(article_survey):
|
||||
if article_survey is None or len(article_survey) < int(MIN_ARTICLE_SURVEY) or int(
|
||||
MAX_ARTICLE_SURVEY) < len(article_survey):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2040']})
|
||||
return attrs
|
||||
|
||||
@@ -102,7 +112,7 @@ class ArticleSerializer(serializers.ModelSerializer):
|
||||
for survey in article_survey:
|
||||
survey = pop_id(survey)
|
||||
options = survey.pop('survey_options')
|
||||
survey_obj = ArticleSurvey.objects.create(article=article, **survey)
|
||||
survey_obj = ArticleSurvey.objects.create(article=article, points=ARTICLE_SURVEY_POINTS, **survey)
|
||||
for option in options:
|
||||
option = pop_id(option)
|
||||
SurveyOption.objects.create(survey=survey_obj, **option)
|
||||
|
||||
+31
-24
@@ -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
|
||||
@@ -13,7 +14,7 @@ from account.utils import custom_response, custom_error_response
|
||||
from base.messages import SUCCESS_CODE, ERROR_CODE
|
||||
from web_admin.models import Article, ArticleCard, ArticleSurvey
|
||||
from web_admin.permission import AdminPermission
|
||||
from web_admin.serializers import ArticleSerializer
|
||||
from web_admin.serializers import ArticleSerializer, ArticleCardSerializer
|
||||
|
||||
|
||||
class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModelMixin,
|
||||
@@ -23,12 +24,18 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
"""
|
||||
serializer_class = ArticleSerializer
|
||||
permission_classes = [IsAuthenticated, AdminPermission]
|
||||
queryset = Article.objects.prefetch_related('article_cards',
|
||||
'article_survey',
|
||||
'article_survey__survey_options').order_by('-created_at')
|
||||
|
||||
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):
|
||||
@@ -140,3 +135,15 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
return custom_response(SUCCESS_CODE["3031"])
|
||||
except AttributeError:
|
||||
return custom_error_response(ERROR_CODE["2043"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(methods=['post'], url_name='test-add-card', url_path='test-add-card',
|
||||
detail=False, serializer_class=ArticleCardSerializer)
|
||||
def add_card(self, request):
|
||||
"""
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE["3000"])
|
||||
|
||||
@@ -263,9 +263,6 @@ ALIYUN_OSS_BUCKET_NAME = os.getenv('ALIYUN_OSS_BUCKET_NAME')
|
||||
ALIYUN_OSS_ENDPOINT = os.getenv('ALIYUN_OSS_ENDPOINT')
|
||||
ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION')
|
||||
|
||||
MAX_ARTICLE_CARD = os.getenv('MAX_ARTICLE_CARD', 6)
|
||||
MIN_ARTICLE_SURVEY = os.getenv('MIN_ARTICLE_SURVEY', 5)
|
||||
MAX_ARTICLE_SURVEY = os.getenv('MAX_ARTICLE_SURVEY', 10)
|
||||
|
||||
# define static url
|
||||
STATIC_URL = 'static/'
|
||||
|
||||
Reference in New Issue
Block a user