From baeb7e5b930c807b6590fe2ecd604298b66039bf Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Tue, 28 Jan 2020 14:55:13 +0300 Subject: [PATCH] fix news update --- apps/news/serializers.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/apps/news/serializers.py b/apps/news/serializers.py index 35db45d1..2ddae3c1 100644 --- a/apps/news/serializers.py +++ b/apps/news/serializers.py @@ -379,6 +379,30 @@ class NewsBackOfficeDetailSerializer(NewsBackOfficeBaseSerializer, 'duplicates', ) + def validate(self, attrs): + """Overridden validate method.""" + if 'descriptions' in attrs: + descriptions = attrs.pop('descriptions') + locales = list(map(lambda x: x['locale'], descriptions)) + status_to_bool = { + 'active': True, + 'inactive': False, + } + attrs['slugs'] = {obj['locale']: obj['slug'] for obj in descriptions if 'slug' in obj} + attrs['title'] = {obj['locale']: obj['title'] for obj in descriptions if 'title' in obj} + 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 if 'text' in obj} + if self.context['request'].method == 'PATCH': + instance = models.News.objects.get(pk=self.context['request'].data['id']) + for key in ['slugs', 'title', 'locale_to_description_is_active', 'description']: + for locale in locales: + if not attrs[key].get(locale): + attrs[key][locale] = getattr(instance, key).get(locale) + + return attrs + class NewsBackOfficeGallerySerializer(serializers.ModelSerializer): """Serializer class for model NewsGallery."""