def action_merge(self, ids): if len(ids) < 2: raise ValidationError( _("You have to pick two or more threads to merge.")) threads = [] for thread in self.threads: if thread.pk in ids: threads.append(thread) if self.request.POST.get('origin') == 'merge_form': form = MergeThreadsForm(self.request.POST, request=self.request, threads=threads) if form.is_valid(): new_thread = Thread.objects.create( forum=form.cleaned_data['new_forum'], name=form.cleaned_data['thread_name'], slug=slugify(form.cleaned_data['thread_name']), start=timezone.now(), last=timezone.now()) merged = [] for thread in reversed(threads): merged.append(thread.pk) thread.merge_with(new_thread) Thread.objects.filter(id__in=merged).delete() new_thread.sync() new_thread.save(force_update=True) new_thread.update_current_dates() self.forum.sync() self.forum.save(force_update=True) if form.cleaned_data['new_forum'].pk != self.forum.pk: form.cleaned_data['new_forum'].sync() form.cleaned_data['new_forum'].save(force_update=True) self.request.messages.set_flash( Message( _('Selected threads have been merged into new one.')), 'success', 'threads') return None self.message = Message(form.non_field_errors()[0], 'error') else: form = MergeThreadsForm(request=self.request, threads=threads) warning = None lookback = threads[0].last_post_id for thread in threads[1:]: if thread.start_post_id < lookback: warning = Message( _("Warning: Posting times in one or more of threads that you are going to merge are overlapping. This may result in disturbed flow of merged thread." ), 'warning') break else: lookback = thread.last_post_id return self.request.theme.render_to_response( ('%ss/merge.html' % self.type_prefix), { 'type_prefix': self.type_prefix, 'message': self.message, 'warning': warning, 'forum': self.forum, 'parents': self.parents, 'threads': threads, 'form': FormLayout(form), }, context_instance=RequestContext(self.request))
def action_merge(self, ids): if len(ids) < 2: raise ValidationError( _("You have to pick two or more threads to merge.")) threads = [] for thread in self.threads: if thread.pk in ids: threads.append(thread) if self.request.POST.get('origin') == 'merge_form': form = MergeThreadsForm(self.request.POST, request=self.request, threads=threads) if form.is_valid(): new_thread = Thread.objects.create( forum=form.cleaned_data['new_forum'], name=form.cleaned_data['thread_name'], slug=slugify(form.cleaned_data['thread_name']), start=timezone.now(), last=timezone.now()) merged = [] for thread in reversed(threads): merged.append(thread.pk) thread.merge_with(new_thread) new_thread.sync() new_thread.save(force_update=True) new_thread.update_current_dates() poll_action = form.cleaned_data.get('final_poll', 'no') if poll_action == 'no': for thread in threads: if thread.has_poll: thread.poll.move_to(forum=new_thread.forum, thread=new_thread) new_thread.has_poll = True new_thread.save(force_update=True) break else: if poll_action > 0: for thread in threads: if thread.pk == poll_action: thread.poll.move_to(forum=new_thread.forum, thread=new_thread) new_thread.has_poll = True new_thread.save(force_update=True) break for thread in Thread.objects.filter(id__in=merged): thread.delete() self.forum.sync() self.forum.save(force_update=True) if form.cleaned_data['new_forum'].pk != self.forum.pk: form.cleaned_data['new_forum'].sync() form.cleaned_data['new_forum'].save(force_update=True) messages.success( self.request, _('Selected threads have been merged into new one.'), 'threads') return None self.message = Message(form.non_field_errors()[0], messages.ERROR) else: form = MergeThreadsForm(request=self.request, threads=threads) warning = None lookback = threads[-1] for thread in reversed(threads[:-1]): if thread.start_post_id < lookback.last_post_id: warning = Message( _("Warning: Posting times in one or more of threads that you are going to merge are overlapping. This may result in disturbed flow of merged thread." ), 'warning') break else: lookback = thread return render_to_response('%ss/merge.html' % self.type_prefix, { 'type_prefix': self.type_prefix, 'search_in': self.search_in, 'message': self.message, 'warning': warning, 'forum': self.forum, 'parents': self.parents, 'threads': threads, 'form': form, }, context_instance=RequestContext( self.request))