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