def create(): data = request.get_json() form = TopicForm(**data) if form.validate(): form_data = form.data form_data['ip'] = request.remote_addr try: topic = g.user.create_topic(**form_data) alert = dict( type='success', messages= _("Your topic has been created successfully. You will be redirected to it shortly" )) redirect = topic.get_url('view') cache.update_sorted_topics(topic, 'date_created') return jsonify({ "data": topic.json_data(), "alert": alert, "redirect": redirect }) 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(): topic = g.topic data = request.get_json() up = data.get('up', True) try: vote = topic.vote(user=g.user, ip=request.remote_addr, up=up) if vote.id: # this is an updated vote if vote.changed: points = topic.update_points(up=up, points=2) topic.user.update_points(up=up, points=6) else: points = topic.points else: # this is a new vote points = topic.update_points(up) user_points = 1 if up: user_points = 5 topic.user.update_points(up=up, points=user_points) db_session.commit() data = {"points": points} cache.update_topic(topic.id, topic) cache.update_sorted_topics(topic, 'points') return jsonify({"data": data}) except Exception, e: logging.error(e) db_session.rollback() return json_error_database()
def comment_create(): data = request.get_json() topic = g.topic form = CommentForm(**data) if form.validate(): form_data = form.data form_data['user'] = g.user form_data['ip'] = request.remote_addr try: comment = topic.create_comment(**form_data) topic.update_comment_count() topic.update_user_comment_count(user_id=comment.user_id) db_session.commit() cache.update_topic(topic.id, topic) cache.update_sorted_topics(topic, '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 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 vote(): topic = g.topic data = request.get_json() up = data.get('up', True) try: vote = topic.vote(user=g.user, ip=request.remote_addr, up=up) if vote.id: # this is an updated vote if vote.changed: points = topic.update_points(up=up, points=2) topic.user.update_points(up=up, points=6) else: points = topic.points else: # this is a new vote points = topic.update_points(up) user_points = 1 if up: user_points = 5 topic.user.update_points(up=up, points=user_points) db_session.commit() data = { "points": points } cache.update_topic(topic.id, topic) cache.update_sorted_topics(topic, 'points') return jsonify({"data": data}) except Exception, e: logging.error(e) db_session.rollback() return json_error_database()
def create(): data = request.get_json() form = TopicForm(**data) if form.validate(): form_data = form.data form_data['ip'] = request.remote_addr try: topic = g.user.create_topic(**form_data) alert = dict( type='success', messages=_("Your topic has been created successfully. You will be redirected to it shortly") ) redirect = topic.get_url('view') cache.update_sorted_topics(topic, 'date_created') return jsonify({"data": topic.json_data(), "alert": alert, "redirect": redirect}) 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 answer_create(): user = g.user question = g.question ip = request.remote_addr content = request.get_json().get('content') form = AnswerForm(content=content) if form.validate(): form_data = form.data form_data['ip'] = ip form_data['user'] = user try: answer = question.create_answer(**form_data) question.update_answer_count() db_session.commit() return jsonify({"data": answer.json_data()}) except Exception, e: logging.error(e) db_session.rollback() return json_error_database()
def unvote(): topic = g.topic vote = topic.unvote(g.user) if vote: try: topic.update_points(up=not vote.up) user_points = 1 if vote.up: user_points = 5 topic.user.update_points(up=not vote.up, points=user_points) db_session.commit() data = {"points": topic.points} cache.update_topic(topic.id, topic) cache.update_sorted_topics(topic, 'points') return jsonify({"data": data}) except Exception, e: logging.error(e) return json_error_database()
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 comment_delete(): data = request.get_json() id = data.get('id') comment = Comment.find_by_pk(id) if comment: try: comment.topic.update_comment_count(offset=-1) comment.topic.update_user_comment_count(offset=-1, user_id=comment.user_id) comment.delete() db_session.commit() cache.update_topic(comment.topic.id, comment.topic) cache.update_sorted_topics(comment.topic, '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 unvote(): topic = g.topic vote = topic.unvote(g.user) if vote: try: topic.update_points(up= not vote.up) user_points = 1 if vote.up: user_points = 5 topic.user.update_points(up= not vote.up, points=user_points) db_session.commit() data = { "points": topic.points } cache.update_topic(topic.id, topic) cache.update_sorted_topics(topic, 'points') return jsonify({"data": data}) except Exception, e: logging.error(e) return json_error_database()