cover colors
[wolnelektury.git] / wolnelektury / views.py
1 from datetime import datetime
2 import feedparser
3
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
13
14 from django.conf import settings
15 from ajaxable.utils import AjaxableFormView
16 from catalogue.models import Book
17 from ajaxable.utils import placeholdized
18
19
20 @never_cache
21 def main_page(request):
22     last_published = Book.objects.filter(parent=None).order_by('-created_at')[:4]
23
24     return render_to_response("main_page.html", locals(),
25         context_instance=RequestContext(request))
26
27
28 class LoginFormView(AjaxableFormView):
29     form_class = AuthenticationForm
30     template = "auth/login.html"
31     placeholdize = True
32     title = _('Sign in')
33     submit = _('Sign in')
34     ajax_redirect = True
35
36     def __call__(self, request):
37         if request.user.is_authenticated():
38             return HttpResponseRedirect('/')
39         return super(LoginFormView, self).__call__(request)
40
41     def success(self, form, request):
42         auth.login(request, form.get_user())
43
44
45 class RegisterFormView(AjaxableFormView):
46     form_class = UserCreationForm
47     placeholdize = True
48     title = _('Register')
49     submit = _('Register')
50     ajax_redirect = True
51     form_prefix = 'register'
52
53     def __call__(self, request):
54         if request.user.is_authenticated():
55             return HttpResponseRedirect('/')
56         return super(RegisterFormView, self).__call__(request)
57
58     def success(self, form, request):
59         form.save()
60         user = auth.authenticate(
61             username=form.cleaned_data['username'],
62             password=form.cleaned_data['password1']
63         )
64         auth.login(request, user)
65
66
67 class LoginRegisterFormView(LoginFormView):
68     template = 'auth/login_register.html'
69     title = _('You have to be logged in to continue')
70
71     def extra_context(self, request, obj):
72         return {
73             "register_form": placeholdized(UserCreationForm(prefix='register')),
74             "register_submit": _('Register'),
75         }
76
77
78 @never_cache
79 def logout_then_redirect(request):
80     auth.logout(request)
81     return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
82
83
84 def clock(request):
85     """ Provides server time for jquery.countdown,
86     in a format suitable for Date.parse()
87     """
88     return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))
89
90
91 def publish_plan(request):
92     cache_key = "publish_plan"
93     plan = cache.get(cache_key)
94
95     if plan is None:
96         plan = []
97         try:
98             feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
99         except:
100             pass
101         else:
102             for i in range(len(feed['entries'])):
103                 plan.append({
104                     'title': feed['entries'][i].title,
105                     'link': feed['entries'][i].link,
106                     })
107         cache.set(cache_key, plan, 1800)
108
109     return render_to_response("publish_plan.html", {'plan': plan},
110         context_instance=RequestContext(request))