def hydrate_collab_id(self, bundle):
     if ("code" in bundle.data  # ignore this for update status
           and not bundle.data.get("collab_id", None)):
         # this just checks collab_id isn't blank
         # should maybe check that collab actually exists
         raise fields.ApiFieldError("The `collab_id` field must not be empty")
     return bundle
예제 #2
0
 def hydrate_hardware_platform(self, bundle):
     if ("code" in bundle.data  # ignore this for update status
             and not bundle.data.get("hardware_platform", None)):
         # this checks hardware_platform isn't blank
         raise fields.ApiFieldError(
             "The `hardware_platform` field must not be empty")
     return bundle
예제 #3
0
 def hydrate_premium_type(self, bundle):
     typ = amo.ADDON_PREMIUM_API_LOOKUP.get(bundle.data['premium_type'],
                                            None)
     if typ is None:
         raise fields.ApiFieldError(
             "premium_type should be one of 'free', 'premium', 'free-inapp'"
             ", 'premium-inapp', or 'other'.")
     bundle.obj.premium_type = typ
예제 #4
0
    def update_premium_type(self, bundle):
        self.hydrate_premium_type(bundle)
        if bundle.obj.premium_type in (amo.ADDON_FREE, amo.ADDON_FREE_INAPP):
            return

        ap = AddonPremium.objects.safer_get_or_create(addon=bundle.obj)[0]
        if not bundle.data.get('price') or not Price.objects.filter(
                price=bundle.data['price']).exists():
            tiers = ', '.join('"%s"' % p.price
                              for p in Price.objects.exclude(price="0.00"))
            raise fields.ApiFieldError(
                'Premium app specified without a valid price. Price can be'
                ' one of %s.' % (tiers,))
        else:
            ap.price = Price.objects.get(price=bundle.data['price'])
            ap.save()
    def dispatch_nested(self, request, **kwargs):
        """
        Dispatch a request to the nested resource.
        """
        # We don't check for is_authorized here since it will be
        # parent_cached_obj_get which will check that we have permissions
        # over the parent.
        self.is_authenticated(request)
        self.throttle_check(request)

        nested_name = kwargs.pop('nested_name')
        nested_field = self._nested[nested_name]

        try:
            obj = self.parent_cached_obj_get(
                request=request, **self.remove_api_resource_names(kwargs))
        except ObjectDoesNotExist:
            return http.HttpNotFound()
        except MultipleObjectsReturned:
            return http.HttpMultipleChoices("More than one parent resource is "
                                            "found at this URI.")

        # The nested resource needs to get the api_name from its parent because
        # it is possible that the resource being used as nested is not
        # registered in the API (ie. it can only be used as nested)
        nested_resource = nested_field.to_class()
        nested_resource._meta.api_name = self._meta.api_name

        # TODO: comment further to make sense of this block
        manager = None
        try:
            if isinstance(nested_field.attribute, basestring):
                name = nested_field.attribute
                manager = getattr(obj, name, None)
            elif callable(nested_field.attribute):
                manager = nested_field.attribute(obj)
            else:
                raise fields.ApiFieldError(
                    "The model '%r' has an empty attribute '%s' \
                    and doesn't allow a null value." %
                    (obj, nested_field.attribute))
        except ObjectDoesNotExist:
            pass

        kwargs['nested_name'] = nested_name
        kwargs['parent_resource'] = self
        kwargs['parent_object'] = obj

        if manager is None or not hasattr(manager, 'all'):
            dispatch_type = 'detail'
            kwargs['child_object'] = manager
        else:
            dispatch_type = 'list'
            kwargs['related_manager'] = manager
            # 'pk' will refer to the parent, so we remove it.
            if 'pk' in kwargs:
                del kwargs['pk']
            # Update with the related manager's filters, which will link to
            # the parent.
            kwargs.update(manager.core_filters)

        return nested_resource.dispatch(dispatch_type, request, **kwargs)