Commit 029655ae8fad8047740a53e5459a3d3a871489ac

Authored by Marcel Kawski
1 parent 69b6f284

Refactor split_doc view for class-based view and modify DocSplitForm to fit

collector/storage/forms.py
... ... @@ -317,7 +317,7 @@ class KeywordForm(Form):
317 317 label = CharField(max_length=200, label='Etykieta')
318 318  
319 319  
320   -class DocSplitForm(Form):
  320 +class DocSplitForm(ModelForm):
321 321 class ChunkChoiceField(ModelChoiceField):
322 322 def label_from_instance(self, obj):
323 323 return str(obj.sequence)
... ... @@ -332,6 +332,11 @@ class DocSplitForm(Form):
332 332 self.fields['chunk_end'].queryset = chunks
333 333  
334 334  
  335 + class Meta:
  336 + model = Document
  337 + fields = ['chunk_beg', 'chunk_end']
  338 +
  339 +
335 340 class MoveChunkForm(ModelForm):
336 341  
337 342 def __init__(self, *args, **kwargs):
... ...
collector/storage/urls.py
... ... @@ -41,7 +41,7 @@ urlpatterns = [
41 41 path('keyword/add/<str:doc_id>', login_required(views.add_keyword), name='add_keyword'),
42 42 path('keyword/edit/<str:doc_id>/<int:pk>', login_required(views.edit_keyword), name='edit_keyword'),
43 43 path('keyword/delete/<str:doc_id>/<int:pk>', login_required(views.delete_keyword), name='delete_keyword'),
44   - path('split-doc/<str:doc_id>', login_required(views.split_doc), name='split_doc'),
  44 + path('split-doc/<int:pk>', login_required(views.DocSplitView.as_view()), name='split_doc'),
45 45 path('move-subdoc/<str:subdoc_id>/<str:direction>', login_required(views.move_subdoc), name='move_subdoc'),
46 46 path('revert-subdoc-division/<int:pk>/', login_required(views.RevertSubdocDivisionView.as_view()),
47 47 name='revert_subdoc_division'),
... ...
collector/storage/views.py
... ... @@ -311,7 +311,7 @@ class ChunkAddBetweenView(CreateView):
311 311 def get_success_url(self):
312 312 return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.kwargs['doc_id']}),
313 313 self.object.pk)
314   -
  314 +
315 315  
316 316 class MoveChunkView(UpdateView):
317 317 model = Chunk
... ... @@ -855,26 +855,28 @@ class FinishAnnotationView(RedirectView):
855 855 return super().get(request, *args, **kwargs)
856 856  
857 857  
858   -def split_doc(request, doc_id):
859   - document = Document.objects.get(pk=doc_id)
860   - chunks = document.chunks.all().order_by('sequence')
861   - if request.method == 'POST':
862   - form = DocSplitForm(request.POST, chunks=chunks)
863   - if form.is_valid():
  858 +class DocSplitView(UpdateView):
  859 + model = Document
  860 + template_name = 'storage/edit.html'
  861 + form_class = DocSplitForm
  862 +
  863 + def form_valid(self, form):
  864 + if not self.request.is_ajax():
864 865 chunk_beg = form.cleaned_data['chunk_beg'].sequence
865 866 chunk_end = form.cleaned_data['chunk_end'].sequence
  867 + chunks = self.object.chunks.all().order_by('sequence')
866 868 if chunk_end < chunk_beg:
867   - messages.error(request, 'Błąd: Nieprawidłowy zakres sekcji.')
868   - return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': doc_id}))
869   - max_seq_doc = Document.objects.filter(parent=document).order_by('-sequence').first()
  869 + messages.error(self.request, 'Błąd: Nieprawidłowy zakres sekcji.')
  870 + return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': self.object.id}))
  871 + max_seq_doc = Document.objects.filter(parent=self.object).order_by('-sequence').first()
870 872 if max_seq_doc is None:
871 873 sequence = 0
872 874 else:
873 875 sequence = max_seq_doc.sequence
874   - if document.parent is None:
875   - parent = document
  876 + if self.object.parent is None:
  877 + parent = self.object
876 878 else:
877   - parent = document.parent
  879 + parent = self.object.parent
878 880 new_document = Document.objects.create(name=parent.name,
879 881 lang=parent.lang,
880 882 original_lang=parent.original_lang,
... ... @@ -889,15 +891,25 @@ def split_doc(request, doc_id):
889 891 sequence=sequence + 1)
890 892 new_document.chunks.set(chunks.filter(sequence__range=(chunk_beg, chunk_end)))
891 893 new_document.save()
892   - document.chunks.set(chunks.filter(sequence__lt=chunk_beg) | chunks.filter(sequence__gt=chunk_end))
893   - document.changed = True
894   - document.save()
895   - return HttpResponseRedirect(reverse_lazy('annotation', kwargs={'doc_id': doc_id}))
896   - else:
897   - form = DocSplitForm(chunks=chunks)
898   - return render(request, 'storage/edit.html', {'form': form,
899   - 'title': 'Podział dokumentu',
900   - 'submit_btn_text': 'Podziel'})
  894 + self.object.chunks.set(chunks.filter(sequence__lt=chunk_beg) | chunks.filter(sequence__gt=chunk_end))
  895 + self.object.changed = True
  896 + self.object.save()
  897 + return HttpResponseRedirect(self.get_success_url())
  898 + return render(self.request, self.template_name, {'form': form})
  899 +
  900 + def get_context_data(self, **kwargs):
  901 + context = super().get_context_data(**kwargs)
  902 + context['title'] = 'Podział dokumentu'
  903 + context['submit_btn_text'] = 'Podziel'
  904 + return context
  905 +
  906 + def get_form_kwargs(self):
  907 + kwargs = super(DocSplitView, self).get_form_kwargs()
  908 + kwargs['chunks'] = self.object.chunks.all().order_by('sequence')
  909 + return kwargs
  910 +
  911 + def get_success_url(self):
  912 + return reverse_lazy('annotation', kwargs={'doc_id': self.object.id})
901 913  
902 914  
903 915 def move_subdoc(request, subdoc_id, direction):
... ...