better ordering for tags
[redakcja.git] / redakcja / views.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 from django.shortcuts import render, redirect
7 from django.contrib.auth import authenticate, login
8 from django.contrib.auth.models import User
9 from django.core.mail import send_mail
10 from honeypot.decorators import check_honeypot
11
12 from .forms import RegistrationForm
13 from catalogue.models import Document
14 from organizations.models import Organization
15
16
17 def main(request):
18     upcoming = Document.objects.filter(deleted=False).filter(publish_log=None)
19     finished = Document.objects.filter(deleted=False).exclude(publish_log=None)
20     organizations = Organization.objects.all()
21     more_upcoming = upcoming.count() > 8
22     more_finished = finished.count() > 8
23     more_organizations = organizations.count() > 8
24     upcoming = upcoming[:8]
25     finished = finished[:8]
26     organizations = organizations[:8]
27
28     return render(request, 'main.html', {
29         'finished': finished,
30         'upcoming': upcoming,
31         'organizations': organizations,
32         'more_upcoming': more_upcoming,
33         'more_finished': more_finished,
34         'more_organizations': more_organizations,
35     })
36
37
38 @check_honeypot
39 def register(request):
40     if request.method == 'POST':
41         form = RegistrationForm(request.POST, request.FILES)
42         if form.is_valid():
43             u = User.objects.create(
44                     username=form.cleaned_data['email'],
45                     first_name=form.cleaned_data['first_name'],
46                     last_name=form.cleaned_data['last_name'],
47                     email=form.cleaned_data['email']
48                 )
49             u.set_password(form.cleaned_data['password'])
50             u.save()
51             login(request, authenticate(username=form.cleaned_data['email'], password=form.cleaned_data['password']))
52             send_mail(
53                 'Registered at MIL/PEER',
54                 '''You have been successfully registered at MIL/PEER with this e-mail address.
55
56 Thank you.
57
58 -- 
59 MIL/PEER team.''', 'milpeer@mdrn.pl', [form.cleaned_data['email']])
60             return redirect('/')
61     else:
62         form = RegistrationForm()
63     return render(request, 'registration.html', {'form': form})