Commit c0d1dda576bf0c1fa5d04cc5e4747f4cd61f74f9

Authored by Marcel Kawski
1 parent b583b73d

Add handling subdocuments types

collector/storage/admin.py
... ... @@ -3,7 +3,7 @@ from django.contrib import admin
3 3 from admin_numeric_filter.admin import RangeNumericFilter
4 4  
5 5 from storage.models import Chunk, Document, Keyword, Metadata, Participant, Annotation, Magazine, \
6   - BookWithMultipleAuthors
  6 + BookWithMultipleAuthors, Article, Chapter
7 7  
8 8  
9 9 class ChunkInline(admin.StackedInline):
... ... @@ -52,6 +52,8 @@ admin.site.register(Chunk, ChunkAdmin)
52 52 admin.site.register(Document, DocumentAdmin)
53 53 admin.site.register(Magazine)
54 54 admin.site.register(BookWithMultipleAuthors)
  55 +admin.site.register(Article)
  56 +admin.site.register(Chapter)
55 57 admin.site.register(Keyword)
56 58 admin.site.register(Participant)
57 59 admin.site.register(Annotation, AnnotationAdmin)
... ...
collector/storage/models.py
... ... @@ -141,7 +141,7 @@ class Document(models.Model):
141 141 return True
142 142  
143 143 @staticmethod
144   - def get_doc_type():
  144 + def get_doc_type_display():
145 145 return 'dokument'
146 146  
147 147 class Meta:
... ... @@ -165,7 +165,7 @@ class Magazine(Document):
165 165 return True
166 166  
167 167 @staticmethod
168   - def get_doc_type():
  168 + def get_doc_type_display():
169 169 return 'czasopismo'
170 170  
171 171  
... ... @@ -179,10 +179,22 @@ class BookWithMultipleAuthors(Document):
179 179 return True
180 180  
181 181 @staticmethod
182   - def get_doc_type():
  182 + def get_doc_type_display():
183 183 return 'książka o wielu autorach'
184 184  
185 185  
  186 +class Article(Document):
  187 + @staticmethod
  188 + def get_doc_type_display():
  189 + return 'artykuł'
  190 +
  191 +
  192 +class Chapter(Document):
  193 + @staticmethod
  194 + def get_doc_type_display():
  195 + return 'rozdział'
  196 +
  197 +
186 198 class Chunk(models.Model):
187 199 document = models.ForeignKey(Document, related_name='chunks', on_delete=models.CASCADE)
188 200 sequence = models.PositiveIntegerField()
... ...
collector/storage/utils.py
1   -from .models import Document, Magazine, BookWithMultipleAuthors
  1 +from .models import Document, Magazine, BookWithMultipleAuthors, Article, Chapter
2 2  
3 3  
4 4 DOCUMENT_TYPES_CLASSES = Document, Magazine, BookWithMultipleAuthors
5 5  
  6 +SUBDOCUMENT_TYPES = { # first subdocument type in tuples are the default ones for this type of the document
  7 + Document: (Document, ),
  8 + Magazine: (Article, ),
  9 + BookWithMultipleAuthors: (Chapter, )
  10 +}
6 11  
7   -def get_doc_types_tuple():
  12 +def get_remaining_doc_types_tuple(current_type):
8 13 doc_types = []
9 14 for doc_type_model in DOCUMENT_TYPES_CLASSES:
10   - doc_types.append((doc_type_model.__name__, doc_type_model.get_doc_type()))
  15 + if doc_type_model is not current_type:
  16 + doc_types.append((doc_type_model.__name__, doc_type_model.get_doc_type_display()))
11 17 return tuple(doc_types)
12 18  
  19 +
13 20 def get_doc_with_type(doc_id):
14   - try:
15   - doc = Magazine.objects.get(id=doc_id)
16   - except Magazine.DoesNotExist:
  21 + all_document_types = list(set(list(DOCUMENT_TYPES_CLASSES) + [dt for dt_list in SUBDOCUMENT_TYPES.values() for dt in dt_list]))
  22 + all_document_types.append(all_document_types.pop(all_document_types.index(Document))) # Document at the end
  23 + for doc_type in all_document_types:
17 24 try:
18   - doc = BookWithMultipleAuthors.objects.get(id=doc_id)
19   - except BookWithMultipleAuthors.DoesNotExist:
20   - try:
21   - doc = Document.objects.get(id=doc_id)
22   - except Document.DoesNotExist:
23   - return None # TODO
24   - return doc
25 25 \ No newline at end of file
  26 + doc = doc_type.objects.get(id=doc_id)
  27 + return doc
  28 + except doc_type.DoesNotExist:
  29 + continue
  30 + return None # TODO
... ...
collector/storage/views.py
... ... @@ -25,7 +25,7 @@ from .forms import ChunkForm, ParticipantForm, SubchunkForm, MetadataForm, Keywo
25 25 SubDocDetailsForm, AuthorForm, ChunkMergeForm, ChunkSplitForm, DocTypeForm, DocDetailsForm, MagazineDetailsForm, \
26 26 BWMADetailsForm
27 27 from .models import Chunk, Document, Participant, Metadata, Keyword, Annotation, Magazine, BookWithMultipleAuthors
28   -from .utils import get_doc_types_tuple, get_doc_with_type
  28 +from .utils import get_remaining_doc_types_tuple, get_doc_with_type, SUBDOCUMENT_TYPES
29 29 from projects.ppc.models import Utterance
30 30 from pipeline.models import ProcessingStatus
31 31  
... ... @@ -636,7 +636,7 @@ class AnnotationView(View):
636 636 context = {'document': doc}
637 637 doc = get_doc_with_type(doc_id)
638 638 if doc is not None:
639   - doc_type = doc.get_doc_type()
  639 + doc_type = doc.get_doc_type_display()
640 640 subdocuments = Document.objects.filter(parent=doc).order_by('sequence')
641 641 metadata = doc.metadata.order_by('sequence')
642 642 chunks = doc.chunks.order_by('sequence')
... ... @@ -948,17 +948,20 @@ class DocSplitView(UpdateView):
948 948 parent = self.object
949 949 else:
950 950 parent = self.object.parent
951   - new_document = Document.objects.create(name=parent.name,
952   - lang=parent.lang,
953   - original_lang=parent.original_lang,
954   - pipeline=parent.pipeline,
955   - publication_date=None,
956   - path=parent.path,
957   - type='',
958   - status='',
959   - parent=parent,
960   - processing_status=parent.processing_status,
961   - sequence=sequence + 1)
  951 + self.object = get_doc_with_type(self.object.id)
  952 + subdocument_class = SUBDOCUMENT_TYPES[type(self.object)][0]
  953 + print('sd class: ', subdocument_class)
  954 + new_document = subdocument_class.objects.create(name=parent.name,
  955 + lang=parent.lang,
  956 + original_lang=parent.original_lang,
  957 + pipeline=parent.pipeline,
  958 + publication_date=None,
  959 + path=parent.path,
  960 + type='',
  961 + status='',
  962 + parent=parent,
  963 + processing_status=parent.processing_status,
  964 + sequence=sequence + 1)
962 965 new_document.chunks.set(chunks.filter(sequence__range=(chunk_beg, chunk_end)))
963 966 new_document.save()
964 967 self.object.chunks.set(chunks.filter(sequence__lt=chunk_beg) | chunks.filter(sequence__gt=chunk_end))
... ... @@ -1194,13 +1197,14 @@ class DocTypeView(UpdateView):
1194 1197 context = super().get_context_data(**kwargs)
1195 1198 context['title'] = 'Zmiana typu dokumentu'
1196 1199 context['submit_btn_text'] = 'Zmień'
1197   - messages.warning(self.request, 'Uwaga! Zmiana typu dokumentu może spowodować usunięcie danych typowych dla tego typu,'
1198   - ' np. numeru dla czasopisma.')
  1200 + messages.warning(self.request, 'Uwaga! Zmiana typu dokumentu może spowodować usunięcie danych typowych dla '
  1201 + 'tego typu.')
1199 1202 return context
1200 1203  
1201 1204 def get_form_kwargs(self):
1202 1205 kwargs = super(DocTypeView, self).get_form_kwargs()
1203   - kwargs['doc_types'] = get_doc_types_tuple()
  1206 + self.object = get_doc_with_type(self.object.id)
  1207 + kwargs['doc_types'] = get_remaining_doc_types_tuple(type(self.object))
1204 1208 return kwargs
1205 1209  
1206 1210 def get_success_url(self, new_doc):
... ...