def comment_create(): data = request.get_json() article = g.article form = CommentForm(**data) if form.validate(): form_data = form.data form_data['user'] = g.user form_data['ip'] = request.remote_addr try: comment = article.create_comment(**form_data) article.update_comment_count() article.update_user_comment_count(user_id=comment.user_id) db_session.commit() cache.update_article(article.id, article) cache.update_sorted_articles(article, 'comment_count') return jsonify({"data": comment.json_data()}) except ModelException, me: db_session.rollback() return json_error(type=me.type, messages=me.message) except Exception, e: logging.error(e) db_session.rollback() return json_error_database()
def vote(): article = g.article data = request.get_json() up = data.get('up', True) try: vote = article.vote(user=g.user, ip=request.remote_addr, up=up) if vote.id: # this is an updated vote if vote.changed: points = article.update_points(up=up, points=2) article.user.update_points(up=up, points=11) else: points = article.points else: # this is a new vote points = article.update_points(up) user_points = 1 if up: user_points = 10 article.user.update_points(up=up, points=user_points) db_session.commit() data = { "points": points } cache.update_article(article.id, article) cache.update_sorted_articles(article, 'points') return jsonify({"data": data}) except Exception, e: logging.error(e) db_session.rollback() return json_error_database()
def vote(): article = g.article data = request.get_json() up = data.get('up', True) try: vote = article.vote(user=g.user, ip=request.remote_addr, up=up) if vote.id: # this is an updated vote if vote.changed: points = article.update_points(up=up, points=2) article.user.update_points(up=up, points=11) else: points = article.points else: # this is a new vote points = article.update_points(up) user_points = 1 if up: user_points = 10 article.user.update_points(up=up, points=user_points) db_session.commit() data = {"points": points} cache.update_article(article.id, article) cache.update_sorted_articles(article, 'points') return jsonify({"data": data}) except Exception, e: logging.error(e) db_session.rollback() return json_error_database()
def approve(): article = g.article data = {} data['ip'] = request.remote_addr data['user'] = g.user data['is_active'] = True article.update(**data) cache.update_article(article.id, article) cache.update_sorted_articles(article, 'comment_count') cache.update_sorted_articles(article, 'date_created') cache.update_sorted_articles(article, 'view_count') cache.update_sorted_articles(article, 'points') # also update the user point article.user.update_points(up=True, points=25, commit=True) return jsonify({"data": article.json_data()})
def view(slug, id): article = g.article user = g.user if article.update_view_count(ip=request.remote_addr, user=user, commit=True): cache.update_article(article.id, article) cache.update_sorted_articles(article, 'view_count') article_data = article.json_data() article_data['comments'] = article.get_comments(json=True) article_data['user_vote'] = Article.check_vote(user, article.id) article_data['user_comment'] = Article.check_comment(user, article.id) context = { 'js_module': 'view_article', 'style': 'view_article', 'article': article_data, 'sub_title': article.title } return render_template('article/view.html', **context)
def comment_delete(): data = request.get_json() id = data.get('id') comment = Comment.find_by_pk(id) if comment: try: comment.article.update_comment_count(offset=-1) comment.article.update_user_comment_count(offset=-1, user_id=comment.user_id) comment.delete() db_session.commit() cache.update_article(comment.article.id, comment.article) cache.update_sorted_articles(comment.article, 'comment_count') return json_data(data) except Exception: db_session.rollback() return json_error_database() else: return json_error_invalid_request()
def unvote(): article = g.article vote = article.unvote(g.user) if vote: try: article.update_points(up=not vote.up) user_points = 1 if vote.up: user_points = 10 article.user.update_points(up=not vote.up, points=user_points) db_session.commit() data = {"points": article.points} cache.update_article(article.id, article) cache.update_sorted_articles(article, 'points') return jsonify({"data": data}) except Exception, e: logging.error(e) return json_error_database()
def update(): article = g.article user = g.user data = request.get_json() form = ArticleForm(**data) if form.validate(): form_data = form.data data = form_data data['ip'] = request.remote_addr data['user'] = user # Make sure that users cant change article status try: if not user.has_permission('approve_article'): del data['is_active'] except Exception: pass article.update(**data) cache.update_article(article.id, article) return jsonify({"data": article.json_data()}) else: return json_error(type="VALIDATION_FAILED", messages=form.errors)
def unvote(): article = g.article vote = article.unvote(g.user) if vote: try: article.update_points(up= not vote.up) user_points = 1 if vote.up: user_points = 10 article.user.update_points(up= not vote.up, points=user_points) db_session.commit() data = { "points": article.points } cache.update_article(article.id, article) cache.update_sorted_articles(article, 'points') return jsonify({"data": data}) except Exception, e: logging.error(e) return json_error_database()