Skip to content

Commit

Permalink
bugfix(mp): update boolean field on member
Browse files Browse the repository at this point in the history
  • Loading branch information
DevHugo committed May 6, 2015
1 parent e06d249 commit 3d5c7ce
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions zds/member/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ class Meta:
'show_sign', 'hover_or_click', 'email_for_answer', 'last_visit')
read_only_fields = ('is_active', 'date_joined', 'last_visit',)

def get_fields(self, *args, **kwargs):
fields = super(ProfileValidatorSerializer, self).get_fields(*args, **kwargs)
request = self.context.get('request', None)

if not request.data.get('show_email'):
fields['show_email'].read_only = True
if not request.data.get('show_sign'):
fields['show_sign'].read_only = True
if not request.data.get('hover_or_click'):
fields['hover_or_click'].read_only = True
if not request.data.get('email_for_answer'):
fields['email_for_answer'].read_only = True

return fields

def update(self, instance, validated_data):
"""
Update and return an existing `Profile` instance, given the validated data.
Expand All @@ -104,12 +119,15 @@ def update(self, instance, validated_data):
instance.avatar_url = validated_data.get('avatar_url', instance.avatar_url) or instance.avatar_url
instance.biography = validated_data.get('biography', instance.biography) or instance.biography
instance.sign = validated_data.get('sign', instance.sign) or instance.sign
instance.show_email = validated_data.get('show_email', instance.show_email) or instance.show_email
instance.show_sign = validated_data.get('show_sign', instance.show_sign) or instance.show_sign
instance.hover_or_click = validated_data.get('hover_or_click',
instance.hover_or_click) or instance.hover_or_click
instance.email_for_answer = validated_data.get('email_for_answer',
instance.email_for_answer) or instance.email_for_answer

if validated_data.get('show_email', instance.show_email) != instance.show_email:
instance.show_email = validated_data.get('show_email', instance.show_email)
if validated_data.get('show_sign', instance.show_sign) != instance.show_sign:
instance.show_sign = validated_data.get('show_sign', instance.show_sign)
if validated_data.get('hover_or_click', instance.hover_or_click) != instance.hover_or_click:
instance.hover_or_click = validated_data.get('hover_or_click', instance.hover_or_click)
if validated_data.get('email_for_answer', instance.email_for_answer) != instance.email_for_answer:
instance.email_for_answer = validated_data.get('email_for_answer', instance.email_for_answer)
instance.user.save()
instance.save()
return instance
Expand Down

0 comments on commit 3d5c7ce

Please sign in to comment.