Commit 650fe8bf5edffa03e653fc087ebb20ba2cd94067

Authored by Marcel Kawski
1 parent ec9dc7f9

Add utils.py and update forms.py

collector/storage/forms.py
... ... @@ -3,7 +3,8 @@ import re
3 3 from django.forms import ChoiceField, ModelChoiceField, ModelForm, Textarea, CharField, Form
4 4 from django import forms
5 5  
6   -from .models import Chunk, Document, Participant, Metadata, Keyword
  6 +from .models import Chunk, Document, Participant, Metadata, Keyword, Magazine, BookWithMultipleAuthors
  7 +from .utils import get_doc_with_type
7 8 from projects.ppc.models import Utterance
8 9 from collector import settings
9 10  
... ... @@ -268,8 +269,36 @@ class MetadataForm(ModelForm):
268 269 }
269 270  
270 271  
271   -class DocDetailsForm(ModelForm):
  272 +DOC_TYPE_DETAILS_FIELDS = {
  273 + Document: {
  274 +
  275 + },
  276 + Magazine: {
  277 + 'fields': ['title', 'publication_date', 'publication_place', 'number', 'channel', 'original_lang'],
  278 + 'labels': {
  279 + 'title': 'Tytuł',
  280 + 'publication_date': 'Data publikacji',
  281 + 'publication_place': 'Miejsce publikacji',
  282 + 'number': 'Numer',
  283 + 'channel': 'Kanał',
  284 + 'original_lang': 'Język oryginału'
  285 + }
  286 + },
  287 + BookWithMultipleAuthors: {
  288 + 'fields': ['title', 'publication_date', 'publication_place', 'publisher', 'channel', 'original_lang'],
  289 + 'labels': {
  290 + 'title': 'Tytuł',
  291 + 'publication_date': 'Data publikacji',
  292 + 'publication_place': 'Miejsce publikacji',
  293 + 'publisher': 'Wydawca',
  294 + 'channel': 'Kanał',
  295 + 'original_lang': 'Język oryginału'
  296 + }
  297 + }
  298 +}
272 299  
  300 +
  301 +class DocDetailsForm(ModelForm):
273 302 def __init__(self, *args, **kwargs):
274 303 super(DocDetailsForm, self).__init__(*args, **kwargs)
275 304 self.fields['title'].widget = Textarea(attrs={'rows': 1})
... ... @@ -292,6 +321,54 @@ class DocDetailsForm(ModelForm):
292 321 }
293 322  
294 323  
  324 +class MagazineDetailsForm(ModelForm):
  325 + def __init__(self, *args, **kwargs):
  326 + super(MagazineDetailsForm, self).__init__(*args, **kwargs)
  327 + self.fields['title'].widget = Textarea(attrs={'rows': 1})
  328 +
  329 + def save(self, commit=True):
  330 + magazine = super(MagazineDetailsForm, self).save(commit)
  331 + magazine.changed = True
  332 + magazine.save()
  333 + return magazine
  334 +
  335 + class Meta:
  336 + model = Magazine
  337 + fields = ['title', 'publication_date', 'publication_place', 'number', 'channel', 'original_lang']
  338 + labels = {
  339 + 'title': 'Tytuł',
  340 + 'publication_date': 'Data publikacji',
  341 + 'publication_place': 'Miejsce publikacji',
  342 + 'number': 'Numer',
  343 + 'channel': 'Kanał',
  344 + 'original_lang': 'Język oryginału'
  345 + }
  346 +
  347 +
  348 +class BWMADetailsForm(ModelForm):
  349 + def __init__(self, *args, **kwargs):
  350 + super(BWMADetailsForm, self).__init__(*args, **kwargs)
  351 + self.fields['title'].widget = Textarea(attrs={'rows': 1})
  352 +
  353 + def save(self, commit=True):
  354 + bwma = super(BWMADetailsForm, self).save(commit)
  355 + bwma.changed = True
  356 + bwma.save()
  357 + return bwma
  358 +
  359 + class Meta:
  360 + model = BookWithMultipleAuthors
  361 + fields = ['title', 'publication_date', 'publication_place', 'publisher', 'channel', 'original_lang']
  362 + labels = {
  363 + 'title': 'Tytuł',
  364 + 'publication_date': 'Data publikacji',
  365 + 'publication_place': 'Miejsce publikacji',
  366 + 'publisher': 'Wydawca',
  367 + 'channel': 'Kanał',
  368 + 'original_lang': 'Język oryginału'
  369 + }
  370 +
  371 +
295 372 class SubDocDetailsForm(ModelForm):
296 373  
297 374 def __init__(self, *args, **kwargs):
... ... @@ -387,3 +464,18 @@ class ChunkSplitForm(Form):
387 464 self.fields['text'].widget = Textarea(attrs={'class': 'formset-field',
388 465 'placeholder': 'Tekst akapitu',
389 466 'rows': 5})
  467 +
  468 +
  469 +class DocTypeForm(ModelForm):
  470 + doc_type = ChoiceField(label='Typ dokumentu')
  471 +
  472 + def __init__(self, *args, **kwargs):
  473 + doc_types = kwargs.pop('doc_types')
  474 + super(DocTypeForm, self).__init__(*args, **kwargs)
  475 + self.fields['doc_type'].choices = doc_types
  476 + self.instance = get_doc_with_type(self.instance.id)
  477 + self.fields['doc_type'].initial = type(self.instance).__name__
  478 +
  479 + class Meta:
  480 + model = Document
  481 + fields = ['doc_type']
... ...
collector/storage/models.py
... ... @@ -156,7 +156,7 @@ class Document(models.Model):
156 156  
157 157  
158 158 class Magazine(Document):
159   - number2 = models.CharField(max_length=50, null=True)
  159 + number = models.CharField(max_length=50, null=True)
160 160  
161 161 def check_details_filling(self):
162 162 if self.title == '' or self.publication_date is None or self.publication_place == '' or self.number == '' \
... ... @@ -170,7 +170,7 @@ class Magazine(Document):
170 170  
171 171  
172 172 class BookWithMultipleAuthors(Document):
173   - publisher2 = models.CharField(max_length=100, null=True)
  173 + publisher = models.CharField(max_length=100, null=True)
174 174  
175 175 def check_details_filling(self):
176 176 if self.title == '' or self.publication_date is None or self.publication_place == '' or self.publisher == '' \
... ...
collector/storage/utils.py 0 → 100755
  1 +from .models import Document, Magazine, BookWithMultipleAuthors, Article, Chapter
  2 +
  3 +DOCUMENT_TYPES_CLASSES = Document, Magazine, BookWithMultipleAuthors
  4 +
  5 +SUBDOCUMENT_TYPES = { # first subdocument type in tuples are the default ones for this type of the document
  6 + Document: (Document,),
  7 + Magazine: (Article,),
  8 + BookWithMultipleAuthors: (Chapter,)
  9 +}
  10 +
  11 +
  12 +def get_remaining_doc_types_tuple(current_type):
  13 + doc_types = []
  14 + for doc_type_model in DOCUMENT_TYPES_CLASSES:
  15 + if doc_type_model is not current_type:
  16 + doc_types.append((doc_type_model.__name__, doc_type_model.get_doc_type_display()))
  17 + return tuple(doc_types)
  18 +
  19 +
  20 +def get_remaining_subdoc_types_tuple(current_parent_type, current_type):
  21 + doc_types = []
  22 + for doc_type_model in SUBDOCUMENT_TYPES[current_parent_type]:
  23 + if doc_type_model is not current_type:
  24 + doc_types.append((doc_type_model.__name__, doc_type_model.get_doc_type_display()))
  25 + return tuple(doc_types)
  26 +
  27 +
  28 +def get_doc_with_type(doc_id):
  29 + all_document_types = list(
  30 + set(list(DOCUMENT_TYPES_CLASSES) + [dt for dt_list in SUBDOCUMENT_TYPES.values() for dt in dt_list]))
  31 + all_document_types.append(all_document_types.pop(all_document_types.index(Document))) # Document at the end
  32 + for doc_type in all_document_types:
  33 + try:
  34 + doc = doc_type.objects.get(id=doc_id)
  35 + return doc
  36 + except doc_type.DoesNotExist:
  37 + continue
  38 + return None # TODO
... ...