a7b48c28773ae2d1e63847c71e1d1a3e383dc73e
[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.utils.translation import gettext_lazy as _
19 from django.views.decorators.cache import never_cache
20
21 from catalogue.models import Book, Collection, Tag, Fragment
22 import club.models
23 from social.utils import get_or_choose_cite
24 from wolnelektury.forms import RegistrationForm, SocialSignupForm, WLAuthenticationForm
25
26
27 @never_cache
28 def main_page(request):
29     if request.GET.get('w') in settings.WIDGETS:
30         request.EXPERIMENTS['layout'].override(True)
31     if request.EXPERIMENTS['sowka'].value:
32         request.EXPERIMENTS['layout'].override(True)
33
34     ctx = {}
35     ctx['last_published'] = Book.objects.exclude(cover_clean='').filter(findable=True, parent=None).order_by('-created_at')[:10]
36     ctx['recommended_collection'] = Collection.objects.filter(listed=True, role='recommend').order_by('?').first()
37     ctx['ambassadors'] = club.models.Ambassador.objects.all().order_by('?')
38     ctx['widget'] = settings.WIDGETS.get(request.GET.get('w'))
39     if not ctx['widget'] and request.EXPERIMENTS['sowka'].value:
40         ctx['widget'] = settings.WIDGETS['pan-sowka']
41     return render(request, '2022/main_page.html', ctx)
42
43
44 class WLLoginView(LoginView):
45     form_class = WLAuthenticationForm
46
47
48 class WLRegisterView(FormView):
49     form_class = RegistrationForm
50     template_name = 'registration/register.html'
51
52     def form_valid(self, form):
53         form.save()
54         user = auth.authenticate(
55             username=form.cleaned_data['username'],
56             password=form.cleaned_data['password1']
57         )
58         auth.login(self.request, user)
59         return HttpResponseRedirect(quote_plus(self.request.GET.get('next', '/'), safe='/?='))
60
61
62 @never_cache
63 def logout_then_redirect(request):
64     auth.logout(request)
65     return HttpResponseRedirect(quote_plus(request.GET.get('next', '/'), safe='/?='))
66
67
68 @never_cache
69 def clock(request):
70     """ Provides server UTC time for jquery.countdown,
71     in a format suitable for Date.parse()
72     """
73     return HttpResponse(datetime.utcnow().strftime('%Y/%m/%d %H:%M:%S UTC'))
74
75
76 def publish_plan(request):
77     cache_key = "publish_plan"
78     plan = cache.get(cache_key)
79
80     if plan is None:
81         plan = []
82         try:
83             feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
84         except:
85             pass
86         else:
87             for i in range(len(feed['entries'])):
88                 plan.append({
89                     'title': feed['entries'][i].title,
90                     'link': feed['entries'][i].link,
91                     })
92         cache.set(cache_key, plan, 1800)
93
94     return render(request, "publish_plan.html", {'plan': plan})
95
96
97 @login_required
98 def user_settings(request):
99     return render(request, "user.html")
100
101
102 def widget(request):
103     return render(request, 'widget.html')
104
105
106 class SocialSignupView(SignupView):
107     form_class = SocialSignupForm