2017-07-26 21:33:38 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
'''
|
|
|
|
forms for bib models
|
|
|
|
'''
|
2017-07-25 13:28:14 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from ckeditor.widgets import CKEditorWidget
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from django.conf.global_settings import LANGUAGES
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from selectable.forms import (
|
|
|
|
AutoCompleteSelectWidget,
|
|
|
|
AutoCompleteSelectField
|
|
|
|
)
|
|
|
|
from regluit.core.lookups import (
|
|
|
|
WorkLookup,
|
|
|
|
PublisherNameLookup,
|
|
|
|
SubjectLookup,
|
|
|
|
EditionNoteLookup,
|
|
|
|
)
|
|
|
|
from regluit.bisac.models import BisacHeading
|
2018-01-03 22:50:21 +00:00
|
|
|
from regluit.core.cc import CHOICES as RIGHTS_CHOICES
|
2017-07-26 21:33:38 +00:00
|
|
|
from regluit.core.models import Edition, Identifier
|
2017-07-25 13:28:14 +00:00
|
|
|
from regluit.core.parameters import (
|
|
|
|
AGE_LEVEL_CHOICES,
|
2017-07-26 21:33:38 +00:00
|
|
|
ID_CHOICES,
|
2017-07-25 13:28:14 +00:00
|
|
|
TEXT_RELATION_CHOICES,
|
|
|
|
)
|
2017-08-03 20:15:06 +00:00
|
|
|
from regluit.core.validation import identifier_cleaner
|
2017-07-26 21:33:38 +00:00
|
|
|
from regluit.utils.text import sanitize_line, remove_badxml
|
2017-07-25 13:28:14 +00:00
|
|
|
|
|
|
|
CREATOR_RELATIONS = (
|
|
|
|
('aut', 'Author'),
|
|
|
|
('edt', 'Editor'),
|
|
|
|
('trl', 'Translator'),
|
|
|
|
('ill', 'Illustrator'),
|
|
|
|
('dsr', 'Designer'),
|
|
|
|
('aui', 'Author of Introduction'),
|
|
|
|
)
|
|
|
|
|
2017-07-26 21:33:38 +00:00
|
|
|
NULLS = [False, 'delete', '']
|
2017-07-25 13:28:14 +00:00
|
|
|
|
|
|
|
bisac_headings = BisacHeading.objects.all()
|
|
|
|
|
2017-07-26 21:33:38 +00:00
|
|
|
class IdentifierForm(forms.ModelForm):
|
|
|
|
id_type = forms.ChoiceField(label='Identifier Type', choices=ID_CHOICES)
|
2017-08-04 21:12:05 +00:00
|
|
|
id_value = forms.CharField(
|
|
|
|
label='Identifier Value',
|
|
|
|
widget=forms.TextInput(attrs={'size': 60}),
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
make_new = forms.BooleanField(
|
2017-10-26 17:03:05 +00:00
|
|
|
label='There\'s no existing Identifier. ',
|
2017-08-04 21:12:05 +00:00
|
|
|
required=False,
|
|
|
|
)
|
2017-07-26 21:33:38 +00:00
|
|
|
identifier = None
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
id_type = self.cleaned_data['id_type']
|
2017-08-04 21:12:05 +00:00
|
|
|
id_value = self.cleaned_data.get('id_value', '').strip()
|
|
|
|
make_new = self.cleaned_data.get('make_new', False)
|
|
|
|
if not make_new:
|
2017-10-17 19:44:44 +00:00
|
|
|
self.cleaned_data['id_value'] = identifier_cleaner(id_type)(id_value)
|
2017-07-26 21:33:38 +00:00
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Identifier
|
|
|
|
fields = ('id_type', 'id_value')
|
|
|
|
widgets = {
|
|
|
|
'id_value': forms.TextInput(attrs={'size': 40}),
|
|
|
|
}
|
|
|
|
|
2017-07-25 13:28:14 +00:00
|
|
|
class EditionForm(forms.ModelForm):
|
2017-07-26 21:33:38 +00:00
|
|
|
'''
|
|
|
|
form for bibliographic data (both editions and works
|
|
|
|
'''
|
2017-07-25 13:28:14 +00:00
|
|
|
add_author = forms.CharField(max_length=500, required=False)
|
|
|
|
add_author_relation = forms.ChoiceField(choices=CREATOR_RELATIONS, initial=('aut', 'Author'))
|
|
|
|
add_subject = AutoCompleteSelectField(
|
|
|
|
SubjectLookup,
|
|
|
|
widget=AutoCompleteSelectWidget(SubjectLookup, allow_new=True),
|
|
|
|
label='Keyword',
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
add_related_work = AutoCompleteSelectField(
|
|
|
|
WorkLookup,
|
|
|
|
widget=AutoCompleteSelectWidget(WorkLookup, allow_new=False, attrs={'size': 40}),
|
|
|
|
label='Related Work',
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
add_work_relation = forms.ChoiceField(
|
|
|
|
choices=TEXT_RELATION_CHOICES,
|
|
|
|
initial=('translation', 'translation'),
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
bisac = forms.ModelChoiceField( bisac_headings, required=False )
|
|
|
|
|
|
|
|
publisher_name = AutoCompleteSelectField(
|
|
|
|
PublisherNameLookup,
|
|
|
|
label='Publisher Name',
|
|
|
|
widget=AutoCompleteSelectWidget(PublisherNameLookup,allow_new=True),
|
|
|
|
required=False,
|
|
|
|
allow_new=True,
|
|
|
|
)
|
|
|
|
|
2017-07-26 21:33:38 +00:00
|
|
|
id_type = forms.ChoiceField(label='Identifier Type', choices=ID_CHOICES)
|
2017-08-03 20:15:06 +00:00
|
|
|
id_value = forms.CharField(
|
|
|
|
label='Identifier Value',
|
|
|
|
widget=forms.TextInput(attrs={'size': 60}),
|
|
|
|
required=False,
|
|
|
|
)
|
2017-07-25 13:28:14 +00:00
|
|
|
language = forms.ChoiceField(choices=LANGUAGES)
|
|
|
|
age_level = forms.ChoiceField(choices=AGE_LEVEL_CHOICES, required=False)
|
|
|
|
description = forms.CharField( required=False, widget=CKEditorWidget())
|
|
|
|
coverfile = forms.ImageField(required=False)
|
|
|
|
note = AutoCompleteSelectField(
|
|
|
|
EditionNoteLookup,
|
|
|
|
widget=AutoCompleteSelectWidget(EditionNoteLookup, allow_new=True),
|
|
|
|
label='Edition Note',
|
|
|
|
required=False,
|
|
|
|
allow_new=True,
|
|
|
|
)
|
2018-01-03 22:50:21 +00:00
|
|
|
set_rights = forms.CharField(widget=forms.Select(choices=RIGHTS_CHOICES), required=False)
|
|
|
|
|
2017-07-25 13:28:14 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(EditionForm, self).__init__(*args, **kwargs)
|
|
|
|
self.relators = []
|
|
|
|
if self.instance:
|
|
|
|
for relator in self.instance.relators.all():
|
|
|
|
select = forms.Select(choices=CREATOR_RELATIONS).render(
|
|
|
|
'change_relator_%s' % relator.id,
|
|
|
|
relator.relation.code
|
|
|
|
)
|
|
|
|
self.relators.append({'relator':relator, 'select':select})
|
|
|
|
|
|
|
|
def clean_title(self):
|
|
|
|
return sanitize_line(self.cleaned_data["title"])
|
|
|
|
|
|
|
|
def clean_add_author(self):
|
|
|
|
return sanitize_line(self.cleaned_data["add_author"])
|
|
|
|
|
|
|
|
def clean_description(self):
|
|
|
|
return remove_badxml(self.cleaned_data["description"])
|
|
|
|
|
|
|
|
def clean(self):
|
2017-07-26 21:33:38 +00:00
|
|
|
id_type = self.cleaned_data['id_type']
|
2017-08-03 20:15:06 +00:00
|
|
|
id_value = self.cleaned_data.get('id_value','').strip()
|
|
|
|
if id_value:
|
|
|
|
try:
|
2018-05-11 01:45:56 +00:00
|
|
|
id_value = identifier_cleaner(id_type)(id_value)
|
|
|
|
identifier = Identifier.objects.filter(type=id_type, value=id_value)
|
|
|
|
ident = identifier[0] if identifier else None
|
|
|
|
if not ident or not self.instance:
|
|
|
|
self.cleaned_data['id_value'] = id_value
|
|
|
|
elif ident.edition_id == self.instance.id:
|
|
|
|
self.cleaned_data['id_value'] = id_value
|
|
|
|
elif not ident.edition_id and ident.work_id == self.instance.work_id:
|
|
|
|
self.cleaned_data['id_value'] = id_value
|
|
|
|
else:
|
|
|
|
if ident.edition_id:
|
|
|
|
err_msg = "{} is a duplicate for edition #{}.".format(id_value, ident.edition_id)
|
|
|
|
else:
|
|
|
|
err_msg = "{} is a duplicate for work #{}.".format(id_value, ident.work_id)
|
|
|
|
self.add_error('id_value', forms.ValidationError(err_msg))
|
2020-02-14 03:28:51 +00:00
|
|
|
except forms.ValidationError as ve:
|
2018-05-11 01:45:56 +00:00
|
|
|
self.add_error(
|
|
|
|
'id_value',
|
|
|
|
forms.ValidationError('{}: {}'.format(ve.message, id_value))
|
|
|
|
)
|
2017-07-25 13:28:14 +00:00
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Edition
|
|
|
|
exclude = ('created', 'work')
|
|
|
|
widgets = {
|
|
|
|
'title': forms.TextInput(attrs={'size': 40}),
|
|
|
|
'add_author': forms.TextInput(attrs={'size': 30}),
|
|
|
|
'add_subject': forms.TextInput(attrs={'size': 30}),
|
|
|
|
'unglued': forms.CheckboxInput(),
|
|
|
|
'cover_image': forms.TextInput(attrs={'size': 60}),
|
|
|
|
}
|
|
|
|
|