fix news update
This commit is contained in:
parent
475de626ce
commit
baeb7e5b93
|
|
@ -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."""
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user