Browse and My Reserves now sport a "timeframe" widget
authorgfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Thu, 14 Apr 2011 02:20:00 +0000 (02:20 +0000)
committergfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Thu, 14 Apr 2011 02:20:00 +0000 (02:20 +0000)
You can use it to specify whether you want to look at current courses, past
ones, or future ones. For past and future, there are options to show just
nearby past/future courses, or all past/future courses.

The setting is sticky to your login session.

git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@1357 6d9bc8c9-1ec2-4278-b937-99fde70a366f

conifer/syrup/models.py
conifer/syrup/urls.py
conifer/syrup/views/_common.py
conifer/syrup/views/general.py
conifer/syrup/views/sites.py
conifer/templates/browse_index.xhtml
conifer/templates/components/timeframe.xhtml [new file with mode: 0644]
conifer/templates/my_sites.xhtml

index 0c83aa5..403e00a 100644 (file)
@@ -5,7 +5,7 @@ from collections                     import defaultdict
 from conifer.libsystems              import marcxml as MX
 from conifer.plumbing.genshi_support import get_request
 from conifer.plumbing.hooksystem     import *
-from datetime                        import datetime, timedelta
+from datetime                        import datetime, timedelta, date
 from django.conf                     import settings
 from django.contrib.auth.models      import AnonymousUser, User
 from django.db                       import models as m
@@ -177,7 +177,31 @@ class Term(BaseModel):
     def midpoint(self):
         return self.start + (self.finish-self.start) / 2
 
-
+    @classmethod
+    def timeframe_query(cls, N=0, extent=30):
+        """
+        Returns three lists: a list of terms that recently ended, a list of
+        terms that are active, and a list of terms that are upcoming soon.
+        """
+        N = int(N)
+        today = date.today()
+        delta = timedelta(days=extent)
+        before = today - delta
+        after  = today + delta
+
+        if N == 0:              # active
+            return Q(start_term__start__lte=today, end_term__finish__gte=today)
+        elif N == -1:           # recently finished
+            return Q(end_term__finish__lt=today, end_term__finish__gte=before)
+        elif N == -2:           # all past courses
+            return Q(end_term__finish__lt=today)
+        elif N ==  1:           # starting soon
+            return Q(start_term__start__lte=after, start_term__start__gt=today)
+        elif N ==  2:           # all future courses
+            return Q(start_term__start__gt=today)
+        else:
+            raise Exception('unknown timeframe: %d' % N)
+        
 class Department(BaseModel):
     name   = m.CharField(max_length=256)
     active = m.BooleanField(default=True)
index ae8cad9..d402cd7 100644 (file)
@@ -11,6 +11,7 @@ urlpatterns = patterns('conifer.syrup.views',
     (r'^site/$', 'my_sites'),
     (r'^site/new/$', 'add_new_site'),
     (r'^site/invitation/$', 'site_invitation'),
+    (r'^timeframe/$', 'timeframe'),
     (r'^browse/$', 'browse'),
     (r'^browse/(?P<browse_option>.*)/$', 'browse'),
     (r'^prefs/$', 'user_prefs'),
index 6fa8430..7f26453 100644 (file)
@@ -17,6 +17,7 @@ g = TemplateSet(HERE('templates'), genshi_namespace)
 
 import django.conf
 import django.forms
+import itertools
 import re
 import sys
 import warnings
index a877567..925d11a 100644 (file)
@@ -47,35 +47,10 @@ def user_prefs(request):
         profile.save()
         return HttpResponseRedirect('../')
 
-def browse(request, browse_option=''):
-    #the defaults should be moved into a config file or something...
-    page_num = int(request.GET.get('page', 1))
-    count    = int(request.GET.get('count', 5))
-
-    if browse_option == '':
-        queryset = None
-        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':
-        # 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'
-
-    queryset = queryset and queryset.distinct()
-    paginator = Paginator(queryset, count)
-    return g.render(template, paginator=paginator,
-                    page_num=page_num,
-                    count=count)
-
+def timeframe(request):
+    tf = int(request.REQUEST.get('timeframe', '0'))
+    request.session['timeframe'] = tf
+    return HttpResponseRedirect(request.META.get('HTTP_REFERER', '../'))
 
 def instructor_detail(request, instructor_id):
     page_num = int(request.GET.get('page', 1))
index d16d375..9edc595 100644 (file)
@@ -171,7 +171,23 @@ def delete_site(request, site_id):
 
 @login_required
 def my_sites(request):
-    return g.render('my_sites.xhtml')
+    timeframe = request.session.get('timeframe', 0)
+    time_query = models.Term.timeframe_query(timeframe)
+    return g.render('my_sites.xhtml', **locals())
+
+
+def browse(request, browse_option=''):
+    #the defaults should be moved into a config file or something...
+    page_num = int(request.GET.get('page', 1))
+    count    = int(request.GET.get('count', 5))
+    timeframe = request.session.get('timeframe', 0)
+    time_query = models.Term.timeframe_query(timeframe)
+    queryset = None
+    template = 'browse_index.xhtml'
+    sites = list(models.Site.objects.order_by('course__department__name', 'course__code', 'owner__last_name').select_related().filter(time_query))
+    blocks = itertools.groupby(sites, lambda s: s.course.department)
+    return g.render('browse_index.xhtml', **locals())
+
 
 #-----------------------------------------------------------------------------
 # Site Invitation Code handler
index 3820921..c71fabc 100644 (file)
@@ -1,28 +1,31 @@
 <?python
 title = _('All Reserves, by Department')
-
-sites = models.Site.objects.order_by('course__department__name', 'course__code', 'owner__last_name').select_related()
-blocks = itertools.groupby(sites, lambda s: s.course.department)
 ?>
 <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:xi="http://www.w3.org/2001/XInclude"
       xmlns:py="http://genshi.edgewall.org/">
 <xi:include href="master.xhtml"/>
+<xi:include href="components/timeframe.xhtml"/>
 <head>
   <title>${title}</title>
 </head>
 <body>
   <h1>${title}</h1>
-  <div py:if="user.is_anonymous()">
+  ${timeframe_nav(timeframe)}
+  <p py:if="user.is_anonymous() and sites">
     (Note: some reserve materials may require you
     to <a href="${ROOT}${settings.LOGIN_URL}?next=${ROOT}/">log in</a>)
-  </div>
+  </p>
   
   <img py:def="lock(condition=True)" 
           py:if="condition"
           src="${ROOT}/static/tango/lock.png"
           alt="lock" title="This resource is access-controlled."/>
   
+  <p py:if="not sites">
+    There are no reserves materials within the selected timeframe.
+  </p>
+
   <div py:for="(dept, ss) in blocks">
        <h2>${dept}</h2>
        <div py:for="site in ss">
diff --git a/conifer/templates/components/timeframe.xhtml b/conifer/templates/components/timeframe.xhtml
new file mode 100644 (file)
index 0000000..c32c282
--- /dev/null
@@ -0,0 +1,24 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude"
+      py:strip="">
+
+  <form py:def="timeframe_nav(timeframe)"
+       action="${ROOT}/timeframe/" method="post" id="timeframe">
+  <select name="timeframe" style="font-size: 115%;">
+    <option py:def="opt(n,title,bold=False)" value="${n}" 
+           py:attrs="{'style':'font-weight:bold;' if bold else '', 'selected':'selected' if n==timeframe else None}">
+      ${title}
+    </option>
+    ${opt(-2, _('Past courses'))}
+    ${opt(-1, _('Recently finished'))}
+    ${opt( 0, _('Active courses'), True)}
+    ${opt( 1, _('Starting soon'))}
+    ${opt( 2, _('Future courses'))}
+  </select>
+  <script>
+  $(function() { $('#timeframe select').change(function () { $('#timeframe')[0].submit(); }); });
+  </script>
+  </form>
+
+</html>
\ No newline at end of file
index 0a1d3dd..727f79c 100644 (file)
@@ -5,14 +5,16 @@ title = _('My Reserves')
       xmlns:xi="http://www.w3.org/2001/XInclude"
       xmlns:py="http://genshi.edgewall.org/">
 <xi:include href="master.xhtml"/>
+<xi:include href="components/timeframe.xhtml"/>
 <head>
   <title>${title}</title>
 </head>
 <body>
   <h1>${title}</h1>
+  ${timeframe_nav(timeframe)}
   <?python
     if user.is_authenticated():
-        my_sites = user.sites()
+        my_sites = user.sites().filter(time_query)
     else:
         my_sites = []
   ?>