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