1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
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
10 from isbn.forms import WLISBNForm, WLConfirmForm, FNPISBNForm
11 from isbn.models import ONIXRecord
12 from isbn.utils import PRODUCT_FORMATS, PRODUCT_FORMS
15 @permission_required('add_onixrecord')
16 def add_isbn_wl(request):
18 return render(request, 'isbn/add_isbn.html', {
20 'next_view': reverse('confirm_isbn_wl'),
25 @permission_required('add_onixrecord')
26 def confirm_isbn_wl(request):
27 form = WLConfirmForm(data=request.POST)
28 if not form.is_valid():
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', {
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'))
47 return HttpResponseRedirect(reverse('wl_dc_tags', kwargs={'slug': data['dc_slug']}))
50 @permission_required('add_onixrecord')
51 def wl_dc_tags(request, slug):
52 records = ONIXRecord.objects.filter(dc_slug=slug)
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', {
59 'isbn_formats': isbn_formats,
60 'title': records[0].title,
64 @permission_required('add_onixrecord')
65 def add_isbn_fnp(request):
67 form = FNPISBNForm(data=request.POST)
70 return HttpResponseRedirect(reverse('assigned_isbn', kwargs={'slug': form.slug()}))
73 return render(request, 'isbn/add_isbn.html', {
79 def get_format(record):
80 if record.product_form_detail:
81 return PRODUCT_FORMATS[record.product_form_detail][0]
83 return [key for key, value in PRODUCT_FORMS.items() if value == record.product_form][0]
86 @permission_required('add_onixrecord')
87 def assigned_isbn(request, slug):
88 records = ONIXRecord.objects.filter(dc_slug=slug)
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,