class PFBModelSerializer(serializers.ModelSerializer):
    """Base serializer for PFBModel

    This base serializer should be used for any serializer that needs
    to serialize a model that inherites from ``PFBModel``. It automatically
    handles setting ``created_by`` and ``modified_by``
    """
    def __init__(self, *args, **kwargs):
        super(PFBModelSerializer, self).__init__(*args, **kwargs)
        self.request = self.context.get('request')

    uuid = serializers.UUIDField(read_only=True)
    createdAt = serializers.DateTimeField(default=CreateOnlyDefault(now),
                                          read_only=True,
                                          source='created_at')
    modifiedAt = serializers.DateTimeField(default=now,
                                           read_only=True,
                                           source='modified_at')

    createdBy = serializers.HiddenField(default=CreateOnlyDefault(
        CurrentUserDefault()),
                                        source='created_by')
    modifiedBy = serializers.HiddenField(default=CurrentUserDefault(),
                                         source='modified_by')

    class Meta:
        model = PFBModel
Exemplo n.º 2
0
class RecipeListSerializer(ModelSerializer):
    """
    Main GET LIST Serializer for Recipes without all the details
    """
    comment_count = IntegerField(read_only=True)
    ingredients = QuantityIngredientSerializer(source='quantity_set',
                                               many=True,
                                               read_only=True)
    slug = CharField(read_only=True)
    tags = TagSerializer(many=True)
    added_by = NestedUserSerializer(read_only=True,
                                    default=CreateOnlyDefault(
                                        CurrentUserDefault()))

    class Meta:
        model = Recipe
        fields = (
            'id',
            'slug',
            'name',
            'created',
            'added_by',
            'ingredients',
            'comment_count',
            'tags',
        )

    @staticmethod
    def setup_eager_loading(queryset):
        "Perform necessary eager loading of data."
        queryset = queryset.select_related('added_by')
        queryset = queryset.prefetch_related('tags', 'quantity_set',
                                             'quantity_set__ingredient')

        return queryset
Exemplo n.º 3
0
class CommentSerializer(ModelSerializer):
    user = NestedUserSerializer(
        read_only=True,
        default=CreateOnlyDefault(CurrentUserDefault()),
    )
    recipe = NestedRecipeListSerializer()

    class Meta:
        model = Comment
        fields = (
            'id',
            'user',
            'recipe',
            'text',
            'updated',
            'created',
        )
        validators = [
            UniqueTogetherValidator(queryset=Comment.objects.all(),
                                    fields=('user', 'recipe'))
        ]

    @staticmethod
    def setup_eager_loading(queryset):
        "Perform necessary eager loading of data."
        queryset = queryset.select_related('recipe', 'recipe__added_by')
        queryset = queryset.prefetch_related(
            'recipe__quantity_set', 'recipe__quantity_set__ingredient')

        return queryset
Exemplo n.º 4
0
 class Meta:
     model = LegalPosition
     fields = "__all__"
     extra_kwargs = {
         'client': {
             'default': CreateOnlyDefault(CurrentUserDefault()),
         }
     }
Exemplo n.º 5
0
 class Meta:
     model = DocumentUpload
     exclude = ('upload_datetime', )
     extra_kwargs = {
         'client': {
             'default': CreateOnlyDefault(CurrentUserDefault()),
         }
     }
Exemplo n.º 6
0
 def __init__(self, *args, **kwargs):
     kwargs['default'] = CreateOnlyDefault(
         CurrentUserDefault()
     )
     if 'read_only' not in kwargs:
         # default to read_only
         kwargs['read_only'] = True
     kwargs['create'] = False
     super(DynamicCreatorField, self).__init__(*args, **kwargs)
Exemplo n.º 7
0
class PostVoteSerializer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(
        queryset=User.objects.all(),
        default=CreateOnlyDefault(CurrentUserDefault()),
    )
    idea = serializers.PrimaryKeyRelatedField(queryset=Idea.objects.all(), )

    class Meta:
        model = Vote
        fields = ('idea', 'user')
Exemplo n.º 8
0
class PostIdeaSerializer(serializers.ModelSerializer):
    main_image = Base64ImageField(required=True)
    user = serializers.PrimaryKeyRelatedField(
        queryset=User.objects.all(),
        default=CreateOnlyDefault(CurrentUserDefault()),
    )

    class Meta:
        model = Idea
        fields = ('title', 'description', 'url_video', 'main_image', 'user')
Exemplo n.º 9
0
class UserListSerializer(ModelSerializer):
    recipe_count = IntegerField(read_only=True)
    user = NestedUserSerializer(
        read_only=True,
        default=CreateOnlyDefault(CurrentUserDefault()),
    )

    class Meta:
        model = UserList
        fields = (
            'id',
            'created',
            'updated',
            'user',
            'name',
            'description',
            'recipe_count',
        )
Exemplo n.º 10
0
class RepositorySerializer(ModelSerializer):
    """Serializer for Repository."""

    created_by = HiddenField(default=CreateOnlyDefault(CurrentUserDefault()))

    class Meta:
        # pylint: disable=missing-docstring
        model = Repository
        fields = (
            'id',
            'slug',
            'name',
            'description',
            'date_created',
            'created_by',
        )
        read_only_fields = (
            'id',
            'slug',
            'date_created',
        )
Exemplo n.º 11
0
class ProfileSerializer(TaggitSerializer, ModelSerializer):
    excluded_feed_tags = TagListSerializerField()
    settings = IsNullSetDefaultJSONField(
        initial=default_settings, default=CreateOnlyDefault(default_settings))

    class Meta:
        model = Profile
        exclude = ('email', 'password')
        read_only_fields = ('is_active', 'is_staff', 'user_permissions',
                            'groups')
        extra_kwargs = {
            'email': {
                'write_only': True
            },
            'password': {
                'write_only': True
            }
        }

    def create(self, validated_data):
        instance = super().create(validated_data)
        instance.set_password(validated_data['password'])
        instance.save()
        return instance