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