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 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 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 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 view(slug, id): topic = g.topic user = g.user if topic.update_view_count(ip=request.remote_addr, user=user, commit=True): cache.update_topic(topic.id, topic) cache.update_sorted_topics(topic, 'view_count') topic_data = topic.json_data() topic_data['comments'] = topic.get_comments(json=True) topic_data['user_vote'] = Topic.check_vote(user, topic.id) topic_data['user_comment'] = Topic.check_comment(user, topic.id) context = { 'js_module': 'view_topic', 'style': 'view_topic', 'topic': topic_data, 'sub_title': topic.title } return render_template('topics/view.html', **context)
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.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(): 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()