def save(self, commit=True): """ Recalculate the number of posts and topics in the source and destination forums when a topic is moved """ topic = super(TopicMoveForm, self).save(commit) inform_new_topic(topic) return topic
def save(self): # Create the new topic new_topic = Topic(forum=self.topic.forum, name=self.cleaned_data["name"]) new_topic.save() # Move posts to the new topic self.topic.posts.filter(pk__in=self.cleaned_data["posts"]).update(topic=new_topic) # Update the new topic stats new_topic.first_post = new_topic.posts.order_by("date")[0] new_topic.update_posts_count() new_topic.update_last_post() new_topic.save() # Update the old topic stats self.topic.first_post = self.topic.posts.order_by("date")[0] self.topic.update_posts_count() self.topic.update_last_post() self.topic.save() # Inform subscribed users about the new topic inform_new_topic(new_topic) return new_topic
def topic_add(request, forum_id): """ Adds a new topic """ forum = get_object_or_404(Forum, id=forum_id) group, perms = get_group_perms_or_404(request.user, forum) if perms.can_add_topic(): PollFormset = inlineformset_factory(Topic, Poll, fk_name='topic', form=PollForm, max_num=10, extra=10) if request.method == 'POST': topic_form = TopicForm(request.POST, prefix='topic_form') post_form = PostForm(request.POST, prefix='post_form') poll_formset = PollFormset(request.POST) if topic_form.is_valid() and post_form.is_valid() and poll_formset.is_valid(): profile = request.user.get_profile() ip_address = request.META.get('REMOTE_ADDR', None) topic = topic_form.save(commit=False) topic.forum = forum topic.save() post = post_form.save(commit=False) post.topic = topic post.profile = profile post.ip_address = ip_address post.save() topic.first_post = post topic.save() # Save poll poll_formset.instance = topic for form in poll_formset.forms: if hasattr(form, 'cleaned_data') and form.cleaned_data: form.cleaned_data['topic'] = topic poll_formset.save() # Save poll choices for form in poll_formset.forms: form.save_choices() # Inform subscribed users about new topic inform_new_topic(topic) # Auto subscribe user to his topic do_subscribe(profile, topic) return HttpResponseRedirect(topic.get_absolute_url()) else: topic_form = TopicForm(prefix='topic_form') post_form = PostForm(prefix='post_form') poll_formset = PollFormset() else: raise Http404 return { 'forum': forum, 'topic_form': topic_form, 'post_form': post_form, 'poll_formset': poll_formset, }