def get_entity_privileges(self, obj): """ Privileges of the user for creating workshops. Entities - Points of Contact """ entities = obj.get_entity_privileges() return EntitySerializer(entities, many=True).data
def get_entity_subscriptions(self, obj): """ User subscriptions for entities. """ entities = obj.entity_subscriptions.all() return EntitySerializer(entities, many=True).data
class ProfileSerializer(serializers.ModelSerializer): phone_number = serializers.CharField(max_length=15, validators=[ phone_regex, ], allow_blank=True) club_subscriptions = serializers.SerializerMethodField() entity_subscriptions = serializers.SerializerMethodField() club_privileges = serializers.SerializerMethodField() entity_privileges = serializers.SerializerMethodField() @swagger_serializer_method(serializer_or_field=ClubSerializer(many=True)) def get_club_subscriptions(self, obj): """ User subscriptions for the clubs. """ clubs = obj.club_subscriptions.all().select_related('council') return ClubSerializer(clubs, many=True).data @swagger_serializer_method(serializer_or_field=EntitySerializer(many=True)) def get_entity_subscriptions(self, obj): """ User subscriptions for entities. """ entities = obj.entity_subscriptions.all() return EntitySerializer(entities, many=True).data @swagger_serializer_method(serializer_or_field=ClubSerializer(many=True)) def get_club_privileges(self, obj): """ Privileges of the user for creating workshops. Clubs - Secretary / Joint Secretary, Councils - General Secretary / Joint General Secretary. """ clubs = obj.get_club_privileges() return ClubSerializer(clubs, many=True).data @swagger_serializer_method(serializer_or_field=EntitySerializer(many=True)) def get_entity_privileges(self, obj): """ Privileges of the user for creating workshops. Entities - Points of Contact """ entities = obj.get_entity_privileges() return EntitySerializer(entities, many=True).data def update(self, instance, validated_data): name = validated_data['name'] phone_number = validated_data['phone_number'] # pylint: disable=no-member instance.name = name instance.phone_number = phone_number instance.photo_url = FirebaseAPI.get_photo_url( instance.uid) # update photo_url of user instance.save() logger.info('[PUT/PATCH Response] (%s) : %s', instance, validated_data) return instance class Meta: model = UserProfile read_only_fields = ('id', 'email', 'department', 'year_of_joining', 'club_subscriptions', 'entity_subscriptions', 'club_privileges', 'entity_privileges', 'photo_url') fields = ('id', 'name', 'email', 'phone_number', 'department', 'year_of_joining', 'club_subscriptions', 'entity_subscriptions', 'club_privileges', 'entity_privileges', 'photo_url')