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
8 from isbn.forms import WLISBNForm, WLConfirmForm, FNPISBNForm
9 from isbn.models import ONIXRecord
10 from isbn.utils import PRODUCT_FORMATS, PRODUCT_FORMS
13 @permission_required('add_onixrecord')
14 def add_isbn_wl(request):
16 return render(request, 'isbn/add_isbn.html', {
18 'next_view': reverse('confirm_isbn_wl'),
23 @permission_required('add_onixrecord')
24 def confirm_isbn_wl(request):
25 form = WLConfirmForm(data=request.POST)
26 if not form.is_valid():
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', {
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'))
45 return HttpResponseRedirect(reverse('wl_dc_tags', kwargs={'slug': data['dc_slug']}))
48 @permission_required('add_onixrecord')
49 def wl_dc_tags(request, slug):
50 records = ONIXRecord.objects.filter(dc_slug=slug)
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', {
57 'isbn_formats': isbn_formats,
58 'title': records[0].title,
62 @permission_required('add_onixrecord')
63 def add_isbn_fnp(request):
65 form = FNPISBNForm(data=request.POST)
68 return HttpResponseRedirect(reverse('assigned_isbn', kwargs={'slug': form.slug()}))
71 return render(request, 'isbn/add_isbn.html', {
77 def get_format(record):
78 if record.product_form_detail:
79 return PRODUCT_FORMATS[record.product_form_detail][0]
81 return [key for key, value in PRODUCT_FORMS.items() if value == record.product_form][0]
84 @permission_required('add_onixrecord')
85 def assigned_isbn(request, slug):
86 records = ONIXRecord.objects.filter(dc_slug=slug)
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,