1 from datetime import datetime
4 from django.contrib import auth
5 from django.contrib.auth.decorators import login_required
6 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
7 from django.core.cache import cache
8 from django.http import HttpResponse, HttpResponseRedirect
9 from django.shortcuts import render_to_response
10 from django.template import RequestContext
11 from django.utils.http import urlquote_plus
12 from django.utils.translation import ugettext_lazy as _
13 from django.views.decorators.cache import never_cache
15 from django.conf import settings
16 from ajaxable.utils import AjaxableFormView
17 from catalogue.models import Book
18 from ajaxable.utils import placeholdized
22 def main_page(request):
23 last_published = Book.objects.filter(parent=None).order_by('-created_at')[:4]
25 return render_to_response("main_page.html", locals(),
26 context_instance=RequestContext(request))
29 class LoginFormView(AjaxableFormView):
30 form_class = AuthenticationForm
31 template = "auth/login.html"
37 def __call__(self, request):
38 if request.user.is_authenticated():
39 return self.redirect_or_refresh(request, '/',
40 message=_('Already logged in as user %(user)s', ) %
41 {'user': request.user.username})
42 return super(LoginFormView, self).__call__(request)
44 def success(self, form, request):
45 auth.login(request, form.get_user())
48 class RegisterFormView(AjaxableFormView):
49 form_class = UserCreationForm
52 submit = _('Register')
54 form_prefix = 'register'
56 def __call__(self, request):
57 if request.user.is_authenticated():
58 return self.redirect_or_refresh(request, '/',
59 message=_('Already logged in as user %(user)s', ) %
60 {'user': request.user.username})
61 return super(RegisterFormView, self).__call__(request)
63 def success(self, form, request):
65 user = auth.authenticate(
66 username=form.cleaned_data['username'],
67 password=form.cleaned_data['password1']
69 auth.login(request, user)
72 class LoginRegisterFormView(LoginFormView):
73 template = 'auth/login_register.html'
74 title = _('You have to be logged in to continue')
76 def extra_context(self, request, obj):
78 "register_form": placeholdized(UserCreationForm(prefix='register')),
79 "register_submit": _('Register'),
84 def logout_then_redirect(request):
86 return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
90 """ Provides server time for jquery.countdown,
91 in a format suitable for Date.parse()
93 return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))
96 def publish_plan(request):
97 cache_key = "publish_plan"
98 plan = cache.get(cache_key)
103 feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
107 for i in range(len(feed['entries'])):
109 'title': feed['entries'][i].title,
110 'link': feed['entries'][i].link,
112 cache.set(cache_key, plan, 1800)
114 return render_to_response("publish_plan.html", {'plan': plan},
115 context_instance=RequestContext(request))
119 def user_settings(request):
120 return render_to_response("user.html",
121 context_instance=RequestContext(request))