make tags parameter work for content rest api
authorymh <ymh.work@gmail.com>
Fri, 20 Jan 2017 10:26:19 +0100
changeset 1479 536250030744
parent 1478 26db87e1b013
child 1480 31da4223b620
make tags parameter work for content rest api
.hgignore
src/ldt/ldt/api/ldt/resources/content.py
src/ldt/ldt/ldt_utils/models.py
--- a/.hgignore	Tue Mar 15 12:33:15 2016 +0100
+++ b/.hgignore	Fri Jan 20 10:26:19 2017 +0100
@@ -15,3 +15,4 @@
 ^\.settings/org\.eclipse\.core\.resources\.prefs$
 \.DS_Store$
 ^sbin/build/mdplayer_path$
+^src/ldt/.vscode
--- a/src/ldt/ldt/api/ldt/resources/content.py	Tue Mar 15 12:33:15 2016 +0100
+++ b/src/ldt/ldt/api/ldt/resources/content.py	Fri Jan 20 10:26:19 2017 +0100
@@ -33,6 +33,40 @@
         else:
             return bundle_or_obj.videopath + bundle_or_obj.stream_src
 
+"""
+Content Resource definition.
+The following urls are defined:
+    - <api_base>/contents/ : get a list of content
+        Method: GET
+        Returns: in json returns an object:
+            {
+                meta: {
+                    limit: cf. limit parameter
+                    offset: cf. offset parameter
+                    next: next page url or '''null'''
+                    previous: previous page url ir '''null'''
+                    total_count: nb of total results
+                },
+                objects: [
+                    <list of content objects>
+                ]
+
+            }
+        Parameters:
+        - format: compulsory, onlt "json" is defined
+        - limit: Max number of results (20 by default)
+        - offset: nb of object to skip (0 by default)
+        - tags: the tags to filter for. This must be the exact value(s).
+            tags=tag1,tag2,tag3 will do an "OR", i.e. here will find the content tagged with either tag1, tag2 or tag3
+            tags=tag1&tags=tag2 vill do an "AND", i.e. will find the contents tagged with tag1 and tag2.
+        - title: filter by title cf http://django-tastypie.readthedocs.io/en/latest/resources.html#basic-filtering for the syntax
+        - order_by: order the results. Possible values are [-]title, [-]creation_date, [-content_creation_date].
+            c.f. : http://django-tastypie.readthedocs.io/en/latest/resources.html#ordering for details
+    - <api_base>/content/<iri_id>/ : get the detail of a content
+        Method: GET
+        Parameters:
+        - format: compulsory, only "json" is defined
+"""
 class ContentResource(ModelResource):
 
     front_project = fields.ForeignKey('ldt.api.ldt.resources.ProjectResource','front_project', null=True, full=False)
@@ -123,3 +157,39 @@
         protect_models()
 
         return self.create_response(request, data)
+
+    def build_filters(self, filters=None):
+        if filters is None:
+            filters = {}
+
+        orm_filters = super(ContentResource, self).build_filters(filters)
+
+        if 'tags' in filters and filters.getlist('tags'):
+            tags_filter_list = orm_filters.setdefault('tags', [])
+            for tags_filter in filters.getlist('tags'):
+                tags_filter_list.append({'tags__name__in': tags_filter.split(",")})
+
+        logger.debug("build_filters filters %r, orm_filters %r", filters['tags'], orm_filters)
+
+        return orm_filters
+
+    def apply_filters(self, request, applicable_filters):
+        """
+        An ORM-specific implementation of ``apply_filters``.
+        The default simply applies the ``applicable_filters`` as ``**kwargs``,
+        but should make it possible to do more advanced things.
+        """
+        logger.debug("apply_filters applicable_filters %r", applicable_filters)
+
+        tags_filter_list = applicable_filters.pop('tags', [])
+        qs = self.get_object_list(request).filter(**applicable_filters)
+
+        if tags_filter_list and len(tags_filter_list) == 1:
+            qs = qs.filter(**tags_filter_list[0])
+        elif tags_filter_list and len(tags_filter_list) > 1:
+            for tag_filter in tags_filter_list:
+                qs = qs.filter(id__in=self.get_object_list(request).filter(**tag_filter))
+
+        return qs
+
+
--- a/src/ldt/ldt/ldt_utils/models.py	Tue Mar 15 12:33:15 2016 +0100
+++ b/src/ldt/ldt/ldt_utils/models.py	Fri Jan 20 10:26:19 2017 +0100
@@ -30,7 +30,7 @@
 from .events import post_project_save
 
 
-logger = logging.getLogger(__name__);
+logger = logging.getLogger(__name__)
 
 class Author(SafeModel):