[open-ils-commits] r920 - in servres/trunk/conifer/syrup: . views (gfawcett)

svn at svn.open-ils.org svn at svn.open-ils.org
Wed Jul 14 20:55:47 EDT 2010


Author: gfawcett
Date: 2010-07-14 20:55:46 -0400 (Wed, 14 Jul 2010)
New Revision: 920

Modified:
   servres/trunk/conifer/syrup/models.py
   servres/trunk/conifer/syrup/views/_common.py
   servres/trunk/conifer/syrup/views/general.py
Log:
got rid of 'user_filters', moving towards putting filter-clause generators in the model.

So, if you want a clause that would show only Items accessible by the current user:

  accessible = Item.objects.filter(Item.filter_for_user(current_user))

filter_for_user() returns a Q() object, which can be combined using
binary ops to construct more complex queries.

Modified: servres/trunk/conifer/syrup/models.py
===================================================================
--- servres/trunk/conifer/syrup/models.py	2010-07-15 00:55:37 UTC (rev 919)
+++ servres/trunk/conifer/syrup/models.py	2010-07-15 00:55:46 UTC (rev 920)
@@ -1,20 +1,22 @@
-from django.db import models as m
-from django.contrib.auth.models import User
-from django.contrib.auth.models import AnonymousUser
-from datetime import datetime
-from genshi import Markup
-from django.utils.translation import ugettext as _
 import re
 import random
-from django.utils import simplejson
+
+from django.db                       import models as m
+from django.db.models                import Q
+from django.contrib.auth.models      import User
+from django.contrib.auth.models      import AnonymousUser
+from datetime                        import datetime
+from genshi                          import Markup
+from django.utils.translation        import ugettext as _
+from django.utils                    import simplejson
 from conifer.plumbing.genshi_support import get_request
+
 # campus and library integration
-from conifer.plumbing.hooksystem import *
-from django.conf import settings
+from conifer.plumbing.hooksystem     import *
+from django.conf                     import settings
 campus = settings.CAMPUS_INTEGRATION
-# TODO: fixme, not sure if conifer.custom is a good parent.
 import conifer.libsystems.z3950.marcxml as MX
-from django.utils import simplejson as json
+from django.utils                    import simplejson as json
 
 #----------------------------------------------------------------------
 
@@ -178,6 +180,7 @@
 
     class Meta:
         unique_together = (('course', 'term', 'owner'))
+        ordering = ['-term__start', 'course__code']
 
     def __unicode__(self):
         return u'%s: %s (%s, %s)' % (
@@ -294,7 +297,24 @@
             or (self.access == 'LOGIN' and user.is_authenticated()) \
             or user.is_staff \
             or self.is_member(user)
-            
+
+
+    #--------------------------------------------------
+
+    @classmethod
+    def filter_for_user(cls, user):
+        """
+        Given a user object, return an appropriate Q filter that would
+        filter only Sites that the user has permission to view.
+        """
+        if user.is_anonymous():
+            return Q(access='ANON')
+        elif user.is_staff:
+            return Q()
+        else:
+            return (Q(access__in=('LOGIN','ANON')) \
+                        | Q(group__membership__user=user))
+
 #------------------------------------------------------------
 # User membership in sites
 
@@ -468,6 +488,9 @@
     fileobj_mimetype = m.CharField(max_length=128, blank=True, null=True)
 
 
+    class Meta:
+        ordering = ['title', 'author', 'published']
+
     #--------------------------------------------------
     # MARC
     def marc_as_dict(self):
@@ -553,6 +576,21 @@
         return hl_author
 
 
+    @classmethod
+    def filter_for_user(cls, user):
+        """
+        Given a user object, return an appropriate Q filter that would
+        filter only Items that the user has permission to view.
+        """
+        if user.is_anonymous():
+            return Q(site__access='ANON')
+        elif user.is_staff:
+            return Q()
+        else:
+            return (Q(site__access__in=('LOGIN','ANON')) \
+                        | Q(site__group__membership__user=user))
+                     
+       
 #------------------------------------------------------------
 # TODO: move this to a utility module.
 

Modified: servres/trunk/conifer/syrup/views/_common.py
===================================================================
--- servres/trunk/conifer/syrup/views/_common.py	2010-07-15 00:55:37 UTC (rev 919)
+++ servres/trunk/conifer/syrup/views/_common.py	2010-07-15 00:55:46 UTC (rev 920)
@@ -125,31 +125,7 @@
                           _('The page you requested could not be found'))
     return HttpResponse(msg._container, status=404)
 
-#-----------------------------------------------------------
 
-def user_filters(user):
-    """Returns a dict of filters for Item, Site, etc. querysets,
-    based on the given user's permissions."""
-    # TODO, figure out a way of EXPLAIN'ing these queries! I have no
-    # idea of their complexity.
-    if user.is_anonymous():
-        # then only anonymous-access sites are available.
-        filters = {'items': Q(site__access='ANON'),
-                   'sites': Q(access='ANON'),
-                   'instructors': Q(), # TODO: do we really need a filter here?
-                   }
-    else:
-        # logged-in users have access to sites which are of the
-        # LOGIN class ('all logged-in users') or in which they
-        # have explicit Member-ship.
-        filters = {
-            'items': (Q(site__access__in=('LOGIN','ANON')) \
-                          | Q(site__group__membership__user=user)),
-            'sites': (Q(access__in=('LOGIN','ANON')) | Q(group__membership__user=user)),
-            'instructors': Q(), # TODO: do we really need a filter here?
-            }
-    return filters
-
 #------------------------------------------------------------
 
 # decorator

Modified: servres/trunk/conifer/syrup/views/general.py
===================================================================
--- servres/trunk/conifer/syrup/views/general.py	2010-07-15 00:55:37 UTC (rev 919)
+++ servres/trunk/conifer/syrup/views/general.py	2010-07-15 00:55:46 UTC (rev 920)
@@ -96,13 +96,15 @@
         template = 'browse_index.xhtml'
     elif browse_option == 'instructors':
         queryset = models.User.active_instructors()
+        # TODO: fixme, user_filters is no more.
         queryset = queryset.filter(user_filters(request.user)['instructors'])
         template = 'instructors.xhtml'
     elif browse_option == 'departments':
         queryset = models.Department.objects.filter(active=True)
         template = 'departments.xhtml'
     elif browse_option == 'courses':
-        # fixme, course filter should not be (active=True) but based on user identity.
+        # TODO: fixme, course filter should not be (active=True) but based on user identity.
+        # TODO: fixme, user_filters is no more.
         for_courses = user_filters(request.user)['courses']
         queryset = models.Site.objects.filter(for_courses)
         template = 'courses.xhtml'
@@ -126,6 +128,7 @@
     '''
     sites = models.Site.objects.filter(member__user=instructor_id,
                                            member__role='INSTR')
+    # TODO: fixme, user_filters is no more.
     filters = user_filters(request.user)
     courses = courses.filter(filters['courses'])
     paginator = Paginator(courses.order_by('title'), count)



More information about the open-ils-commits mailing list