merge search into pretty branch
[wolnelektury.git] / wolnelektury / views.py
1 from datetime import datetime
2
3 from django.contrib import auth
4 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
5 from django.http import HttpResponse, HttpResponseRedirect
6 from django.shortcuts import render_to_response
7 from django.template import RequestContext
8 from django.utils.http import urlquote_plus
9 from django.utils.translation import ugettext_lazy as _
10 from django.views.decorators import cache
11
12 from ajaxable.utils import AjaxableFormView
13 from catalogue.models import Book
14
15
16 def main_page(request):
17     last_published = Book.objects.exclude(html_file='').order_by('-created_at')[:4]
18
19     return render_to_response("main_page.html", locals(),
20         context_instance=RequestContext(request))
21
22
23 class LoginFormView(AjaxableFormView):
24     form_class = AuthenticationForm
25     #template = "auth/login.html"
26     title = _('Sign in')
27     submit = _('Sign in')
28
29     def __call__(self, request):
30         if request.user.is_authenticated():
31             return HttpResponseRedirect('/')
32         return super(LoginFormView, self).__call__(request)
33
34     def success(self, form, request):
35         auth.login(request, form.get_user())
36
37
38 class RegisterFormView(AjaxableFormView):
39     form_class = UserCreationForm
40     #template = "auth/register.html"
41     title = _('Register')
42     submit = _('Register')
43
44     def __call__(self, request):
45         if request.user.is_authenticated():
46             return HttpResponseRedirect('/')
47         return super(RegisterFormView, self).__call__(request)
48
49     def success(self, form, request):
50         user = form.save()
51         user = auth.authenticate(
52             username=form.cleaned_data['username'],
53             password=form.cleaned_data['password1']
54         )
55         auth.login(request, user)
56
57
58 @cache.never_cache
59 def logout_then_redirect(request):
60     auth.logout(request)
61     return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
62
63
64 def clock(request):
65     """ Provides server time for jquery.countdown,
66     in a format suitable for Date.parse()
67     """
68     return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))