From 9ed7eb111566793e580074fcedb378750547ce0a Mon Sep 17 00:00:00 2001
From: marcelkawski <marcel.kawski1@gmail.com>
Date: Mon, 11 Oct 2021 15:27:17 +0200
Subject: [PATCH] Correct handling subdocuments types

---
 collector/storage/static/storage/css/document.css   |  4 ++++
 collector/storage/templates/storage/annotation.html |  4 ++--
 collector/storage/templates/storage/edit.html       | 14 ++++++++++----
 collector/storage/utils.py                          | 19 ++++++++++++++-----
 collector/storage/views.py                          | 12 ++++++++++--
 5 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/collector/storage/static/storage/css/document.css b/collector/storage/static/storage/css/document.css
index 29ad044..d074f53 100755
--- a/collector/storage/static/storage/css/document.css
+++ b/collector/storage/static/storage/css/document.css
@@ -421,3 +421,7 @@ ul.legend {
 .navbar-brand:hover, .nav-item:hover {
     color: #0a79df;
 }
+
+#change-doc-type-btn {
+    margin-left: 10px;
+}
diff --git a/collector/storage/templates/storage/annotation.html b/collector/storage/templates/storage/annotation.html
index 27176f3..3cab2dc 100755
--- a/collector/storage/templates/storage/annotation.html
+++ b/collector/storage/templates/storage/annotation.html
@@ -102,8 +102,8 @@
     <div class="text-align-center">
       <p>
         Typ dokumentu: <strong>{{ doc_type }}</strong>
-        <button class="change-doc-type btn btn-primary" type="button" data-id="{% url 'change_doc_type' document.id %}">
-          Zmień
+        <button class="change-doc-type btn btn-primary" id="change-doc-type-btn" type="button"
+                data-id="{% url 'change_doc_type' document.id %}">Zmień
         </button>
       </p>
     </div>
diff --git a/collector/storage/templates/storage/edit.html b/collector/storage/templates/storage/edit.html
index 5c53ae8..277bf6f 100755
--- a/collector/storage/templates/storage/edit.html
+++ b/collector/storage/templates/storage/edit.html
@@ -34,10 +34,16 @@
     <div class="modal-body">
       
       {% for message in messages %}
-        <div class="alert alert-{{ message.tags }} text-center" role="alert">
-          {{ message }}
-        </div>
-    {% endfor %}
+        {% if message.tags == "error" %}
+          <div class="alert alert-danger text-center" role="alert">
+            {{ message }}
+          </div>
+        {% else %}
+          <div class="alert alert-{{ message.tags }} text-center" role="alert">
+            {{ message }}
+          </div>
+        {% endif %}
+      {% endfor %}
   
       <div class="{% if form.non_field_errors %}invalid{% endif %} mb-2">
         {% for error in form.non_field_errors %}
diff --git a/collector/storage/utils.py b/collector/storage/utils.py
index aad0435..097518f 100755
--- a/collector/storage/utils.py
+++ b/collector/storage/utils.py
@@ -1,14 +1,14 @@
 from .models import Document, Magazine, BookWithMultipleAuthors, Article, Chapter
 
-
 DOCUMENT_TYPES_CLASSES = Document, Magazine, BookWithMultipleAuthors
 
 SUBDOCUMENT_TYPES = {  # first subdocument type in tuples are the default ones for this type of the document
-    Document: (Document, ),
-    Magazine: (Article, ),
-    BookWithMultipleAuthors: (Chapter, )
+    Document: (Document,),
+    Magazine: (Article,),
+    BookWithMultipleAuthors: (Chapter,)
 }
 
+
 def get_remaining_doc_types_tuple(current_type):
     doc_types = []
     for doc_type_model in DOCUMENT_TYPES_CLASSES:
@@ -17,8 +17,17 @@ def get_remaining_doc_types_tuple(current_type):
     return tuple(doc_types)
 
 
+def get_remaining_subdoc_types_tuple(current_parent_type, current_type):
+    doc_types = []
+    for doc_type_model in SUBDOCUMENT_TYPES[current_parent_type]:
+        if doc_type_model is not current_type:
+            doc_types.append((doc_type_model.__name__, doc_type_model.get_doc_type_display()))
+    return tuple(doc_types)
+
+
 def get_doc_with_type(doc_id):
-    all_document_types = list(set(list(DOCUMENT_TYPES_CLASSES) + [dt for dt_list in SUBDOCUMENT_TYPES.values() for dt in dt_list]))
+    all_document_types = list(
+        set(list(DOCUMENT_TYPES_CLASSES) + [dt for dt_list in SUBDOCUMENT_TYPES.values() for dt in dt_list]))
     all_document_types.append(all_document_types.pop(all_document_types.index(Document)))  # Document at the end
     for doc_type in all_document_types:
         try:
diff --git a/collector/storage/views.py b/collector/storage/views.py
index a3ed706..2094574 100755
--- a/collector/storage/views.py
+++ b/collector/storage/views.py
@@ -25,7 +25,7 @@ from .forms import ChunkForm, ParticipantForm, SubchunkForm, MetadataForm, Keywo
     SubDocDetailsForm, AuthorForm, ChunkMergeForm, ChunkSplitForm, DocTypeForm, DocDetailsForm, MagazineDetailsForm, \
     BWMADetailsForm
 from .models import Chunk, Document, Participant, Metadata, Keyword, Annotation, Magazine, BookWithMultipleAuthors
-from .utils import get_remaining_doc_types_tuple, get_doc_with_type, SUBDOCUMENT_TYPES
+from .utils import get_remaining_doc_types_tuple, get_remaining_subdoc_types_tuple, get_doc_with_type, SUBDOCUMENT_TYPES
 from projects.ppc.models import Utterance
 from pipeline.models import ProcessingStatus
 
@@ -1204,7 +1204,15 @@ class DocTypeView(UpdateView):
     def get_form_kwargs(self):
         kwargs = super(DocTypeView, self).get_form_kwargs()
         self.object = get_doc_with_type(self.object.id)
-        kwargs['doc_types'] = get_remaining_doc_types_tuple(type(self.object))
+        if self.object.parent is None:
+            doc_types = get_remaining_doc_types_tuple(type(self.object))
+        else:
+            doc_types = get_remaining_subdoc_types_tuple(type(get_doc_with_type(self.object.parent.id)),
+                                                                   type(self.object))
+        if not doc_types:
+            messages.error(self.request, 'Nie jest możliwa zmiana typu dokumentu. Dla tego typu dokumentu głównego nie '
+                                         'ma dostępnych innych typów dokumentów.')
+        kwargs['doc_types'] = doc_types
         return kwargs
 
     def get_success_url(self, new_doc):
--
libgit2 0.22.2