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