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)