Commit ff4f6d502b6b2f8e79a88535f3205bce11f3ba35

Authored by Marcel Kawski
1 parent a82b248a

Add changing document type

collector/storage/admin.py
... ... @@ -2,7 +2,7 @@ from django.contrib import admin
2 2  
3 3 from admin_numeric_filter.admin import RangeNumericFilter
4 4  
5   -from storage.models import Chunk, Document, Keyword, Metadata, Participant, Annotation
  5 +from storage.models import Chunk, Document, Keyword, Metadata, Participant, Annotation, DocumentType
6 6  
7 7  
8 8 class ChunkInline(admin.StackedInline):
... ... @@ -52,3 +52,4 @@ admin.site.register(Document, DocumentAdmin)
52 52 admin.site.register(Keyword)
53 53 admin.site.register(Participant)
54 54 admin.site.register(Annotation, AnnotationAdmin)
  55 +admin.site.register(DocumentType)
... ...
collector/storage/forms.py
... ... @@ -388,3 +388,27 @@ class ChunkSplitForm(Form):
388 388 self.fields['text'].widget = Textarea(attrs={'class': 'formset-field',
389 389 'placeholder': 'Tekst akapitu',
390 390 'rows': 5})
  391 +
  392 +
  393 +class DocTypeForm(ModelForm):
  394 +
  395 + def __init__(self, *args, **kwargs):
  396 + super(DocTypeForm, self).__init__(*args, **kwargs)
  397 + self.fields['doc_type'].label_from_instance = self.label_from_instance
  398 +
  399 + def save(self, commit=True):
  400 + document = super(DocTypeForm, self).save(commit)
  401 + document.changed = True
  402 + document.save()
  403 + return document
  404 +
  405 + @staticmethod
  406 + def label_from_instance(obj):
  407 + return obj.name
  408 +
  409 + class Meta:
  410 + model = Document
  411 + fields = ['doc_type']
  412 + labels = {
  413 + 'doc_type': 'Typ dokumentu'
  414 + }
... ...
collector/storage/models.py
... ... @@ -12,6 +12,14 @@ from pipeline.models import Pipeline, ProcessingStatus
12 12 User = get_user_model()
13 13  
14 14  
  15 +class DocumentType(models.Model):
  16 + key = models.CharField(max_length=32, unique=True)
  17 + name = models.CharField(max_length=32, unique=True)
  18 +
  19 + def __str__(self):
  20 + return self.key
  21 +
  22 +
15 23 class Document(models.Model):
16 24 name = models.TextField()
17 25 source_id = models.CharField(max_length=32)
... ... @@ -43,6 +51,8 @@ class Document(models.Model):
43 51 unk_coverage = models.FloatField(default=0.0)
44 52 parent = models.ForeignKey("self", null=True, default=None, on_delete=models.CASCADE)
45 53 sequence = models.PositiveIntegerField(default=0)
  54 + doc_type = models.ForeignKey(DocumentType, null=True, blank=True, on_delete=models.CASCADE)
  55 +
46 56  
47 57 def delete(self, *args, **kwargs):
48 58 super().delete(*args, **kwargs)
... ...
collector/storage/static/storage/js/document.js
... ... @@ -72,6 +72,10 @@ $(function () {
72 72 $(this).modalForm({formURL: $(this).data("id")});
73 73 });
74 74  
  75 + $(".change-doc-type").each(function () {
  76 + $(this).modalForm({formURL: $(this).data("id")});
  77 + });
  78 +
75 79 $(".add-keyword").each(function () {
76 80 $(this).modalForm({formURL: $(this).data("id")});
77 81 });
... ...
collector/storage/templates/storage/annotation.html
... ... @@ -110,10 +110,19 @@
110 110 <div class="modal-content"></div>
111 111 </div>
112 112 </div>
  113 +
  114 + <div class="text-align-center">
  115 + <p>
  116 + Typ dokumentu: <strong>{{ document.doc_type.name|default_if_none:'nieokreślony' }}</strong>
  117 + <button class="change-doc-type btn btn-success" type="button" style="margin-left: 10px"
  118 + data-id="{% url 'change_doc_type' document.pk %}">Zmień
  119 + </button>
  120 + </p>
  121 + </div>
113 122  
114 123 {% if subdocuments %}
115 124 <div class="info">
116   - <p>Poddokumenty:</p>
  125 + <p>Poddokumenty:</p>
117 126 </div>
118 127  
119 128 <table class="data-table doc-table" id="subdoc-list-table">
... ...
collector/storage/urls.py
... ... @@ -12,6 +12,7 @@ urlpatterns = [
12 12 path('document/edit-details/<int:pk>', login_required(views.DocDetailsEditView.as_view()), name='edit_doc_details'),
13 13 path('document/edit-subdoc-details/<int:pk>', login_required(views.SubDocDetailsEditView.as_view()),
14 14 name='edit_subdoc_details'),
  15 + path('document/change-doc-type/<int:pk>', login_required(views.ChangeDocTypeView.as_view()), name='change_doc_type'),
15 16 path('chunk/add/<str:doc_id>', login_required(views.ChunkAddBetweenView.as_view()), name='add_chunk_between'),
16 17 path('chunk/add/<str:doc_id>/<int:pk>', login_required(views.ChunkAddBetweenView.as_view()),
17 18 name='add_chunk_between'),
... ...
collector/storage/views.py
... ... @@ -22,7 +22,7 @@ from sys import maxsize
22 22 from functools import partial, wraps
23 23  
24 24 from .forms import ChunkForm, ParticipantForm, SubchunkForm, MetadataForm, DocDetailsForm, KeywordForm, DocSplitForm, \
25   - ChunkMoveForm, SubDocDetailsForm, AuthorForm, ChunkMergeForm, ChunkSplitForm
  25 + ChunkMoveForm, SubDocDetailsForm, AuthorForm, ChunkMergeForm, ChunkSplitForm, DocTypeForm
26 26 from .models import Chunk, Document, Participant, Metadata, Keyword, Annotation
27 27 from projects.ppc.models import Utterance
28 28 from pipeline.models import ProcessingStatus
... ... @@ -638,12 +638,14 @@ class AnnotationView(View):
638 638 chunks = doc.chunks.order_by('sequence')
639 639 keywords = doc.keywords.all()
640 640 authors = doc.participants.all()
  641 + doc_type_form = DocTypeForm()
641 642 context = {'document': doc,
642 643 'subdocuments': subdocuments,
643 644 'metadata': metadata,
644 645 'chunks': chunks,
645 646 'keywords': keywords,
646   - 'authors': authors}
  647 + 'authors': authors,
  648 + 'doc_type_form': doc_type_form}
647 649 except Document.DoesNotExist:
648 650 pass
649 651  
... ... @@ -1117,6 +1119,27 @@ class DraftListView(ListView):
1117 1119 return context
1118 1120  
1119 1121  
  1122 +class ChangeDocTypeView(UpdateView):
  1123 + model = Document
  1124 + template_name = 'storage/edit.html'
  1125 + form_class = DocTypeForm
  1126 +
  1127 + def form_valid(self, form):
  1128 + if not self.request.is_ajax():
  1129 + form.save(commit=True)
  1130 + return HttpResponseRedirect(self.get_success_url())
  1131 + return render(self.request, self.template_name, {'form': form})
  1132 +
  1133 + def get_context_data(self, **kwargs):
  1134 + context = super().get_context_data(**kwargs)
  1135 + context['title'] = 'Edycja typu dokumentu'
  1136 + context['submit_btn_text'] = 'Zmień'
  1137 + return context
  1138 +
  1139 + def get_success_url(self):
  1140 + return reverse_lazy('annotation', kwargs={'doc_id': self.object.id})
  1141 +
  1142 +
1120 1143 # ****************************** autocomplete ******************************
1121 1144  
1122 1145  
... ...