1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. 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.views.decorators.cache import never_cache
20 from catalogue.models import Book, Collection, Tag, Fragment
22 from social.utils import get_or_choose_cite
23 from wolnelektury.forms import RegistrationForm, SocialSignupForm, WLAuthenticationForm
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)
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)
43 class WLLoginView(LoginView):
44 form_class = WLAuthenticationForm
47 class WLRegisterView(FormView):
48 form_class = RegistrationForm
49 template_name = 'registration/register.html'
51 def form_valid(self, form):
53 user = auth.authenticate(
54 username=form.cleaned_data['username'],
55 password=form.cleaned_data['password1']
57 auth.login(self.request, user)
58 return HttpResponseRedirect(quote_plus(self.request.GET.get('next', '/'), safe='/?='))
62 def logout_then_redirect(request):
64 return HttpResponseRedirect(quote_plus(request.GET.get('next', '/'), safe='/?='))
69 """ Provides server UTC time for jquery.countdown,
70 in a format suitable for Date.parse()
72 return HttpResponse(datetime.utcnow().strftime('%Y/%m/%d %H:%M:%S UTC'))
75 def publish_plan(request):
76 cache_key = "publish_plan"
77 plan = cache.get(cache_key)
82 feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
86 for i in range(len(feed['entries'])):
88 'title': feed['entries'][i].title,
89 'link': feed['entries'][i].link,
91 cache.set(cache_key, plan, 1800)
93 return render(request, "publish_plan.html", {'plan': plan})
97 def user_settings(request):
98 return render(request, "user.html")
102 return render(request, 'widget.html')
105 class SocialSignupView(SignupView):
106 form_class = SocialSignupForm