diff --git a/dispatch/api/fields.py b/dispatch/api/fields.py index e77cb1753..561332680 100644 --- a/dispatch/api/fields.py +++ b/dispatch/api/fields.py @@ -4,15 +4,28 @@ from rest_framework.exceptions import ValidationError class JSONField(Field): - def to_internal_value(self, data): return data def to_representation(self, value): return value -class PrimaryKeyField(Field): +class NullBooleanField(Field): + """Forces a Django NullBooleanField to always return False for null values.""" + + def get_attribute(self, instance): + """Overrides the default get_attribute method to convert None values to False.""" + attr = super(NullBooleanField, self).get_attribute(instance) + return True if attr else False + + def to_internal_value(self, data): + return True if data else None + + def to_representation(self, value): + return True if value else False + +class PrimaryKeyField(Field): def __init__(self, serializer, *args, **kwargs): super(PrimaryKeyField, self).__init__(*args, **kwargs) diff --git a/dispatch/api/serializers.py b/dispatch/api/serializers.py index 5c1f63680..371a61b21 100644 --- a/dispatch/api/serializers.py +++ b/dispatch/api/serializers.py @@ -18,7 +18,7 @@ from dispatch.api.validators import ( FilenameValidator, ImageGalleryValidator, PasswordValidator, SlugValidator, AuthorValidator, TemplateValidator) -from dispatch.api.fields import JSONField, PrimaryKeyField, ForeignKeyField +from dispatch.api.fields import JSONField, NullBooleanField, PrimaryKeyField, ForeignKeyField class PersonSerializer(DispatchModelSerializer): """Serializes the Person model.""" @@ -608,6 +608,8 @@ class ArticleSerializer(DispatchModelSerializer, DispatchPublishableSerializer): 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) @@ -770,6 +772,8 @@ class PageSerializer(DispatchModelSerializer, DispatchPublishableSerializer): id = serializers.ReadOnlyField(source='parent_id') slug = serializers.SlugField(validators=[SlugValidator()]) + is_published = NullBooleanField(read_only=True) + featured_image = ImageAttachmentSerializer(required=False, allow_null=True) featured_video = VideoAttachmentSerializer(required=False, allow_null=True)