Пример #1
0
class StorySerializer(BaseSerializer):
    class Meta:
        model = Story
        name = 'story'
        plural_name = 'stories'
        fields = (
            'character',
            'creator',
            'created_at',
            'cover_image',
            'duration',
            'id',
            'recording',
            'recordings',
            'scenes',
            'scene_durations',
            'title',
        )

    recording = FileField()
    recordings = DynamicRelationField(
        'bbook_backend.api.serializers.SceneRecordingSerializer',
        many=True,
    )
    scenes = DynamicRelationField(
        'bbook_backend.api.serializers.SceneSerializer',
        many=True,
    )
    character = DynamicRelationField(
        'bbook_backend.api.serializers.CharacterSerializer',
        required=True,
    )
    cover_image = DynamicMethodField(requires=['recordings.scene.', 'scenes.'])
    duration = DynamicMethodField(requires=['recordings.'])
    title = FileField(required=False)

    def get_cover_image(self, instance):
        image = None
        try:
            if instance.scene_durations is not None:
                scene_id = instance.scene_durations[0]['scene']
                # take advantage of prefetching
                image = next(scene.image for scene in instance.scenes.all()
                             if scene.id == scene_id)
            else:
                image = min(instance.recordings.all(),
                            key=lambda rec: rec.order).scene.image
        except Exception as e:
            print('get_cover_image failed', e)

        return FileField().to_representation(image)

    def get_duration(self, instance):
        try:
            if instance.recording is not None:
                return sum([r['duration'] for r in instance.scene_durations])
            return sum([r.duration for r in instance.recordings.all()])
        except Exception as e:
            print('get_duration failed', e)
Пример #2
0
class DogSerializer(DynamicModelSerializer):
    class Meta:
        model = Dog
        fields = ('id', 'name', 'origin', 'fur', 'is_red')

    fur = CharField(source='fur_color')
    is_red = DynamicMethodField(deferred=True, requires=['fur_color'])

    def get_is_red(self, instance):
        return instance.fur_color == 'red'
Пример #3
0
class UserSerializer(DynamicModelSerializer):
    class Meta:
        model = User
        name = 'user'
        name_field = 'name'
        fields = (
            'id',
            'name',
            'permissions',
            'groups',
            'location',
            'last_name',
            'display_name',
            'thumbnail_url',
            'number_of_cats',
            'profile',
            'date_of_birth',
            'favorite_pet_id',
            'favorite_pet',
            'is_dead',
        )
        deferred_fields = (
            'last_name',
            'date_of_birth',
            'display_name',
            'profile',
            'thumbnail_url',
            'favorite_pet_id',
            'favorite_pet',
            'is_dead',
        )
        read_only_fields = ('profile', )

    location = DynamicRelationField('LocationSerializer')
    permissions = DynamicRelationField('PermissionSerializer',
                                       many=True,
                                       help_text='Permissions for this user',
                                       deferred=True)
    groups = DynamicRelationField('GroupSerializer', many=True, deferred=True)
    display_name = DynamicField(source='profile.display_name', read_only=True)
    thumbnail_url = DynamicField(source='profile.thumbnail_url',
                                 read_only=True)
    number_of_cats = DynamicMethodField(requires=['location.cat_set.*'],
                                        deferred=True)

    # Don't set read_only on this field directly. Used in test for
    # Meta.read_only_fields.
    profile = DynamicRelationField('ProfileSerializer', deferred=True)
    favorite_pet = DynamicGenericRelationField(required=False)

    def get_number_of_cats(self, user):
        location = user.location
        return len(location.cat_set.all()) if location else 0
Пример #4
0
class FollowSerializer(serializers.DynamicModelSerializer):
    target = DynamicMethodField(requires=["target"])

    def get_target(self, instance):
        target = instance.target
        tweets = UserProfileSerializer(target).data
        if tweets["tweets"]:
            return tweets

    class Meta:
        model = FollowModel
        fields = ("target", )
Пример #5
0
class FeedSerializer(serializers.DynamicModelSerializer):
    feed = DynamicMethodField()

    def get_feed(self, instance):
        follows = instance.follows.all()
        data = []
        for follow in follows:
            following_userdata = FollowSerializer(follow).data
            if following_userdata["target"]:
                for tweet in following_userdata["target"]["tweets"]:
                    data.append(tweet)
        return sorted(data, key=lambda x: x["created_at"], reverse=True)

    class Meta:
        model = User
        fields = ("feed", )