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
7 from isbn.forms import WLISBNForm, WLConfirmForm, FNPISBNForm
8 from isbn.models import ONIXRecord
9 from isbn.utils import PRODUCT_FORMATS, PRODUCT_FORMS
12 @permission_required('add_onixrecord')
13 def add_isbn_wl(request):
15 return render(request, 'isbn/add_isbn.html', {
17 'next_view': reverse('confirm_isbn_wl'),
22 @permission_required('add_onixrecord')
23 def confirm_isbn_wl(request):
24 form = WLConfirmForm(data=request.POST)
25 if not form.is_valid():
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', {
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'))
44 return HttpResponseRedirect(reverse('wl_dc_tags', kwargs={'slug': data['dc_slug']}))
47 @permission_required('add_onixrecord')
48 def wl_dc_tags(request, slug):
49 records = ONIXRecord.objects.filter(dc_slug=slug)
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', {
56 'isbn_formats': isbn_formats,
57 'title': records[0].title,
61 @permission_required('add_onixrecord')
62 def add_isbn_fnp(request):
64 form = FNPISBNForm(data=request.POST)
67 return HttpResponseRedirect(reverse('assigned_isbn', kwargs={'slug': form.slug()}))
70 return render(request, 'isbn/add_isbn.html', {
76 def get_format(record):
77 if record.product_form_detail:
78 return PRODUCT_FORMATS[record.product_form_detail][0]
80 return [key for key, value in PRODUCT_FORMS.items() if value == record.product_form][0]
83 @permission_required('add_onixrecord')
84 def assigned_isbn(request, slug):
85 records = ONIXRecord.objects.filter(dc_slug=slug)
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,