genre for pictures
[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 def main_page(request):
23     last_published = Book.objects.filter(parent=None).order_by('-created_at')[:4]
24     cite = choose_cite(RequestContext(request))
25
26     return render_to_response("main_page.html", locals(),
27         context_instance=RequestContext(request))
28
29
30 class LoginFormView(AjaxableFormView):
31     form_class = AuthenticationForm
32     template = "auth/login.html"
33     placeholdize = True
34     title = _('Sign in')
35     submit = _('Sign in')
36     ajax_redirect = True
37
38     def __call__(self, request):
39         if request.user.is_authenticated():
40             return self.redirect_or_refresh(request, '/',
41                 message=_('Already logged in as user %(user)s', ) %
42                             {'user': request.user.username})
43         return super(LoginFormView, self).__call__(request)
44
45     def success(self, form, request):
46         auth.login(request, form.get_user())
47
48
49 class RegisterFormView(AjaxableFormView):
50     form_class = UserCreationForm
51     template = "auth/register.html"
52     placeholdize = True
53     title = _('Register')
54     submit = _('Register')
55     ajax_redirect = True
56     form_prefix = 'register'
57     honeypot = True
58
59     def __call__(self, request):
60         if request.user.is_authenticated():
61             return self.redirect_or_refresh(request, '/',
62                 message=_('Already logged in as user %(user)s', ) %
63                             {'user': request.user.username})
64         return super(RegisterFormView, self).__call__(request)
65
66     def success(self, form, request):
67         form.save()
68         user = auth.authenticate(
69             username=form.cleaned_data['username'],
70             password=form.cleaned_data['password1']
71         )
72         auth.login(request, user)
73
74
75 class LoginRegisterFormView(LoginFormView):
76     template = 'auth/login_register.html'
77     title = _('You have to be logged in to continue')
78
79     def extra_context(self, request, obj):
80         return {
81             "register_form": placeholdized(UserCreationForm(prefix='register')),
82             "register_submit": _('Register'),
83         }
84
85
86 @never_cache
87 def logout_then_redirect(request):
88     auth.logout(request)
89     return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
90
91
92 @never_cache
93 def clock(request):
94     """ Provides server UTC time for jquery.countdown,
95     in a format suitable for Date.parse()
96     """
97     return HttpResponse(datetime.utcnow().strftime('%Y/%m/%d %H:%M:%S UTC'))
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))