dont use celery to index - seems it gets blocked on index file access
[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
47     def __call__(self, request):
48         if request.user.is_authenticated():
49             return HttpResponseRedirect('/')
50         return super(RegisterFormView, self).__call__(request)
51
52     def success(self, form, request):
53         user = form.save()
54         user = auth.authenticate(
55             username=form.cleaned_data['username'],
56             password=form.cleaned_data['password1']
57         )
58         auth.login(request, user)
59
60
61 @never_cache
62 def logout_then_redirect(request):
63     auth.logout(request)
64     return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
65
66
67 def clock(request):
68     """ Provides server time for jquery.countdown,
69     in a format suitable for Date.parse()
70     """
71     return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))
72
73
74 def publish_plan(request):
75     cache_key = "publish_plan"
76     plan = cache.get(cache_key)
77
78     if plan is None:
79         plan = []
80         try:
81             feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
82         except:
83             pass
84         else:
85             for i in range(len(feed['entries'])):
86                 print i
87                 plan.append({
88                     'title': feed['entries'][i].title,
89                     'link': feed['entries'][i].link,
90                     })
91         cache.set(cache_key, plan, 1800)
92
93     return render_to_response("publish_plan.html", {'plan': plan},
94         context_instance=RequestContext(request))