1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from datetime import date, datetime
5 from urllib.parse import quote_plus
7 from allauth.socialaccount.views import SignupView
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
21 from catalogue.models import Book, Collection, Tag, Fragment
23 from social.utils import get_or_choose_cite
24 from wolnelektury.forms import RegistrationForm, SocialSignupForm, WLAuthenticationForm
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)
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)
44 class WLLoginView(LoginView):
45 form_class = WLAuthenticationForm
48 class WLRegisterView(FormView):
49 form_class = RegistrationForm
50 template_name = 'registration/register.html'
52 def form_valid(self, form):
54 user = auth.authenticate(
55 username=form.cleaned_data['username'],
56 password=form.cleaned_data['password1']
58 auth.login(self.request, user)
59 return HttpResponseRedirect(quote_plus(self.request.GET.get('next', '/'), safe='/?='))
63 def logout_then_redirect(request):
65 return HttpResponseRedirect(quote_plus(request.GET.get('next', '/'), safe='/?='))
70 """ Provides server UTC time for jquery.countdown,
71 in a format suitable for Date.parse()
73 return HttpResponse(datetime.utcnow().strftime('%Y/%m/%d %H:%M:%S UTC'))
76 def publish_plan(request):
77 cache_key = "publish_plan"
78 plan = cache.get(cache_key)
83 feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
87 for i in range(len(feed['entries'])):
89 'title': feed['entries'][i].title,
90 'link': feed['entries'][i].link,
92 cache.set(cache_key, plan, 1800)
94 return render(request, "publish_plan.html", {'plan': plan})
98 def user_settings(request):
99 return render(request, "user.html")
103 return render(request, 'widget.html')
106 class SocialSignupView(SignupView):
107 form_class = SocialSignupForm