fix newsletter field in registration form
[wolnelektury.git] / src / isbn / views.py
1 # -*- coding: utf-8 -*-
2 from django.contrib.auth.decorators import permission_required
3 from django.core.urlresolvers import reverse
4 from django.http.response import HttpResponseRedirect
5 from django.shortcuts import render
6 from django.views.decorators.http import require_POST
7
8 from isbn.forms import WLISBNForm, WLConfirmForm, FNPISBNForm
9 from isbn.models import ONIXRecord
10 from isbn.utils import PRODUCT_FORMATS, PRODUCT_FORMS
11
12
13 @permission_required('add_onixrecord')
14 def add_isbn_wl(request):
15     form = WLISBNForm()
16     return render(request, 'isbn/add_isbn.html', {
17         'form': form,
18         'next_view': reverse('confirm_isbn_wl'),
19     })
20
21
22 @require_POST
23 @permission_required('add_onixrecord')
24 def confirm_isbn_wl(request):
25     form = WLConfirmForm(data=request.POST)
26     if not form.is_valid():
27         # komunikat?
28         return HttpResponseRedirect(reverse('add_isbn_wl'))
29     data = form.prepare_data()
30     if ONIXRecord.objects.filter(dc_slug=data['dc_slug']).exists():
31         return HttpResponseRedirect(reverse('wl_dc_tags', kwargs={'slug': data['dc_slug']}))
32     return render(request, 'isbn/confirm_isbn_wl.html', {
33         'data': data,
34         'form': form,
35     })
36
37
38 @require_POST
39 @permission_required('add_onixrecord')
40 def save_wl_onix(request):
41     form = WLConfirmForm(data=request.POST)
42     if not form.is_valid():
43         return HttpResponseRedirect(reverse('add_isbn_wl'))
44     data = form.save()
45     return HttpResponseRedirect(reverse('wl_dc_tags', kwargs={'slug': data['dc_slug']}))
46
47
48 @permission_required('add_onixrecord')
49 def wl_dc_tags(request, slug):
50     records = ONIXRecord.objects.filter(dc_slug=slug)
51     isbn_formats = []
52     for record in records:
53         file_format, content_type = PRODUCT_FORMATS[record.product_form_detail]
54         isbn_formats.append((file_format, content_type, record.isbn(dashes=True)))
55     return render(request, 'isbn/wl_dc_tags.html', {
56         'slug': slug,
57         'isbn_formats': isbn_formats,
58         'title': records[0].title,
59     })
60
61
62 @permission_required('add_onixrecord')
63 def add_isbn_fnp(request):
64     if request.POST:
65         form = FNPISBNForm(data=request.POST)
66         if form.is_valid():
67             form.save()
68             return HttpResponseRedirect(reverse('assigned_isbn', kwargs={'slug': form.slug()}))
69     else:
70         form = FNPISBNForm()
71     return render(request, 'isbn/add_isbn.html', {
72         'form': form,
73         'next_view': '',
74     })
75
76
77 def get_format(record):
78     if record.product_form_detail:
79         return PRODUCT_FORMATS[record.product_form_detail][0]
80     else:
81         return [key for key, value in PRODUCT_FORMS.iteritems() if value == record.product_form][0]
82
83
84 @permission_required('add_onixrecord')
85 def assigned_isbn(request, slug):
86     records = ONIXRecord.objects.filter(dc_slug=slug)
87     isbn_formats = [
88         (get_format(record), record.isbn(dashes=True)) for record in records]
89     return render(request, 'isbn/assigned_isbn.html', {
90         'title': records[0].title,
91         'isbn_formats': isbn_formats,
92     })