Commit 69b6f2845f70b1932339c03b85593324a2e36e29

Authored by Marcel Kawski
1 parent fc722e51

Refactor move_chunk view for class-based view and modify MoveChunk form to fit

collector/storage/forms.py
... ... @@ -332,14 +332,13 @@ class DocSplitForm(Form):
332 332 self.fields['chunk_end'].queryset = chunks
333 333  
334 334  
335   -class MoveChunkForm(Form):
336   - target_doc = ModelChoiceField(queryset=None, label="Dokument docelowy")
  335 +class MoveChunkForm(ModelForm):
337 336  
338 337 def __init__(self, *args, **kwargs):
339 338 poss_target_docs = kwargs.pop('poss_target_docs')
340 339 super(MoveChunkForm, self).__init__(*args, **kwargs)
341   - self.fields['target_doc'].label_from_instance = self.label_from_instance
342   - self.fields['target_doc'].queryset = poss_target_docs
  340 + self.fields['document'].label_from_instance = self.label_from_instance
  341 + self.fields['document'].queryset = poss_target_docs
343 342  
344 343 @staticmethod
345 344 def label_from_instance(obj):
... ... @@ -348,6 +347,13 @@ class MoveChunkForm(Form):
348 347 else:
349 348 return f'{obj} (dokument główny)'
350 349  
  350 + class Meta:
  351 + model = Chunk
  352 + fields = ['document']
  353 + labels = {
  354 + 'document': 'Dokument docelowy'
  355 + }
  356 +
351 357  
352 358 class MergeChunkForm(Form):
353 359 target_chunk = ModelChoiceField(queryset=None, label="Połącz z sekcją:")
... ...
collector/storage/urls.py
... ... @@ -17,7 +17,7 @@ urlpatterns = [
17 17 name='add_chunk_between'),
18 18 path('chunk/edit/<int:pk>', login_required(views.ChunkEditView.as_view()), name='edit_chunk'),
19 19 path('chunk/delete/<int:pk>', login_required(views.ChunkDeleteView.as_view()), name='delete_chunk'),
20   - path('chunk/move/<int:pk>', login_required(views.move_chunk), name='move_chunk'),
  20 + path('chunk/move/<int:pk>', login_required(views.MoveChunkView.as_view()), name='move_chunk'),
21 21 path('chunk/merge/<int:pk>/<str:doc_id>', login_required(views.merge_chunk), name='merge_chunk'),
22 22 path('subchunk/add/<int:chunk_pk>', login_required(views.SubchunkAddView.as_view()), name='add_subchunk'),
23 23 path('subchunk/edit/<int:pk>', login_required(views.SubchunkEditView.as_view()), name='edit_subchunk'),
... ...
collector/storage/views.py
... ... @@ -311,33 +311,38 @@ 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  
  316 +class MoveChunkView(UpdateView):
  317 + model = Chunk
  318 + template_name = 'storage/edit.html'
  319 + form_class = MoveChunkForm
315 320  
316   -def move_chunk(request, pk):
317   - chunk = Chunk.objects.get(pk=pk)
318   - document = chunk.document
319   - if document.parent is not None: # subdocument
320   - poss_target_docs = Document.objects.filter(parent=document.parent)
321   - poss_target_docs = poss_target_docs.exclude(id=document.id)
322   - poss_target_docs |= Document.objects.filter(pk=document.parent.pk)
323   - else: # main document
324   - poss_target_docs = Document.objects.filter(parent=document)
325   - if request.method == 'POST':
326   - form = MoveChunkForm(request.POST, poss_target_docs=poss_target_docs)
327   - if form.is_valid():
328   - target_doc = form.cleaned_data['target_doc']
329   - chunk.document = target_doc
330   - chunk = handle_chunks_seq_uniqueness([chunk], target_doc.chunks.all())[0]
331   - chunk.save()
332   - document.changed = True
333   - document.save()
334   - return HttpResponseRedirect(reverse_lazy('annotation', kwargs={'doc_id': target_doc.id}))
335   - return HttpResponseRedirect(reverse_lazy('annotation', kwargs={'doc_id': document.id}))
336   - else:
337   - form = MoveChunkForm(poss_target_docs=poss_target_docs)
338   - return render(request, 'storage/edit.html', {'form': form,
339   - 'title': 'Przenoszenie sekcji',
340   - 'submit_btn_text': 'Przenieś'})
  321 + def form_valid(self, form):
  322 + if not self.request.is_ajax():
  323 + form.save(commit=True)
  324 + return HttpResponseRedirect(self.get_success_url())
  325 + return render(self.request, self.template_name, {'form': form})
  326 +
  327 + def get_context_data(self, **kwargs):
  328 + context = super().get_context_data(**kwargs)
  329 + context['title'] = 'Przenoszenie sekcji'
  330 + context['submit_btn_text'] = 'Przenieś'
  331 + return context
  332 +
  333 + def get_form_kwargs(self):
  334 + kwargs = super(MoveChunkView, self).get_form_kwargs()
  335 + if self.object.document.parent is not None: # subdocument
  336 + poss_target_docs = Document.objects.filter(parent=self.object.document.parent)
  337 + poss_target_docs = poss_target_docs.exclude(id=self.object.document.id)
  338 + poss_target_docs |= Document.objects.filter(pk=self.object.document.parent.pk)
  339 + else: # main document
  340 + poss_target_docs = Document.objects.filter(parent=self.object.document)
  341 + kwargs['poss_target_docs'] = poss_target_docs
  342 + return kwargs
  343 +
  344 + def get_success_url(self):
  345 + return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id}), self.object.pk)
341 346  
342 347  
343 348 def merge_chunk(request, pk, doc_id):
... ...