c5fbe125db5d25ccc7ccaef017ba51c9af569a3f
[wolnelektury.git] / src / wolnelektury / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from datetime import date, datetime
5 from urllib.parse import quote_plus
6 import feedparser
7 from allauth.socialaccount.views import SignupView
8
9 from django.conf import settings
10 from django.contrib import auth
11 from django.contrib.auth.decorators import login_required
12 from django.contrib.auth.forms import AuthenticationForm
13 from django.contrib.auth.views import LoginView
14 from django.core.cache import cache
15 from django.views.generic import FormView
16 from django.http import HttpResponse, HttpResponseRedirect
17 from django.shortcuts import render
18 from django.views.decorators.cache import never_cache
19
20 from catalogue.models import Book, Collection, Tag, Fragment
21 import club.models
22 from social.utils import get_or_choose_cite
23 from wolnelektury.forms import RegistrationForm, SocialSignupForm, WLAuthenticationForm
24
25
26 @never_cache
27 def main_page(request):
28     if request.GET.get('w') in settings.WIDGETS:
29         request.EXPERIMENTS['layout'].override(True)
30     if request.EXPERIMENTS['sowka'].value:
31         request.EXPERIMENTS['layout'].override(True)
32
33     ctx = {}
34     ctx['last_published'] = Book.objects.exclude(cover_clean='').filter(findable=True, parent=None).order_by('-created_at')[:10]
35     ctx['recommended_collection'] = Collection.objects.filter(listed=True, role='recommend').order_by('?').first()
36     ctx['ambassadors'] = club.models.Ambassador.objects.all().order_by('?')
37     ctx['widget'] = settings.WIDGETS.get(request.GET.get('w'))
38     if not ctx['widget'] and request.EXPERIMENTS['sowka'].value:
39         ctx['widget'] = settings.WIDGETS['pan-sowka']
40     return render(request, 'main_page.html', ctx)
41
42
43 class WLLoginView(LoginView):
44     form_class = WLAuthenticationForm
45
46
47 class WLRegisterView(FormView):
48     form_class = RegistrationForm
49     template_name = 'registration/register.html'
50
51     def form_valid(self, form):
52         form.save()
53         user = auth.authenticate(
54             username=form.cleaned_data['username'],
55             password=form.cleaned_data['password1']
56         )
57         auth.login(self.request, user)
58         return HttpResponseRedirect(quote_plus(self.request.GET.get('next', '/'), safe='/?='))
59
60
61 @never_cache
62 def logout_then_redirect(request):
63     auth.logout(request)
64     return HttpResponseRedirect(quote_plus(request.GET.get('next', '/'), safe='/?='))
65
66
67 @never_cache
68 def clock(request):
69     """ Provides server UTC time for jquery.countdown,
70     in a format suitable for Date.parse()
71     """
72     return HttpResponse(datetime.utcnow().strftime('%Y/%m/%d %H:%M:%S UTC'))
73
74
75 def publish_plan(request):
76     cache_key = "publish_plan"
77     plan = cache.get(cache_key)
78
79     if plan is None:
80         plan = []
81         try:
82             feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
83         except:
84             pass
85         else:
86             for i in range(len(feed['entries'])):
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(request, "publish_plan.html", {'plan': plan})
94
95
96 @login_required
97 def user_settings(request):
98     return render(request, "user.html")
99
100
101 def widget(request):
102     return render(request, 'widget.html')
103
104
105 class SocialSignupView(SignupView):
106     form_class = SocialSignupForm