コード例 #1
0
ファイル: serializers.py プロジェクト: sezginacer/neksflis
class SubscriptionDetailSerializer(serializers.Serializer):
    payment_option = serializers.SlugRelatedField(
        slug_field='slug',
        queryset=PaymentOption.objects.filter(is_active=True))
    plan = EnumChoiceField(enum_class=SubscriptionPlan)
    status = EnumChoiceField(enum_class=SubscriptionStatus)
    last_use_date = serializers.DateTimeField(read_only=True)
コード例 #2
0
    def test_to_internal_value_fails_when_value_not_in_enum_class(self):
        failing_value = 5
        field = EnumChoiceField(enum_class=IntTestEnum)

        with self.assertRaisesMessage(ValidationError,
                                      'Key 5 is not a valid IntTestEnum'):
            field.to_internal_value(failing_value)
コード例 #3
0
ファイル: serializers.py プロジェクト: sezginacer/neksflis
class SubscribeSerializer(serializers.Serializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    payment_option = serializers.SlugRelatedField(
        slug_field='slug',
        queryset=PaymentOption.objects.filter(is_active=True))
    plan = EnumChoiceField(enum_class=SubscriptionPlan)
    credit_card = CreditCardSerializer()

    def validate(self, attrs):
        service = SubscriptionService()
        subscription = service.get_active_subscription(attrs['user'])
        if subscription:
            raise serializers.ValidationError(
                _('You have already an active subscription.'))
        return attrs
コード例 #4
0
ファイル: serializers.py プロジェクト: sezginacer/neksflis
class PaymentOptionSerializer(serializers.ModelSerializer):
    gateway = EnumChoiceField(GatewayChoices)

    def validate(self, attrs):
        serializer = attrs['gateway'].config_serializer_class(
            data=attrs['config'])
        serializer.is_valid()
        if serializer.errors:
            raise serializers.ValidationError(dict(config=serializer.errors))
        attrs['config'] = serializer.validated_data
        return attrs

    class Meta:
        model = PaymentOption
        fields = '__all__'
        extra_kwargs = {'config': {'required': True}}
コード例 #5
0
class RecommendationListSerializer(serializers.ModelSerializer):
    recommendations = RecommendationSerializer(many=True, required=True)
    user = RelatedCustomUserField()
    category = EnumChoiceField(CategoryEnum)
    tags = TagSerializer(many=True, required=False)

    class Meta:
        model = RecommendationList
        fields = [
            'id', 'recommendations', 'tags', 'user', 'is_draft', 'photo',
            'category', 'header'
        ]
        extra_kwargs = {'tags': {'required': False}}

    def create(self, validated_data):
        recommendations = validated_data.pop('recommendations')
        tags = validated_data.pop('tags', [])
        recommendation_list = super().create(validated_data)
        recs = []
        for recommendation in recommendations:
            recommendation['recommendation_list'] = recommendation_list
            recs.append(Recommendation(**recommendation))
        Recommendation.objects.bulk_create(recs)
        for tag in tags:
            new_tag, created = Tag.objects.get_or_create(name=tag['name'])
            recommendation_list.tags.add(new_tag)
        return recommendation_list

    def update(self, instance, validated_data):
        if 'tags' in validated_data:
            tags = validated_data.pop('tags')
            instance.tags.clear()
            for tag in tags:
                new_tag, created = Tag.objects.get_or_create(name=tag['name'])
                instance.tags.add(new_tag)
        if 'recommendations' in validated_data:
            recommendations = validated_data.pop('recommendations')
            recs = []
            for recommendation in recommendations:
                recommendation['recommendation_list'] = instance
                recs.append(Recommendation(**recommendation))
            Recommendation.objects.bulk_create(recs)
        return super().update(instance, validated_data)
コード例 #6
0
 class Serializer(serializers.Serializer):
     enumeration = EnumChoiceField(enum_class=CharTestEnum,
                                   allow_null=True)
コード例 #7
0
 class Serializer(serializers.Serializer):
     enumeration = EnumChoiceField(enum_class=CharTestEnum)
コード例 #8
0
 class Serializer(serializers.Serializer):
     enumeration = EnumChoiceField(enum_class=CharTestEnum,
                                   required=False)
コード例 #9
0
    class Serializer(serializers.ModelSerializer):
        enumeration = EnumChoiceField(enum_class=CharTestEnum)

        class Meta:
            model = StringEnumeratedModel
            fields = ('enumeration', )
コード例 #10
0
    def test_to_representation_returns_primitive_int_value(self):
        field = EnumChoiceField(enum_class=IntTestEnum)

        result = field.to_representation(IntTestEnum.FIRST)

        self.assertEqual(result, '1')
コード例 #11
0
    def test_to_internal_value_returns_enum_value_when_value_is_string(self):
        field = EnumChoiceField(enum_class=CharTestEnum)

        result = field.to_internal_value('first')

        self.assertEqual(result, CharTestEnum.FIRST)
コード例 #12
0
    def test_to_internal_value_returns_enum_value_when_value_is_int(self):
        field = EnumChoiceField(enum_class=IntTestEnum)

        result = field.to_internal_value('1')

        self.assertEqual(result, IntTestEnum.FIRST)
コード例 #13
0
    def test_to_representation_returns_primitive_string_value(self):
        field = EnumChoiceField(enum_class=CharTestEnum)

        result = field.to_representation(CharTestEnum.FIRST)

        self.assertEqual(result, 'first')
コード例 #14
0
class CustomChoiceBuilderSerializer(serializers.Serializer):
    enumerated_field = EnumChoiceField(MyEnum,
                                       choice_builder=custom_choice_builder)
コード例 #15
0
class MyModelSerializer(serializers.ModelSerializer):
    enumerated_field = EnumChoiceField(MyEnum)

    class Meta:
        model = MyModel
        fields = ('enumerated_field', )
コード例 #16
0
class MySerializer(serializers.Serializer):
    enumerated_field = EnumChoiceField(MyEnum)