back office news reformat

This commit is contained in:
Kuroshini 2019-12-25 18:09:45 +03:00
parent 61eca800fc
commit f3c48281d4
2 changed files with 29 additions and 0 deletions

View File

@ -367,6 +367,17 @@ class News(GalleryMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
{f'{crop[len(f"{model_name}_"):]}_url': image.get_image_url(crop)}) {f'{crop[len(f"{model_name}_"):]}_url': image.get_image_url(crop)})
return image_property return image_property
@property
def descriptions(self):
"""Read-only list field for backoffice news representation"""
return [{
'locale': locale,
'slug': (self.slugs or {}).get(locale),
'status': 'active' if (self.locale_to_description_is_active or {}).get(locale) else 'inactive',
'title': (self.title or {}).get(locale),
'text': desc
} for locale, desc in self.description.items()] if self.description else []
class NewsGallery(IntermediateGalleryModelMixin): class NewsGallery(IntermediateGalleryModelMixin):
news = models.ForeignKey(News, null=True, news = models.ForeignKey(News, null=True,

View File

@ -178,6 +178,7 @@ class NewsDetailWebSerializer(NewsDetailSerializer):
class NewsBackOfficeBaseSerializer(NewsBaseSerializer): class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
"""News back office base serializer.""" """News back office base serializer."""
is_published = serializers.BooleanField(source='is_publish', read_only=True) is_published = serializers.BooleanField(source='is_publish', read_only=True)
descriptions = serializers.ListField(required=False)
class Meta(NewsBaseSerializer.Meta): class Meta(NewsBaseSerializer.Meta):
"""Meta class.""" """Meta class."""
@ -195,6 +196,7 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
'publication_time', 'publication_time',
'created', 'created',
'modified', 'modified',
'descriptions',
) )
extra_kwargs = { extra_kwargs = {
'created': {'read_only': True}, 'created': {'read_only': True},
@ -204,6 +206,22 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
'must_of_the_week': {'read_only': True}, 'must_of_the_week': {'read_only': True},
} }
def validate(self, attrs):
"""Overridden validate method."""
if 'descriptions' in attrs:
descriptions = attrs.pop('descriptions')
status_to_bool = {
'active': True,
'inactive': False,
}
attrs['slugs'] = {obj['locale']: obj['slug'] for obj in descriptions}
attrs['title'] = {obj['locale']: obj['title'] for obj in descriptions}
attrs['locale_to_description_is_active'] = {
obj['locale']: status_to_bool[obj['status']] for obj in descriptions
}
attrs['description'] = {obj['locale']: obj['text'] for obj in descriptions}
return attrs
def create(self, validated_data): def create(self, validated_data):
slugs = validated_data.get('slugs') slugs = validated_data.get('slugs')