Ejemplo n.º 1
0
class CollectionAddonSerializer(serializers.ModelSerializer):
    addon = SplitField(
        # Only used for writes (this is input field), so there are no perf
        # concerns and we don't use any special caching.
        SlugOrPrimaryKeyRelatedField(queryset=Addon.objects.public()),
        AddonSerializer())
    notes = TranslationSerializerField(source='comments', required=False)
    collection = serializers.HiddenField(default=ThisCollectionDefault())

    class Meta:
        model = CollectionAddon
        fields = ('addon', 'notes', 'collection')
        validators = [
            UniqueTogetherValidator(
                queryset=CollectionAddon.objects.all(),
                message=_(u'This add-on already belongs to the collection'),
                fields=('addon', 'collection')),
        ]
        writeable_fields = ('notes', )
        read_only_fields = tuple(set(fields) - set(writeable_fields))

    def validate(self, data):
        if self.partial:
            # addon is read_only but SplitField messes with the initialization.
            # DRF normally ignores updates to read_only fields, so do the same.
            data.pop('addon', None)
        return super(CollectionAddonSerializer, self).validate(data)

    def to_representation(self, instance):
        request = self.context.get('request')
        out = super(CollectionAddonSerializer,
                    self).to_representation(instance)
        if request and is_gate_active(request, 'collections-downloads-shim'):
            out['downloads'] = 0
        return out
Ejemplo n.º 2
0
class CollectionAddonSerializer(serializers.ModelSerializer):
    addon = SplitField(
        SlugOrPrimaryKeyRelatedField(
            # .no_cache() because django-cache-machine blows up otherwise.
            # Only used for writes (this is input field) so no perf concerns.
            queryset=Addon.objects.public().no_cache()),
        AddonSerializer())
    notes = TranslationSerializerField(source='comments', required=False)
    collection = serializers.HiddenField(default=ThisCollectionDefault())

    class Meta:
        model = CollectionAddon
        fields = ('addon', 'downloads', 'notes', 'collection')
        validators = [
            UniqueTogetherValidator(
                queryset=CollectionAddon.objects.all(),
                message=_(u'This add-on already belongs to the collection'),
                fields=('addon', 'collection')
            ),
        ]
        writeable_fields = (
            'notes',
        )
        read_only_fields = tuple(set(fields) - set(writeable_fields))

    def validate(self, data):
        if self.partial:
            # addon is read_only but SplitField messes with the initialization.
            # DRF normally ignores updates to read_only fields, so do the same.
            data.pop('addon')
        return super(CollectionAddonSerializer, self).validate(data)
Ejemplo n.º 3
0
class CollectionAddonSerializer(serializers.ModelSerializer):
    addon = AddonSerializer()
    notes = TranslationSerializerField(source='comments')

    class Meta:
        model = CollectionAddon
        fields = ('addon', 'downloads', 'notes')
Ejemplo n.º 4
0
 def serialize(self):
     serializer = AddonSerializer(context={'request': self.request})
     return serializer.to_representation(self.addon)
Ejemplo n.º 5
0
 def serialize(self):
     serializer = AddonSerializer(context={'request': self.request})
     return serializer.to_representation(self.addon)
Ejemplo n.º 6
0
class SampleSplitFieldSerializer(serializers.Serializer):
    addon = SplitField(
        serializers.PrimaryKeyRelatedField(queryset=Addon.objects), AddonSerializer()
    )
 def serialize(self):
     # Manually reload the add-on first to clear any cached properties.
     self.addon = Addon.unfiltered.get(pk=self.addon.pk)
     serializer = AddonSerializer(context={'request': self.request})
     return serializer.to_representation(self.addon)
 def serialize(self):
     # Manually reload the add-on first to clear any cached properties.
     self.addon = Addon.unfiltered.get(pk=self.addon.pk)
     serializer = AddonSerializer(context={'request': self.request})
     return serializer.to_representation(self.addon)