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
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)
(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'),
import django.conf
import django.forms
+import itertools
import re
import sys
import warnings
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))
@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
<?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">
--- /dev/null
+<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
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 = []
?>