mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 10:05:21 +00:00
added api for forgot password, verify otp and resend otp for admin
This commit is contained in:
278
web_admin/serializers/article_serializer.py
Normal file
278
web_admin/serializers/article_serializer.py
Normal file
@ -0,0 +1,278 @@
|
||||
"""
|
||||
web_admin serializers file
|
||||
"""
|
||||
# django imports
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from base.constants import (ARTICLE_SURVEY_POINTS, MAX_ARTICLE_CARD, MIN_ARTICLE_SURVEY, MAX_ARTICLE_SURVEY, NUMBER,
|
||||
USER_TYPE, ARTICLE_CARD_IMAGE_FOLDER)
|
||||
# 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, DefaultArticleCardImage
|
||||
from web_admin.utils import pop_id
|
||||
|
||||
USER = get_user_model()
|
||||
|
||||
|
||||
class ArticleCardSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Article Card serializer
|
||||
"""
|
||||
id = serializers.IntegerField(required=False)
|
||||
image = serializers.FileField(required=False)
|
||||
image_url = serializers.URLField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = ArticleCard
|
||||
fields = ('id', 'title', 'description', 'image', 'image_url')
|
||||
|
||||
def create(self, validated_data):
|
||||
if 'image' in validated_data and validated_data['image'] is not None:
|
||||
image = validated_data.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
# upload image on ali baba
|
||||
validated_data['image_url'] = upload_image_to_alibaba(image, filename)
|
||||
article_card = ArticleCard.objects.create(article_id='1', **validated_data)
|
||||
return article_card
|
||||
|
||||
|
||||
class SurveyOptionSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
survey option serializer
|
||||
"""
|
||||
id = serializers.IntegerField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = SurveyOption
|
||||
fields = ('id', 'option', 'is_answer')
|
||||
|
||||
|
||||
class ArticleSurveySerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
article survey serializer
|
||||
"""
|
||||
id = serializers.IntegerField(required=False)
|
||||
survey_options = SurveyOptionSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = ArticleSurvey
|
||||
fields = ('id', 'question', 'survey_options')
|
||||
|
||||
|
||||
class ArticleSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
serializer for article API
|
||||
"""
|
||||
article_cards = ArticleCardSerializer(many=True)
|
||||
article_survey = ArticleSurveySerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = Article
|
||||
fields = ('id', 'title', 'description', 'article_cards', 'article_survey')
|
||||
|
||||
def validate(self, attrs):
|
||||
"""
|
||||
to validate request data
|
||||
:return: validated attrs
|
||||
"""
|
||||
article_cards = attrs.get('article_cards', None)
|
||||
article_survey = attrs.get('article_survey', None)
|
||||
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(MIN_ARTICLE_SURVEY) or int(
|
||||
MAX_ARTICLE_SURVEY) < len(article_survey):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2040']})
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
"""
|
||||
to create article.
|
||||
ID in post data dict is for update api.
|
||||
:return: article object
|
||||
"""
|
||||
article_cards = validated_data.pop('article_cards')
|
||||
article_survey = validated_data.pop('article_survey')
|
||||
|
||||
article = Article.objects.create(**validated_data)
|
||||
|
||||
for card in article_cards:
|
||||
card = pop_id(card)
|
||||
if 'image' in card and card['image'] is not None:
|
||||
image = card.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
# upload image on ali baba
|
||||
card['image_url'] = upload_image_to_alibaba(image, filename)
|
||||
ArticleCard.objects.create(article=article, **card)
|
||||
|
||||
for survey in article_survey:
|
||||
survey = pop_id(survey)
|
||||
options = survey.pop('survey_options')
|
||||
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)
|
||||
|
||||
return article
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
"""
|
||||
to update article and related table
|
||||
:param instance: article object,
|
||||
:return: article object
|
||||
"""
|
||||
article_cards = validated_data.pop('article_cards')
|
||||
article_survey = validated_data.pop('article_survey')
|
||||
instance.title = validated_data.get('title', instance.title)
|
||||
instance.description = validated_data.get('description', instance.description)
|
||||
instance.save()
|
||||
|
||||
# Update or create cards
|
||||
for card_data in article_cards:
|
||||
card_id = card_data.get('id', None)
|
||||
if card_id:
|
||||
card = ArticleCard.objects.get(id=card_id, article=instance)
|
||||
card.title = card_data.get('title', card.title)
|
||||
card.description = card_data.get('description', card.description)
|
||||
if 'image' in card_data and card_data['image'] is not None:
|
||||
image = card_data.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
# upload image on ali baba
|
||||
card.image_url = upload_image_to_alibaba(image, filename)
|
||||
card.save()
|
||||
else:
|
||||
card_data = pop_id(card_data)
|
||||
if 'image' in card_data and card_data['image'] is not None:
|
||||
image = card_data.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
# upload image on ali baba
|
||||
card_data['image_url'] = upload_image_to_alibaba(image, filename)
|
||||
ArticleCard.objects.create(article=instance, **card_data)
|
||||
|
||||
# Update or create survey sections
|
||||
for survey_data in article_survey:
|
||||
survey_id = survey_data.get('id', None)
|
||||
options_data = survey_data.pop('survey_options')
|
||||
if survey_id:
|
||||
survey = ArticleSurvey.objects.get(id=survey_id, article=instance)
|
||||
survey.question = survey_data.get('question', survey.question)
|
||||
survey.save()
|
||||
else:
|
||||
survey_data = pop_id(survey_data)
|
||||
survey = ArticleSurvey.objects.create(article=instance, **survey_data)
|
||||
|
||||
# Update or create survey options
|
||||
for option_data in options_data:
|
||||
option_id = option_data.get('id', None)
|
||||
if option_id:
|
||||
option = SurveyOption.objects.get(id=option_id, survey=survey)
|
||||
option.option = option_data.get('option', option.option)
|
||||
option.is_answer = option_data.get('is_answer', option.is_answer)
|
||||
option.save()
|
||||
else:
|
||||
option_data = pop_id(option_data)
|
||||
SurveyOption.objects.create(survey=survey, **option_data)
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class DefaultArticleCardImageSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Article Card serializer
|
||||
"""
|
||||
image = serializers.FileField(required=False)
|
||||
image_url = serializers.URLField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = DefaultArticleCardImage
|
||||
fields = ('image_name', 'image', 'image_url')
|
||||
|
||||
def validate(self, attrs):
|
||||
"""
|
||||
to validate data
|
||||
:return: validated data
|
||||
"""
|
||||
if 'image' not in attrs and attrs.get('image') is None:
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2061']})
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
"""
|
||||
to create and upload image
|
||||
:return: card_image object
|
||||
"""
|
||||
image = validated_data.pop('image')
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image.name}"
|
||||
if image and image.size == NUMBER['zero']:
|
||||
raise serializers.ValidationError(ERROR_CODE['2035'])
|
||||
# upload image on ali baba
|
||||
validated_data['image_url'] = upload_image_to_alibaba(image, filename)
|
||||
|
||||
card_image = DefaultArticleCardImage.objects.create(**validated_data)
|
||||
return card_image
|
||||
|
||||
|
||||
class UserManagementListSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
user management serializer
|
||||
"""
|
||||
name = serializers.SerializerMethodField()
|
||||
phone_number = serializers.SerializerMethodField()
|
||||
user_type = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = USER
|
||||
fields = ('name', 'email', 'phone_number', 'user_type', 'is_active')
|
||||
|
||||
@staticmethod
|
||||
def get_name(obj):
|
||||
"""
|
||||
:param obj: user object
|
||||
:return: full name
|
||||
"""
|
||||
return (obj.first_name + obj.last_name) if obj.last_name else obj.first_name
|
||||
|
||||
@staticmethod
|
||||
def get_phone_number(obj):
|
||||
"""
|
||||
:param obj: user object
|
||||
:return: user phone number
|
||||
"""
|
||||
if profile := obj.guardian_profile.all().first():
|
||||
return profile.phone
|
||||
elif profile := obj.junior_profile.all().first():
|
||||
return profile.phone
|
||||
else:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_user_type(obj):
|
||||
"""
|
||||
:param obj: user object
|
||||
:return: user type
|
||||
"""
|
||||
if obj.guardian_profile.all().first():
|
||||
return dict(USER_TYPE).get('2')
|
||||
elif obj.junior_profile.all().first():
|
||||
return dict(USER_TYPE).get('1')
|
||||
else:
|
||||
return None
|
Reference in New Issue
Block a user