Exemplo n.º 1
0
    def field_to_native(self, obj, field_name):
        if not hasattr(self, 'context') or not 'request' in self.context:
            raise ImproperlyConfigured('Pass request in self.context when'
                                       ' using CollectionMembershipField.')

        request = self.context['request']

        # Having 'use-es-for-apps' in the context means the parent view wants
        # us to use ES to fetch the apps. If that key is present, check that we
        # have a view in the context and that the waffle flag is active. If
        # everything checks out, bypass the db and use ES to fetch apps for a
        # nice performance boost.
        if self.context.get('use-es-for-apps') and self.context.get('view'):
            return self.field_to_native_es(obj, request)

        qs = get_component(obj, self.source)

        # Filter apps based on device and feature profiles.
        device = self._get_device(request)
        profile = get_feature_profile(request)
        if device and device != amo.DEVICE_DESKTOP:
            qs = qs.filter(addondevicetype__device_type=device.id)
        if profile:
            qs = qs.filter(**profile.to_kwargs(
                prefix='_current_version__features__has_'))

        return self.to_native(qs)
Exemplo n.º 2
0
    def field_to_native(self, obj, field_name):
        if self.many:
            # To-many relationship

            queryset = None
            if not self.source:
                # Prefer obj.serializable_value for performance reasons
                try:
                    queryset = obj.serializable_value(field_name)
                except AttributeError:
                    pass
            if queryset is None:
                # RelatedManager (reverse relationship)
                source = self.source or field_name
                queryset = obj
                for component in source.split('.'):
                    queryset = get_component(queryset, component)

            # Forward relationship
            return [self.to_native(item.pk) for item in queryset.all()]

        # To-one relationship
        try:
            # Prefer obj.serializable_value for performance reasons
            pk = obj.serializable_value(self.source or field_name)
        except AttributeError:
            # RelatedObject (reverse relationship)
            try:
                pk = getattr(obj, self.source or field_name).pk
            except ObjectDoesNotExist:
                return None

        # Forward relationship
        return self.to_native(pk)
Exemplo n.º 3
0
    def field_to_native(self, obj, field_name):
        if not hasattr(self, 'context') or not 'request' in self.context:
            raise ImproperlyConfigured('Pass request in self.context when'
                                       ' using CollectionMembershipField.')

        request = self.context['request']

        # Having 'use-es-for-apps' in the context means the parent view wants
        # us to use ES to fetch the apps. If that key is present, check that we
        # have a view in the context and that the waffle flag is active. If
        # everything checks out, bypass the db and use ES to fetch apps for a
        # nice performance boost.
        if self.context.get('use-es-for-apps') and self.context.get('view'):
            return self.field_to_native_es(obj, request)

        qs = get_component(obj, self.source)

        # Filter apps based on device and feature profiles.
        device = self._get_device(request)
        profile = get_feature_profile(request)
        if device and device != amo.DEVICE_DESKTOP:
            qs = qs.filter(addondevicetype__device_type=device.id)
        if profile:
            qs = qs.filter(**profile.to_kwargs(
                prefix='_current_version__features__has_'))

        return self.to_native(qs)
Exemplo n.º 4
0
    def field_to_native(self, obj, field_name):
        try:
            if self.source == '*':
                return self.to_native(obj)

            source = self.source or field_name
            value = obj

            for component in source.split('.'):
                if value is None:
                    break
                value = get_component(value, component)
        except ObjectDoesNotExist:
            return None

        if value is None:
            return None

        if self.many:
            if is_simple_callable(getattr(value, 'all', None)):
                return [self.to_native(item) for item in value.all()]
            else:
                # Also support non-queryset iterables.
                # This allows us to also support plain lists of related items.
                return [self.to_native(item) for item in value]
        return self.to_native(value)
Exemplo n.º 5
0
    def field_to_native(self, obj, field_name):
        if not hasattr(self, 'context') or not 'request' in self.context:
            raise ImproperlyConfigured('Pass request in self.context when'
                                       ' using CollectionMembershipField.')

        request = self.context['request']

        # Having 'use-es-for-apps' in the context means the parent view wants us
        # to use ES to fetch the apps. If that key is present, check that we
        # have a view in the context and that the waffle flag is active. If
        # everything checks out, bypass the db and use ES to fetch apps for a
        # nice performance boost.
        if ('use-es-for-apps' in self.context and 'view' in self.context
            and waffle.switch_is_active('collections-use-es-for-apps')):
            return self.field_to_native_es(obj, request)

        qs = get_component(obj, self.source)

        # Filter apps based on feature profiles.
        profile = get_feature_profile(request)
        if profile:
            qs = qs.filter(**profile.to_kwargs(
                prefix='_current_version__features__has_'))

        return [self.to_native(app) for app in qs]
Exemplo n.º 6
0
    def field_to_native(self, obj, field_name):
        try:
            if self.source == '*':
                return self.to_native(obj)

            source = self.source or field_name
            value = obj

            for component in source.split('.'):
                if value is None:
                    break
                value = get_component(value, component)
        except ObjectDoesNotExist:
            return None

        if value is None:
            return None

        if self.many:
            if is_simple_callable(getattr(value, 'all', None)):
                return [self.to_native(item) for item in value.all()]
            else:
                # Also support non-queryset iterables.
                # This allows us to also support plain lists of related items.
                return [self.to_native(item) for item in value]
        return self.to_native(value)
Exemplo n.º 7
0
    def field_to_native(self, obj, field_name):
        source = self.source or field_name
        value = obj
        for component in source.split('.'):
            value = fields.get_component(value, component)
            if value is None:
                break

        field = value
        if field is None:
            return None
        if self.requested_language:
            return self.fetch_single_translation(obj, source, field)
        else:
            return self.fetch_all_translations(obj, source, field)
Exemplo n.º 8
0
    def field_to_native(self, obj, field_name):
        source = self.source or field_name
        value = obj
        for component in source.split('.'):
            value = fields.get_component(value, component)
            if value is None:
                break

        field = value
        if field is None:
            return None
        if self.requested_language:
            return self.fetch_single_translation(obj, source, field)
        else:
            return self.fetch_all_translations(obj, source, field)
Exemplo n.º 9
0
    def field_to_native(self, obj, field_name):
        value = get_component(obj, self.source)

        # Filter apps based on feature profiles.
        if hasattr(self, 'context') and 'request' in self.context:
            sig = self.context['request'].GET.get('pro')
            if sig:
                try:
                    profile = FeatureProfile.from_signature(sig)
                except ValueError:
                    pass
                else:
                    value = value.filter(**profile.to_kwargs(
                        prefix='app___current_version__features__has_'))

        return [self.to_native(item) for item in value.all()]
Exemplo n.º 10
0
    def field_to_native(self, obj, field_name):
        source = self.source or field_name
        value = obj
        for component in source.split("."):
            value = fields.get_component(value, component)
            if value is None:
                break

        field = value
        if field is None:
            return None
        if not self.return_all_translations:
            return unicode(field)
        else:
            translations = field.__class__.objects.filter(id=field.id, localized_string__isnull=False)
            return dict((to_language(trans.locale), unicode(trans)) for trans in translations)
Exemplo n.º 11
0
    def field_to_native(self, obj, field_name):
        value = get_component(obj, self.source)

        # Filter apps based on feature profiles.
        if hasattr(self, 'context') and 'request' in self.context:
            sig = self.context['request'].GET.get('pro')
            if sig:
                try:
                    profile = FeatureProfile.from_signature(sig)
                except ValueError:
                    pass
                else:
                    value = value.filter(**profile.to_kwargs(
                        prefix='app___current_version__features__has_'))

        return [self.to_native(item) for item in value.all()]
Exemplo n.º 12
0
    def field_to_native(self, obj, field_name):
        if not hasattr(self, "context") or not "request" in self.context:
            raise ImproperlyConfigured("Pass request in self.context when" " using CollectionMembershipField.")

        request = self.context["request"]

        # If we have a search resource in the context, we should try to use ES
        # to fetch the apps, if the waffle flag is active.
        if "search_resource" in self.context and waffle.switch_is_active("collections-use-es-for-apps"):
            return self.field_to_native_es(obj, request)

        value = get_component(obj, self.source)

        # Filter apps based on feature profiles.
        profile = get_feature_profile(request)
        if profile and waffle.switch_is_active("buchets"):
            value = value.filter(**profile.to_kwargs(prefix="app___current_version__features__has_"))

        return [self.to_native(item) for item in value.all()]
Exemplo n.º 13
0
    def field_to_native(self, obj, field_name):
        if obj is None:
            return self.empty

        request = self.context['request']

        try:
            state = obj.user_states.get(user=request.user)
        except UserEntryState.DoesNotExist:
            return self.to_native(False)
        else:
            source = self.source or field_name
            value = state

            for component in source.split('.'):
                value = fields.get_component(state, component)
                if value is None:
                    break

            return self.to_native(value)
Exemplo n.º 14
0
    def field_to_native(self, obj, field_name):
        if self.many:
            # To-many relationship

            queryset = None
            if not self.source:
                # Prefer obj.serializable_value for performance reasons
                try:
                    queryset = obj.serializable_value(field_name)
                except AttributeError:
                    pass
            if queryset is None:
                # RelatedManager (reverse relationship)
                source = self.source or field_name
                queryset = obj
                for component in source.split('.'):
                    if queryset is None:
                        return []
                    queryset = get_component(queryset, component)

            # Forward relationship
            if is_simple_callable(getattr(queryset, 'all', None)):
                return [self.to_native(item.pk) for item in queryset.all()]
            else:
                # Also support non-queryset iterables.
                # This allows us to also support plain lists of related items.
                return [self.to_native(item.pk) for item in queryset]

        # To-one relationship
        try:
            # Prefer obj.serializable_value for performance reasons
            pk = obj.serializable_value(self.source or field_name)
        except AttributeError:
            # RelatedObject (reverse relationship)
            try:
                pk = getattr(obj, self.source or field_name).pk
            except (ObjectDoesNotExist, AttributeError):
                return None

        # Forward relationship
        return self.to_native(pk)
Exemplo n.º 15
0
    def field_to_native(self, obj, field_name):
        if self.many:
            # To-many relationship

            queryset = None
            if not self.source:
                # Prefer obj.serializable_value for performance reasons
                try:
                    queryset = obj.serializable_value(field_name)
                except AttributeError:
                    pass
            if queryset is None:
                # RelatedManager (reverse relationship)
                source = self.source or field_name
                queryset = obj
                for component in source.split('.'):
                    if queryset is None:
                        return []
                    queryset = get_component(queryset, component)

            # Forward relationship
            if is_simple_callable(getattr(queryset, 'all', None)):
                return [self.to_native(item.pk) for item in queryset.all()]
            else:
                # Also support non-queryset iterables.
                # This allows us to also support plain lists of related items.
                return [self.to_native(item.pk) for item in queryset]

        # To-one relationship
        try:
            # Prefer obj.serializable_value for performance reasons
            pk = obj.serializable_value(self.source or field_name)
        except AttributeError:
            # RelatedObject (reverse relationship)
            try:
                pk = getattr(obj, self.source or field_name).pk
            except (ObjectDoesNotExist, AttributeError):
                return None

        # Forward relationship
        return self.to_native(pk)
Exemplo n.º 16
0
    def field_to_native(self, obj, field_name):
        try:
            if self.source == '*':
                return self.to_native(obj)

            source = self.source or field_name
            value = obj

            for component in source.split('.'):
                value = get_component(value, component)
                if value is None:
                    break
        except ObjectDoesNotExist:
            return None

        if value is None:
            return None

        if self.many:
            return [self.to_native(item) for item in value.all()]
        return self.to_native(value)
Exemplo n.º 17
0
    def field_to_native(self, obj, field_name):
        try:
            if self.source == '*':
                return self.to_native(obj)

            source = self.source or field_name
            value = obj

            for component in source.split('.'):
                value = get_component(value, component)
                if value is None:
                    break
        except ObjectDoesNotExist:
            return None

        if value is None:
            return None

        if self.many:
            return [self.to_native(item) for item in value.all()]
        return self.to_native(value)
Exemplo n.º 18
0
    def field_to_native(self, obj, field_name):
        if not hasattr(self, 'context') or not 'request' in self.context:
            raise ImproperlyConfigured('Pass request in self.context when'
                                       ' using CollectionMembershipField.')

        request = self.context['request']

        # If we have a search resource in the context, we should try to use ES
        # to fetch the apps, if the waffle flag is active.
        if ('search_resource' in self.context
            and waffle.switch_is_active('collections-use-es-for-apps')):
            return self.field_to_native_es(obj, request)

        value = get_component(obj, self.source)

        # Filter apps based on feature profiles.
        profile = get_feature_profile(request)
        if profile:
            value = value.filter(**profile.to_kwargs(
                prefix='app___current_version__features__has_'))

        return [self.to_native(item) for item in value.all()]
    def field_to_native(self, obj, field_name):
        """
        Override default so that the serializer can be used as a nested field
        across relationships.
        """
        if self.write_only:
            return None

        if self.source == '*':
            return self.to_native(obj)

            # Get the raw field value
        try:
            source = self.source or field_name
            value = obj

            for component in source.split('.'):
                if value is None:
                    break
                value = get_component(value, component)
        except ObjectDoesNotExist:
            return None

        if is_simple_callable(getattr(value, 'all', None)):
            filtered_queryset = value.filter(self.get_related_filter())
            return [self.to_native(item) for item in filtered_queryset]

        if value is None:
            return None

        if self.many is not None:
            many = self.many
        else:
            many = hasattr(value, '__iter__') and not isinstance(value, (Page, dict, six.text_type))

        if many:
            return [self.to_native(item) for item in value]
        return self.to_native(value)
Exemplo n.º 20
0
    def field_to_native(self, obj, field_name):
        """
        Get a value for serialization from an object.

        This is mostly copy/paste from ``fields.Field``.

        :args obj: The object to reead the value from.
        :args field_name: The name of the field to read from obj.
        """
        if obj is None:
            return self.empty

        if self.read_source == '*':
            return self.to_native(obj)

        source = self.read_source or field_name
        value = obj

        for component in source.split('.'):
            value = get_component(value, component)
            if value is None:
                break

        return self.to_native(value)
Exemplo n.º 21
0
    def field_to_native(self, obj, field_name):
        """
        Get a value for serialization from an object.

        This is mostly copy/paste from ``fields.Field``.

        :args obj: The object to reead the value from.
        :args field_name: The name of the field to read from obj.
        """
        if obj is None:
            return self.empty

        if self.read_source == '*':
            return self.to_native(obj)

        source = self.read_source or field_name
        value = obj

        for component in source.split('.'):
            value = get_component(value, component)
            if value is None:
                break

        return self.to_native(value)