def posts_split(request, topic_id): topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) if request.method == 'POST': post_ids = request.POST.getlist('posts_selected') else: post_ids = request.GET.getlist('posts_selected') if not post_ids: messages.error(request, "You haven't checked any post.") return redirect(topic.get_absolute_url()) if topic.first_post.id in post_ids: messages.error(request, "You cannot split out topic's first post.") return redirect(topic.get_absolute_url()) if request.method == 'POST': if not 'cancel' in request.POST and 'confirmation' in request.POST: posts = Post.objects.filter(pk__in=post_ids, topic=topic) \ .order_by('id') topic.post_count = F('post_count') - len(posts) new_topic = Topic(author=posts[0].author, title=form.cleaned_data['new_title'], forum=topic.forum, post_count=len(posts), first_post=post[0]) new_topic.save() posts.update(topic=new_topic) topic.save() messages.success(request, "Posts have been splitted.") return redirect(topic.get_absolute_url()) else: form = SplitPostsForm() return {'topic': topic, 'form': form, 'posts_selected': post_ids}
def topic_new(request, forum_id): forum = get_object_or_404(Forum, pk=forum_id) if not can_post_topic(request.user, forum): messages.error(request, "You are not allowed to to post new topics \ on this forum.") return redirect(forum.get_absolute_url()) if request.method == 'POST': form = TopicForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] topic = Topic(title=title, author=request.user, forum=forum) topic.save() post = Post(topic=topic, author=request.user, author_ip=request.META['REMOTE_ADDR'], content=content) post.save() messages.success(request, "Your topic has been saved.") return redirect(topic.get_absolute_url()) else: form = TopicForm() return {'forum': forum, 'form': form}
def createNewTopic (request) : if request.user.is_authenticated : try : titre = request.POST.get('titre') message = request.POST.get('message') categorie = request.POST.get('categorie') except : return HttpResponse('http://' + request.get_host()) date = date.today() username = request.user.username Topic(titre = titre, message = message, categorie = categorie, username = username, datetimecreation = date).save() return HttpResponse('http://' + request.get_host() + '/forum/')
def form_valid(self, form): topic_name = form.cleaned_data['topic'] post_body = form.cleaned_data['message'] user = self.request.user topic = Topic(forum=self.forum, name=topic_name) topic.save() post = Post(topic=topic, body=post_body, user=user) post.save() topic.last_post = post topic.save() self.success_url = reverse('forums:topic', args=[topic.id]) return super(TopicCreateView, self).form_valid(form)
def form_valid(self, form): topic_name = form.cleaned_data['topic'] post_body = form.cleaned_data['message'] user = UserProfile.objects.get(user=User.objects.get(username=self.request.user.username)) topic = Topic(forum=self.forum, name=topic_name, counter=0) topic.save() post = Post(topic=topic, body=post_body, user=user) post.save() topic.last_post = post topic.save() users = User.objects.all() for u in users: view = View(user=u, topic=topic, visited=False) view.save() self.success_url = ('/forums/topic/' + str(topic.id) + '/page1') return super(TopicCreateView, self).form_valid(form)