mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-17 02:45:08 +00:00
list, view, edit api for article
This commit is contained in:
@ -4,29 +4,38 @@ web_admin serializers file
|
||||
from rest_framework import serializers
|
||||
|
||||
from web_admin.models import Article, ArticleCard, SurveyOption, ArticleSurvey
|
||||
from web_admin.utils import pop_id
|
||||
|
||||
|
||||
class ArticleCardSerializer(serializers.ModelSerializer):
|
||||
id = serializers.IntegerField(required=False)
|
||||
|
||||
class Meta:
|
||||
model = ArticleCard
|
||||
fields = ('title', 'description')
|
||||
fields = ('id', 'title', 'description')
|
||||
|
||||
|
||||
class SurveyOptionSerializer(serializers.ModelSerializer):
|
||||
id = serializers.IntegerField(required=False)
|
||||
|
||||
class Meta:
|
||||
model = SurveyOption
|
||||
fields = ('option', 'is_answer')
|
||||
fields = ('id', 'option', 'is_answer')
|
||||
|
||||
|
||||
class ArticleSurveySerializer(serializers.ModelSerializer):
|
||||
options = SurveyOptionSerializer(many=True)
|
||||
id = serializers.IntegerField(required=False)
|
||||
survey_options = SurveyOptionSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = ArticleSurvey
|
||||
fields = ('question', 'points', 'options')
|
||||
fields = ('id', 'question', 'points', 'survey_options')
|
||||
|
||||
|
||||
class ArticleSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
serializer for article API
|
||||
"""
|
||||
article_cards = ArticleCardSerializer(many=True)
|
||||
article_survey = ArticleSurveySerializer(many=True)
|
||||
|
||||
@ -35,21 +44,76 @@ class ArticleSerializer(serializers.ModelSerializer):
|
||||
meta class
|
||||
"""
|
||||
model = Article
|
||||
fields = ('title', 'description', 'article_cards', 'article_survey')
|
||||
fields = ('id', 'title', 'description', 'article_cards', 'article_survey')
|
||||
|
||||
def create(self, validated_data):
|
||||
"""
|
||||
to create article.
|
||||
ID in post data dict is for update api.
|
||||
:param validated_data:
|
||||
:return: success message
|
||||
"""
|
||||
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)
|
||||
ArticleCard.objects.create(article=article, **card)
|
||||
|
||||
for survey in article_survey:
|
||||
options = survey.pop('options')
|
||||
survey = pop_id(survey)
|
||||
options = survey.pop('survey_options')
|
||||
survey_obj = ArticleSurvey.objects.create(article=article, **survey)
|
||||
for option in options:
|
||||
option = pop_id(option)
|
||||
SurveyOption.objects.create(survey=survey_obj, **option)
|
||||
|
||||
return article
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
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)
|
||||
card.image = card_data.get('image', card.image)
|
||||
card.save()
|
||||
else:
|
||||
card_data = pop_id(card_data)
|
||||
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
|
||||
|
Reference in New Issue
Block a user