Beispiel #1
0
 def filter_by_tags(self, request, api_name, resource_name, tag_set):
     content_type = get_content_type_for_resource(resource_name)
     resource = get_resource_by_name(resource_name)
     model_class = get_resource_model(resource_name)
     tag_id_set = map(int, tag_set.split(";"))
     try:
         exclude = [int(one) for one in request.GET.getlist('exclude')]
     except Exception, e:
         logger.exception(e)
         exclude = []
Beispiel #2
0
    def resolve_resource_uri(self, uri):
        """
        Converts standard format resource URI into corresponding object.
        """
        pattern = r"/%s/(?P<resource_name>[^/]+)/(?P<pk>\d+)/$" % self._meta.api_name
        match = re.match(pattern, uri)
        if not match:
            raise NotFound("The URL provided '%s' was not a link to a valid resource." % uri)

        model_class = get_resource_model(match.group('resource_name'))
        return model_class.objects.get(pk=match.group('pk'))
    def filter_by_tags(self, request, api_name, resource_name, tag_set):
        resource = get_resource_by_name(resource_name)
        model_class = get_resource_model(resource_name)
        tag_id_set = map(int, tag_set.split(";"))

        # select objects sorted according to number of given tags that contain
        objects = PublishableTaggedItem.objects.filter(tag__id__in=tag_id_set)
        objects = objects.values("content_object").annotate(count=Count("content_object"))
        objects = objects.order_by("-count")[:resource._meta.limit]
        # extract primary keys of selected objects
        object_ids = map(lambda o: o["content_object"], objects)

        # sort bundles according to number of given tags that object contains
        objects = dict((o.id, o,) for o in model_class.objects.filter(pk__in=object_ids))
        bundles = [resource.build_bundle(obj=objects[id], request=request) for id in object_ids]
        bundles = [resource.full_dehydrate(bundle) for bundle in bundles]

        return resource.create_response(request, bundles)