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