Commits

Chris Beaven  committed 701c594

Date ranges

  • Participants
  • Parent commits fd61fbb

Comments (0)

Files changed (7)

 - Add ``AuthForm`` which can be used to allow previewing from authenticated
   users (via ``django.contrib.auth``).
 
+- Allow locking up until or only after certain dates.
+
 0.1.1 (2009.11.24)
 ------------------
 
 ----------------
 
 - Initial release.
-
 ===============
 
 A simple Django reusable application for locking down an entire site
-(or particular views), with customizable preview authorization
-(defaults to single password).
+(or particular views), with customizable date ranges and preview authorization.
 
 Installation
 ============
   referenced by ``settings.LOCKDOWN_FORM``. Note that this must be an actual
   form class, not a module reference like the setting. 
 
+``until_date``
+  The date to use rather than the date provided by ``settings.LOCKDOWN_UNTIL``.
+
+``after_date``
+  The date to use rather than the date provided by ``settings.LOCKDOWN_AFTER``.
+
 ``logout_key``
   A preview logout key to use, rather than the one provided by
   ``settings.LOCKDOWN_LOGOUT_KEY``.
         r'\.json$',   # unlock JSON API
     )
 
+LOCKDOWN_UNTIL
+--------------
+
+Used to lock the site down up until a certain date. Use a ``datetime.datetime``
+object.
+
+If neither ``LOCKDOWN_UNTIL`` or ``LOCKDOWN_AFTER`` is provided (i.e. the
+default), the site or views will always be locked.
+
+LOCKDOWN_AFTER
+--------------
+
+Used to lock the site down after a certain date. Use a ``datetime.datetime``
+object.
+
+See also: `LOCKDOWN_UNTIL`_.
+
 LOCKDOWN_LOGOUT_KEY
 -------------------
 
 
 * Once Django 1.2 ships with signed cookies (hopefully), replace
   contrib.sessions dependency with a signed cookie.
-
-* Allow locking up until or only after certain dates (like django-lock).

File lockdown/middleware.py

+import datetime
 import re
 
 from django.http import HttpResponseRedirect
 
 class LockdownMiddleware(object):
 
-    def __init__(self, form=None, logout_key=None, session_key=None,
-                 url_exceptions=None, **form_kwargs):
+    def __init__(self, form=None, until_date=None, after_date=None,
+                 logout_key=None, session_key=None, url_exceptions=None,
+                 **form_kwargs):
         if logout_key is None:
             logout_key = settings.LOGOUT_KEY
         if session_key is None:
             session_key = settings.SESSION_KEY
         self.form = form
         self.form_kwargs = form_kwargs
+        self.until_date = until_date
+        self.after_date = after_date
         self.logout_key = logout_key
         self.session_key = session_key
         self.url_exceptions = url_exceptions
             if pattern.search(request.path):
                 return None
 
+        # Don't lock down if outside of the lockdown dates.
+        if self.until_date is None:
+            until_date = settings.UNTIL_DATE
+        else:
+            until_date = self.until_date
+        if self.after_date is None:
+            after_date = settings.AFTER_DATE
+        else:
+            after_date = self.after_date
+        if until_date or after_date:
+            locked_date = False
+            if until_date and datetime.datetime.now() < until_date:
+                locked_date = True
+            if after_date and datetime.datetime.now() > after_date:
+                locked_date = True
+            if not locked_date:
+                return None
+
         form_data = request.method == 'POST' and request.POST or None
         if self.form is None:
             form_class = _default_form
             session[self.session_key] = token
             return self.redirect(request)
 
-        page_data = {}
+        page_data = {'until_date': until_date, 'after_date': after_date}
         if not hasattr(form, 'show_form') or form.show_form():
             page_data['form'] = form
 

File lockdown/settings.py

 FORM = getattr(settings, 'LOCKDOWN_FORM', 'lockdown.forms.LockdownForm')
 SESSION_KEY = getattr(settings, 'LOCKDOWN_SESSION_KEY', 'lockdown-allow')
 LOGOUT_KEY = getattr(settings, 'LOCKDOWN_LOGOUT_KEY', 'preview-logout')
+UNTIL_DATE = getattr(settings, 'LOCKDOWN_UNTIL', None)
+AFTER_DATE = getattr(settings, 'LOCKDOWN_AFTER', None)
 
 if not isinstance(PASSWORDS, (tuple, list)):
     PASSWORDS = PASSWORDS and (PASSWORDS,) or ()

File lockdown/tests/tests.py

+import datetime
 import os
 
 from django.conf import settings as django_settings, global_settings
             middleware._default_form = middleware.get_lockdown_form(
                                                                 settings.FORM)
 
+    def test_locked_until(self):
+        _old_until_date = settings.UNTIL_DATE
+        yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
+        tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
+
+        try:
+            settings.UNTIL_DATE = tomorrow
+            response = self.client.get(self.locked_url)
+            self.assertTemplateUsed(response, 'lockdown/form.html')
+
+            settings.UNTIL_DATE = yesterday
+            response = self.client.get(self.locked_url)
+            self.assertContains(response, self.locked_contents)
+        finally:
+            settings.UNTIL_DATE = _old_until_date
+
+    def test_locked_after(self):
+        _old_after_date = settings.AFTER_DATE
+        yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
+        tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
+
+        try:
+            settings.AFTER_DATE = yesterday
+            response = self.client.get(self.locked_url)
+            self.assertTemplateUsed(response, 'lockdown/form.html')
+
+            settings.AFTER_DATE = tomorrow
+            response = self.client.get(self.locked_url)
+            self.assertContains(response, self.locked_contents)
+        finally:
+            settings.AFTER_DATE = _old_after_date
+
+    def test_locked_until_and_after(self):
+        _old_until_date = settings.UNTIL_DATE
+        _old_after_date = settings.AFTER_DATE
+        yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
+        tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
+
+        try:
+            settings.UNTIL_DATE = yesterday
+            settings.AFTER_DATE = yesterday
+            response = self.client.get(self.locked_url)
+            self.assertTemplateUsed(response, 'lockdown/form.html')
+
+            settings.UNTIL_DATE = tomorrow
+            settings.AFTER_DATE = tomorrow
+            response = self.client.get(self.locked_url)
+            self.assertTemplateUsed(response, 'lockdown/form.html')
+
+            settings.UNTIL_DATE = yesterday
+            settings.AFTER_DATE = tomorrow
+            response = self.client.get(self.locked_url)
+            self.assertContains(response, self.locked_contents)
+        finally:
+            settings.UNTIL_DATE = _old_until_date
+            settings.AFTER_DATE = _old_after_date
+
 
 class DecoratorTests(BaseTests):
     locked_url = '/locked/view/'
 except IOError:
     hgrev = ''
 
-long_description = (open('README.txt').read() +
-                    open('CHANGES.txt').read() +
-                    open('TODO.txt').read())
+long_description = '\n'.join([open('README.txt').read(),
+                              open('CHANGES.txt').read(),
+                              open('TODO.txt').read()])
 setup(
     name='django-lockdown',
     version='0.1.1.post%s' % hgrev,