1 from datetime import datetime
4 from django.contrib import auth
5 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
6 from django.core.cache import cache
7 from django.http import HttpResponse, HttpResponseRedirect
8 from django.shortcuts import render_to_response
9 from django.template import RequestContext
10 from django.utils.http import urlquote_plus
11 from django.utils.translation import ugettext_lazy as _
12 from django.views.decorators.cache import never_cache
14 from django.conf import settings
15 from ajaxable.utils import AjaxableFormView
16 from catalogue.models import Book
19 def main_page(request):
20 last_published = Book.objects.exclude(html_file='').order_by('-created_at')[:4]
22 return render_to_response("main_page.html", locals(),
23 context_instance=RequestContext(request))
26 class LoginFormView(AjaxableFormView):
27 form_class = AuthenticationForm
32 def __call__(self, request):
33 if request.user.is_authenticated():
34 return HttpResponseRedirect('/')
35 return super(LoginFormView, self).__call__(request)
37 def success(self, form, request):
38 auth.login(request, form.get_user())
41 class RegisterFormView(AjaxableFormView):
42 form_class = UserCreationForm
44 submit = _('Register')
47 def __call__(self, request):
48 if request.user.is_authenticated():
49 return HttpResponseRedirect('/')
50 return super(RegisterFormView, self).__call__(request)
52 def success(self, form, request):
54 user = auth.authenticate(
55 username=form.cleaned_data['username'],
56 password=form.cleaned_data['password1']
58 auth.login(request, user)
62 def logout_then_redirect(request):
64 return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
68 """ Provides server time for jquery.countdown,
69 in a format suitable for Date.parse()
71 return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))
74 def publish_plan(request):
75 cache_key = "publish_plan"
76 plan = cache.get(cache_key)
81 feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
85 for i in range(len(feed['entries'])):
88 'title': feed['entries'][i].title,
89 'link': feed['entries'][i].link,
91 cache.set(cache_key, plan, 1800)
93 return render_to_response("publish_plan.html", {'plan': plan},
94 context_instance=RequestContext(request))