Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
authors = [json.loads(author) for author in authors]
if authors:
instance.save_authors(authors)
tag_ids = validated_data.get('tag_ids', False)
if tag_ids and isinstance(tag_ids, list) and len(tag_ids) == 1 and isinstance(tag_ids[0], str):
tag_ids = [int(tag) for tag in tag_ids[0].split(',')]
if tag_ids != False:
instance.save_tags(tag_ids)
return instance
class TopicSerializer(DispatchModelSerializer):
"""Serializes the Topic model."""
class Meta:
model = Topic
fields = (
'id',
'name',
)
class VideoAttachmentSerializer(DispatchModelSerializer):
"""Serializes the ImageAttachment model without including full Image instance."""
video = VideoSerializer(read_only=True)
video_id = serializers.IntegerField(write_only=True, required=False, allow_null=True)
class Meta:
instance.description = validated_data.get('description', instance.description)
# Save instance before processing/saving content in order to save associations to correct ID
instance.save()
authors = validated_data.get('author_ids')
instance.save_authors(authors, is_publishable=False)
article_ids = validated_data.get('article_ids')
instance.save_articles(article_ids)
return instance
class ArticleSerializer(DispatchModelSerializer, DispatchPublishableSerializer):
"""Serializes the Article model."""
id = serializers.ReadOnlyField(source='parent_id')
slug = serializers.SlugField(validators=[SlugValidator()])
is_published = NullBooleanField(read_only=True)
section = SectionSerializer(read_only=True)
section_id = serializers.IntegerField(write_only=True)
subsection = SubsectionSerializer(source='get_subsection', read_only=True)
subsection_id = serializers.IntegerField(write_only=True, required=False, allow_null=True)
featured_image = ImageAttachmentSerializer(required=False, allow_null=True)
featured_video = VideoAttachmentSerializer(required=False, allow_null=True)
def update(self, instance, validated_data):
instance.email = validated_data.get('email', instance.email)
instance.person = validated_data.get('person', instance.person)
if validated_data.get('password_a'):
instance.set_password(validated_data['password_a'])
instance.save()
permissions = validated_data.get('permission_level')
instance.modify_permissions(permissions)
return instance
class InviteSerializer(DispatchModelSerializer):
"""Serializes the Invite model."""
email = serializers.EmailField(
required=True,
validators=[UniqueValidator(queryset=User.objects.all())]
)
person = ForeignKeyField(
model=Person,
serializer=PersonSerializer(),
validators=[UniqueValidator(queryset=User.objects.all())]
)
permissions = serializers.CharField(
required=False,
allow_blank=True
'id',
'email',
'person',
'permissions',
'expiration_date'
)
def create(self, validated_data):
instance = Invite()
return self.update(instance, validated_data)
def update(self, instance, validated_data):
send_invitation(validated_data['email'], instance.id)
return super(InviteSerializer, self).update(instance, validated_data)
class FileSerializer(DispatchModelSerializer):
"""Serializes the File model."""
file = serializers.FileField(write_only=True, validators=[FilenameValidator])
url = serializers.CharField(source='get_absolute_url', read_only=True)
class Meta:
model = File
fields = (
'id',
'name',
'file',
'url',
'created_at',
'updated_at'
)
class SubsectionArticleSerializer(DispatchModelSerializer):
"""Serializes articles for the Subsection model"""
id = serializers.ReadOnlyField(source='parent_id')
authors = AuthorSerializer(many=True, read_only=True)
class Meta:
model = Article
fields = (
'id',
'headline',
'authors',
'published_version'
)
class SubsectionSerializer(DispatchModelSerializer):
"""Serializes the Subsection model"""
authors = AuthorSerializer(many=True, read_only=True)
author_ids = serializers.ListField(
write_only=True,
child=serializers.JSONField(),
validators=[AuthorValidator(False)]
)
authors_string = serializers.CharField(source='get_author_string', read_only=True)
articles = SubsectionArticleSerializer(many=True, read_only=True, source='get_articles')
article_ids = serializers.ListField(
write_only=True,
child=serializers.JSONField(),
required=False
)
section = SectionSerializer(read_only=True)
class Meta:
model = Person
fields = (
'id',
'full_name',
'title',
'slug',
'description',
'image',
'image_url',
'twitter_url',
'facebook_url'
)
class AuthorSerializer(DispatchModelSerializer):
"""Serializes the Author model."""
person = PersonSerializer()
class Meta:
model = Author
fields = (
'id',
'person',
'type'
)
class UserSerializer(DispatchModelSerializer):
"""Serializes the User model."""
email = serializers.EmailField(
Article, Image, ImageAttachment, ImageGallery, Issue,
File, Page, Author, Section, Tag, Topic, Video,
VideoAttachment, Poll, PollAnswer, PollVote, Subsection)
from dispatch.modules.auth.models import Person, User, Invite
from dispatch.modules.podcasts.models import Podcast, PodcastEpisode
from dispatch.admin.registration import send_invitation
from dispatch.theme.exceptions import WidgetNotFound, InvalidField
from dispatch.api.mixins import DispatchModelSerializer, DispatchPublishableSerializer
from dispatch.api.validators import (
FilenameValidator, ImageGalleryValidator, PasswordValidator,
SlugValidator, AuthorValidator, TemplateValidator, SectionValidator)
from dispatch.api.fields import JSONField, NullBooleanField, PrimaryKeyField, ForeignKeyField
class PersonSerializer(DispatchModelSerializer):
"""Serializes the Person model."""
image = serializers.ImageField(required=False, validators=[FilenameValidator], write_only=True)
image_url = serializers.CharField(source='get_absolute_image_url', read_only=True)
class Meta:
model = Person
fields = (
'id',
'full_name',
'title',
'slug',
'description',
'image',
'image_url',
'twitter_url',
'issue',
'url',
'date',
)
class TagSerializer(DispatchModelSerializer):
"""Serializes the Tag model."""
class Meta:
model = Tag
fields = (
'id',
'name',
)
class VideoSerializer(DispatchModelSerializer):
"""Serializes the Video model."""
title = serializers.CharField(required=False, allow_null=True, allow_blank=True)
url = serializers.CharField(required=True, allow_null=True, allow_blank=False)
authors = AuthorSerializer(many=True, read_only=True)
author_ids = serializers.ListField(
write_only=True,
allow_empty=False,
required=True,
child=serializers.JSONField(),
validators=[AuthorValidator(True)])
tags = TagSerializer(many=True, read_only=True)
tag_ids = serializers.ListField(
write_only=True,
class Meta:
model = Issue
fields = (
'id',
'title',
'file',
'file_str',
'img',
'img_str',
'volume',
'issue',
'url',
'date',
)
class TagSerializer(DispatchModelSerializer):
"""Serializes the Tag model."""
class Meta:
model = Tag
fields = (
'id',
'name',
)
class VideoSerializer(DispatchModelSerializer):
"""Serializes the Video model."""
title = serializers.CharField(required=False, allow_null=True, allow_blank=True)
url = serializers.CharField(required=True, allow_null=True, allow_blank=False)
authors = AuthorSerializer(many=True, read_only=True)
class VideoAttachmentSerializer(DispatchModelSerializer):
"""Serializes the ImageAttachment model without including full Image instance."""
video = VideoSerializer(read_only=True)
video_id = serializers.IntegerField(write_only=True, required=False, allow_null=True)
class Meta:
model = VideoAttachment
fields = (
'video',
'video_id',
'caption',
'credit'
)
class ImageAttachmentSerializer(DispatchModelSerializer):
"""Serializes the ImageAttachment model without including full Image instance."""
image = ImageSerializer(read_only=True)
image_id = serializers.IntegerField(write_only=True, required=False, allow_null=True)
class Meta:
model = ImageAttachment
fields = (
'image',
'image_id',
'style',
'width',
'caption',
'credit'
)