Commit d6e74585e8d67973f9799f988bc6743538604429
1 parent
d283781e
Add changing neighbouring chunks sequence when moving
Showing
1 changed file
with
25 additions
and
6 deletions
collector/storage/forms.py
... | ... | @@ -13,16 +13,14 @@ class ChunkForm(ModelForm): |
13 | 13 | def __init__(self, doc_id=None, pk=None, *args, **kwargs): |
14 | 14 | super(ChunkForm, self).__init__(*args, **kwargs) |
15 | 15 | self.document_id = doc_id |
16 | - if pk is not None: | |
16 | + self.old_sequence = self.instance.sequence | |
17 | + if pk is not None: # adding a new chunk | |
17 | 18 | self.prev_chunk = Chunk.objects.get(pk=pk) |
18 | - prev_chunk = Chunk.objects.get(pk=self.prev_chunk.pk) | |
19 | - self.fields['sequence'].initial = prev_chunk.sequence + 1 | |
19 | + self.fields['sequence'].initial = self.prev_chunk.sequence + 1 | |
20 | 20 | else: |
21 | 21 | self.prev_chunk = None |
22 | 22 | self.fields['sequence'].initial = 0 |
23 | 23 | |
24 | - self.fields['sequence'].widget.attrs['readonly'] = True | |
25 | - | |
26 | 24 | def save(self, commit=True): |
27 | 25 | if self.document_id: |
28 | 26 | if self.prev_chunk is not None: |
... | ... | @@ -30,7 +28,7 @@ class ChunkForm(ModelForm): |
30 | 28 | filter(sequence__gt=self.prev_chunk.sequence) |
31 | 29 | else: # adding a chunk at the beginning of a list of chunks |
32 | 30 | next_chunks = Document.objects.get(id=self.document_id).chunks.order_by('sequence') |
33 | - if next_chunks.first() is not None and next_chunks.first().sequence - self.cleaned_data['sequence'] == 0: | |
31 | + if next_chunks.first() is not None and next_chunks.first().sequence == self.cleaned_data['sequence']: | |
34 | 32 | for chunk in next_chunks: |
35 | 33 | chunk.sequence += 1 |
36 | 34 | chunk.save() |
... | ... | @@ -38,6 +36,27 @@ class ChunkForm(ModelForm): |
38 | 36 | sequence=self.cleaned_data['sequence'], |
39 | 37 | text=self.cleaned_data['text']) |
40 | 38 | else: |
39 | + old_chunk_seq = self.old_sequence | |
40 | + new_chunk_seq = self.cleaned_data['sequence'] | |
41 | + try: | |
42 | + doc = Document.objects.get(id=self.instance.document.id) | |
43 | + doc.chunks.get(sequence=new_chunk_seq) | |
44 | + if old_chunk_seq > new_chunk_seq: | |
45 | + chunks = doc.chunks.filter(sequence__gte=new_chunk_seq, | |
46 | + sequence__lt=old_chunk_seq) | |
47 | + if chunks is not None: | |
48 | + for chunk in chunks: | |
49 | + chunk.sequence += 1 | |
50 | + chunk.save() | |
51 | + elif old_chunk_seq < new_chunk_seq: | |
52 | + chunks = doc.chunks.filter(sequence__gt=old_chunk_seq, | |
53 | + sequence__lte=new_chunk_seq) | |
54 | + if chunks is not None: | |
55 | + for chunk in chunks: | |
56 | + chunk.sequence -= 1 | |
57 | + chunk.save() | |
58 | + except Chunk.DoesNotExist: | |
59 | + pass | |
41 | 60 | chunk = super(ChunkForm, self).save(commit) |
42 | 61 | |
43 | 62 | chunk.document.changed = True |
... | ... |