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