def newThread(request): langCode = request.build_absolute_uri().split("/")[-4] if not request.session.has_key('username'): return redirect('/' + langCode + '/login') if request.method == 'POST': thread_form = ThreadForm(request.POST) post_form = PostForm(request.POST) if thread_form.is_valid() and post_form.is_valid(): thread = Thread() thread.title = thread_form.cleaned_data["title"] thread.description = thread_form.cleaned_data["description"] thread.save() post = Post() post.content = post_form.cleaned_data["content"] post.date = timezone.now() post.user = User.objects.get(username=request.session["username"]) post.positionInThread = 0 post.thread = thread post.save() thread.originalPost = post thread.save() return redirect("/" + langCode + "/forum/thread/" + str(thread.pk)) else: # GET return render(request, 'forum/newthread.html', { "thread_form": ThreadForm(), "post_form": PostForm() })
def post_bot_message(package: Package, title: str, message: str): system_user = get_system_user() thread = package.threads.filter_by(author=system_user).first() if not thread: thread = Thread() thread.package = package thread.title = "Bot messages for {}".format(package.title) thread.author = system_user thread.private = True thread.watchers.append(package.author) db.session.add(thread) db.session.flush() reply = ThreadReply() reply.thread = thread reply.author = system_user reply.comment = "**{}**\n\n{}".format(title, message) db.session.add(reply) addNotification(thread.watchers, system_user, NotificationType.BOT, title, thread.getViewURL(), thread.package) thread.replies.append(reply)
def review(package): if current_user in package.maintainers: flash("You can't review your own package!", "danger") return redirect(package.getDetailsURL()) review = PackageReview.query.filter_by(package=package, author=current_user).first() form = ReviewForm(formdata=request.form, obj=review) # Set default values if request.method == "GET" and review: form.title.data = review.thread.title form.recommends.data = "yes" if review.recommends else "no" form.comment.data = review.thread.replies[0].comment # Validate and submit elif request.method == "POST" and form.validate(): was_new = False if not review: was_new = True review = PackageReview() review.package = package review.author = current_user db.session.add(review) review.recommends = form.recommends.data == "yes" thread = review.thread if not thread: thread = Thread() thread.author = current_user thread.private = False thread.package = package thread.review = review db.session.add(thread) thread.watchers.append(current_user) reply = ThreadReply() reply.thread = thread reply.author = current_user reply.comment = form.comment.data db.session.add(reply) thread.replies.append(reply) else: reply = thread.replies[0] reply.comment = form.comment.data thread.title = form.title.data db.session.commit() package.recalcScore() notif_msg = None if was_new: notif_msg = "New review '{}'".format(form.title.data) else: notif_msg = "Updated review '{}'".format(form.title.data) addNotification(package.maintainers, current_user, notif_msg, url_for("threads.view", id=thread.id), package) db.session.commit() return redirect(package.getDetailsURL()) return render_template("packages/review_create_edit.html", \ form=form, package=package, review=review)