Commit 9ed7eb111566793e580074fcedb378750547ce0a

Authored by Marcel Kawski
1 parent c0d1dda5

Correct handling subdocuments types

collector/storage/static/storage/css/document.css
@@ -421,3 +421,7 @@ ul.legend { @@ -421,3 +421,7 @@ ul.legend {
421 .navbar-brand:hover, .nav-item:hover { 421 .navbar-brand:hover, .nav-item:hover {
422 color: #0a79df; 422 color: #0a79df;
423 } 423 }
  424 +
  425 +#change-doc-type-btn {
  426 + margin-left: 10px;
  427 +}
collector/storage/templates/storage/annotation.html
@@ -102,8 +102,8 @@ @@ -102,8 +102,8 @@
102 <div class="text-align-center"> 102 <div class="text-align-center">
103 <p> 103 <p>
104 Typ dokumentu: <strong>{{ doc_type }}</strong> 104 Typ dokumentu: <strong>{{ doc_type }}</strong>
105 - <button class="change-doc-type btn btn-primary" type="button" data-id="{% url 'change_doc_type' document.id %}">  
106 - Zmień 105 + <button class="change-doc-type btn btn-primary" id="change-doc-type-btn" type="button"
  106 + data-id="{% url 'change_doc_type' document.id %}">Zmień
107 </button> 107 </button>
108 </p> 108 </p>
109 </div> 109 </div>
collector/storage/templates/storage/edit.html
@@ -34,10 +34,16 @@ @@ -34,10 +34,16 @@
34 <div class="modal-body"> 34 <div class="modal-body">
35 35
36 {% for message in messages %} 36 {% for message in messages %}
37 - <div class="alert alert-{{ message.tags }} text-center" role="alert">  
38 - {{ message }}  
39 - </div>  
40 - {% endfor %} 37 + {% if message.tags == "error" %}
  38 + <div class="alert alert-danger text-center" role="alert">
  39 + {{ message }}
  40 + </div>
  41 + {% else %}
  42 + <div class="alert alert-{{ message.tags }} text-center" role="alert">
  43 + {{ message }}
  44 + </div>
  45 + {% endif %}
  46 + {% endfor %}
41 47
42 <div class="{% if form.non_field_errors %}invalid{% endif %} mb-2"> 48 <div class="{% if form.non_field_errors %}invalid{% endif %} mb-2">
43 {% for error in form.non_field_errors %} 49 {% for error in form.non_field_errors %}
collector/storage/utils.py
1 from .models import Document, Magazine, BookWithMultipleAuthors, Article, Chapter 1 from .models import Document, Magazine, BookWithMultipleAuthors, Article, Chapter
2 2
3 -  
4 DOCUMENT_TYPES_CLASSES = Document, Magazine, BookWithMultipleAuthors 3 DOCUMENT_TYPES_CLASSES = Document, Magazine, BookWithMultipleAuthors
5 4
6 SUBDOCUMENT_TYPES = { # first subdocument type in tuples are the default ones for this type of the document 5 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, ) 6 + Document: (Document,),
  7 + Magazine: (Article,),
  8 + BookWithMultipleAuthors: (Chapter,)
10 } 9 }
11 10
  11 +
12 def get_remaining_doc_types_tuple(current_type): 12 def get_remaining_doc_types_tuple(current_type):
13 doc_types = [] 13 doc_types = []
14 for doc_type_model in DOCUMENT_TYPES_CLASSES: 14 for doc_type_model in DOCUMENT_TYPES_CLASSES:
@@ -17,8 +17,17 @@ def get_remaining_doc_types_tuple(current_type): @@ -17,8 +17,17 @@ def get_remaining_doc_types_tuple(current_type):
17 return tuple(doc_types) 17 return tuple(doc_types)
18 18
19 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 +
20 def get_doc_with_type(doc_id): 28 def get_doc_with_type(doc_id):
21 - all_document_types = list(set(list(DOCUMENT_TYPES_CLASSES) + [dt for dt_list in SUBDOCUMENT_TYPES.values() for dt in dt_list])) 29 + all_document_types = list(
  30 + 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 31 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: 32 for doc_type in all_document_types:
24 try: 33 try:
collector/storage/views.py
@@ -25,7 +25,7 @@ from .forms import ChunkForm, ParticipantForm, SubchunkForm, MetadataForm, Keywo @@ -25,7 +25,7 @@ from .forms import ChunkForm, ParticipantForm, SubchunkForm, MetadataForm, Keywo
25 SubDocDetailsForm, AuthorForm, ChunkMergeForm, ChunkSplitForm, DocTypeForm, DocDetailsForm, MagazineDetailsForm, \ 25 SubDocDetailsForm, AuthorForm, ChunkMergeForm, ChunkSplitForm, DocTypeForm, DocDetailsForm, MagazineDetailsForm, \
26 BWMADetailsForm 26 BWMADetailsForm
27 from .models import Chunk, Document, Participant, Metadata, Keyword, Annotation, Magazine, BookWithMultipleAuthors 27 from .models import Chunk, Document, Participant, Metadata, Keyword, Annotation, Magazine, BookWithMultipleAuthors
28 -from .utils import get_remaining_doc_types_tuple, get_doc_with_type, SUBDOCUMENT_TYPES 28 +from .utils import get_remaining_doc_types_tuple, get_remaining_subdoc_types_tuple, get_doc_with_type, SUBDOCUMENT_TYPES
29 from projects.ppc.models import Utterance 29 from projects.ppc.models import Utterance
30 from pipeline.models import ProcessingStatus 30 from pipeline.models import ProcessingStatus
31 31
@@ -1204,7 +1204,15 @@ class DocTypeView(UpdateView): @@ -1204,7 +1204,15 @@ class DocTypeView(UpdateView):
1204 def get_form_kwargs(self): 1204 def get_form_kwargs(self):
1205 kwargs = super(DocTypeView, self).get_form_kwargs() 1205 kwargs = super(DocTypeView, self).get_form_kwargs()
1206 self.object = get_doc_with_type(self.object.id) 1206 self.object = get_doc_with_type(self.object.id)
1207 - kwargs['doc_types'] = get_remaining_doc_types_tuple(type(self.object)) 1207 + if self.object.parent is None:
  1208 + doc_types = get_remaining_doc_types_tuple(type(self.object))
  1209 + else:
  1210 + doc_types = get_remaining_subdoc_types_tuple(type(get_doc_with_type(self.object.parent.id)),
  1211 + type(self.object))
  1212 + if not doc_types:
  1213 + messages.error(self.request, 'Nie jest możliwa zmiana typu dokumentu. Dla tego typu dokumentu głównego nie '
  1214 + 'ma dostępnych innych typów dokumentów.')
  1215 + kwargs['doc_types'] = doc_types
1208 return kwargs 1216 return kwargs
1209 1217
1210 def get_success_url(self, new_doc): 1218 def get_success_url(self, new_doc):