Commit db7bc5dd6d4baf614389b23f0a7afa2a0664fd2c
1 parent
029655ae
Refactor merge_chunk view for class-based view and modify MergeChunkForm to fit
Showing
3 changed files
with
42 additions
and
31 deletions
collector/storage/forms.py
... | ... | @@ -331,17 +331,16 @@ class DocSplitForm(ModelForm): |
331 | 331 | self.fields['chunk_beg'].queryset = chunks |
332 | 332 | self.fields['chunk_end'].queryset = chunks |
333 | 333 | |
334 | - | |
335 | 334 | class Meta: |
336 | 335 | model = Document |
337 | 336 | fields = ['chunk_beg', 'chunk_end'] |
338 | 337 | |
339 | 338 | |
340 | -class MoveChunkForm(ModelForm): | |
339 | +class ChunkMoveForm(ModelForm): | |
341 | 340 | |
342 | 341 | def __init__(self, *args, **kwargs): |
343 | 342 | poss_target_docs = kwargs.pop('poss_target_docs') |
344 | - super(MoveChunkForm, self).__init__(*args, **kwargs) | |
343 | + super(ChunkMoveForm, self).__init__(*args, **kwargs) | |
345 | 344 | self.fields['document'].label_from_instance = self.label_from_instance |
346 | 345 | self.fields['document'].queryset = poss_target_docs |
347 | 346 | |
... | ... | @@ -360,17 +359,20 @@ class MoveChunkForm(ModelForm): |
360 | 359 | } |
361 | 360 | |
362 | 361 | |
363 | -class MergeChunkForm(Form): | |
362 | +class ChunkMergeForm(ModelForm): | |
364 | 363 | target_chunk = ModelChoiceField(queryset=None, label="Połącz z sekcją:") |
365 | 364 | |
366 | 365 | def __init__(self, *args, **kwargs): |
367 | 366 | poss_target_chunks = kwargs.pop('poss_target_chunks') |
368 | - super(MergeChunkForm, self).__init__(*args, **kwargs) | |
367 | + super(ChunkMergeForm, self).__init__(*args, **kwargs) | |
369 | 368 | self.fields['target_chunk'].label_from_instance = self.label_from_instance |
370 | 369 | self.fields['target_chunk'].queryset = poss_target_chunks |
371 | - self.fields['target_chunk'].widget.attrs['required'] = 'required' | |
372 | 370 | |
373 | 371 | @staticmethod |
374 | 372 | def label_from_instance(obj): |
375 | 373 | if obj: |
376 | 374 | return obj.sequence |
375 | + | |
376 | + class Meta: | |
377 | + model = Chunk | |
378 | + fields = ['target_chunk'] | |
... | ... |
collector/storage/urls.py
... | ... | @@ -17,8 +17,8 @@ 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.MoveChunkView.as_view()), name='move_chunk'), | |
21 | - path('chunk/merge/<int:pk>/<str:doc_id>', login_required(views.merge_chunk), name='merge_chunk'), | |
20 | + path('chunk/move/<int:pk>', login_required(views.ChunkMoveView.as_view()), name='move_chunk'), | |
21 | + path('chunk/merge/<int:pk>/<str:doc_id>', login_required(views.ChunkMergeView.as_view()), 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'), |
24 | 24 | path('subchunk/delete/<int:pk>', login_required(views.SubchunkDeleteView.as_view()), name='delete_subchunk'), |
... | ... |
collector/storage/views.py
... | ... | @@ -19,7 +19,7 @@ from keras import backend |
19 | 19 | from sys import maxsize |
20 | 20 | |
21 | 21 | from .forms import ChunkForm, ParticipantForm, SubchunkForm, MetadataForm, DocDetailsForm, KeywordForm, DocSplitForm, \ |
22 | - MoveChunkForm, SubDocDetailsForm, AuthorForm, MergeChunkForm | |
22 | + ChunkMoveForm, SubDocDetailsForm, AuthorForm, ChunkMergeForm | |
23 | 23 | from .models import Chunk, Document, Participant, Metadata, Keyword, Annotation |
24 | 24 | from projects.ppc.models import Utterance |
25 | 25 | from pipeline.models import ProcessingStatus |
... | ... | @@ -313,10 +313,10 @@ class ChunkAddBetweenView(CreateView): |
313 | 313 | self.object.pk) |
314 | 314 | |
315 | 315 | |
316 | -class MoveChunkView(UpdateView): | |
316 | +class ChunkMoveView(UpdateView): | |
317 | 317 | model = Chunk |
318 | 318 | template_name = 'storage/edit.html' |
319 | - form_class = MoveChunkForm | |
319 | + form_class = ChunkMoveForm | |
320 | 320 | |
321 | 321 | def form_valid(self, form): |
322 | 322 | if not self.request.is_ajax(): |
... | ... | @@ -331,7 +331,7 @@ class MoveChunkView(UpdateView): |
331 | 331 | return context |
332 | 332 | |
333 | 333 | def get_form_kwargs(self): |
334 | - kwargs = super(MoveChunkView, self).get_form_kwargs() | |
334 | + kwargs = super(ChunkMoveView, self).get_form_kwargs() | |
335 | 335 | if self.object.document.parent is not None: # subdocument |
336 | 336 | poss_target_docs = Document.objects.filter(parent=self.object.document.parent) |
337 | 337 | poss_target_docs = poss_target_docs.exclude(id=self.object.document.id) |
... | ... | @@ -345,27 +345,36 @@ class MoveChunkView(UpdateView): |
345 | 345 | return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id}), self.object.pk) |
346 | 346 | |
347 | 347 | |
348 | -def merge_chunk(request, pk, doc_id): | |
349 | - try: | |
350 | - chunk = Chunk.objects.get(pk=pk) | |
351 | - document = chunk.document | |
352 | - poss_target_chunks = document.chunks.all() | |
353 | - poss_target_chunks = poss_target_chunks.exclude(id=chunk.id) | |
354 | - form = MergeChunkForm(request.POST, poss_target_chunks=poss_target_chunks) | |
355 | - except Chunk.DoesNotExist: | |
356 | - form, chunk, poss_target_chunks, document = None, None, None, None | |
357 | - if request.method == 'POST': | |
358 | - if form is not None and form.is_valid(): | |
348 | +class ChunkMergeView(UpdateView): | |
349 | + model = Chunk | |
350 | + template_name = 'storage/edit.html' | |
351 | + form_class = ChunkMergeForm | |
352 | + | |
353 | + def form_valid(self, form): | |
354 | + if not self.request.is_ajax(): | |
359 | 355 | target_chunk = form.cleaned_data['target_chunk'] |
360 | - target_chunk.text += f' {chunk.text}' | |
356 | + target_chunk.text += f' {self.object.text}' | |
361 | 357 | target_chunk.save() |
362 | - chunk.delete() | |
363 | - return HttpResponseRedirect(reverse_lazy('annotation', kwargs={'doc_id': doc_id})) | |
364 | - else: | |
365 | - form = MergeChunkForm(poss_target_chunks=poss_target_chunks) | |
366 | - return render(request, 'storage/edit.html', {'form': form, | |
367 | - 'title': 'Łączenie sekcji', | |
368 | - 'submit_btn_text': 'Połącz'}) | |
358 | + self.object.delete() | |
359 | + return HttpResponseRedirect(self.get_success_url(target_chunk)) | |
360 | + return render(self.request, self.template_name, {'form': form}) | |
361 | + | |
362 | + def get_context_data(self, **kwargs): | |
363 | + context = super().get_context_data(**kwargs) | |
364 | + context['title'] = 'Łączenie sekcji' | |
365 | + context['submit_btn_text'] = 'Połącz' | |
366 | + return context | |
367 | + | |
368 | + def get_form_kwargs(self): | |
369 | + kwargs = super(ChunkMergeView, self).get_form_kwargs() | |
370 | + poss_target_chunks = self.object.document.chunks.all() | |
371 | + poss_target_chunks = poss_target_chunks.exclude(id=self.object.id) | |
372 | + kwargs['poss_target_chunks'] = poss_target_chunks | |
373 | + return kwargs | |
374 | + | |
375 | + def get_success_url(self, target_chunk): | |
376 | + return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': target_chunk.document.id}), | |
377 | + target_chunk.pk) | |
369 | 378 | |
370 | 379 | |
371 | 380 | class SubchunkAddView(CreateView): |
... | ... |