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