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