def get_likes(post_id): post = Post.query.filter(Post.id == post_id).first() if not post: abort(404) return json_dict(likes_count=post.likes.count(), dislikes_count=post.dislikes.count())
def start_thread(): form = ThreadForm() if not form.validate_on_submit(): abort(400) thread = Thread(form.subject.data) post = create_post(thread, form) if Thread.query.count() >= PAGES * THREADS_PER_PAGE: last_thread = Thread.query.order_by(Thread.bump).first() last_thread.posts.delete() db.session.delete(last_thread) db.session.add(thread) db.session.add(post) db.session.commit() return json_dict(thread_id=thread.id)
def post_message(): form = PostForm() if not form.validate_on_submit(): abort(400) thread = Thread.query.filter(Thread.id == form.thread.data).first() if not thread: abort(404) post = create_post(thread, form) if thread.posts.count() < BUMP_LIMIT: thread.bump = post.time db.session.add(thread) db.session.add(post) db.session.commit() return json_dict(post_id=post.id)
def do_like_or_dislike(post_id, factory, ret): ip_addr = request.environ.get('REMOTE_ADDR') post = Post.query.filter(Post.id == post_id).first() if not post: abort(404) like = factory(post_id, ip_addr) db.session.add(like) try: db.session.commit() except (IntegrityError, InvalidRequestError): db.session.rollback() like = ret(post).filter(factory.ip_address == ip_addr).scalar() db.session.delete(like) db.session.commit() return json_dict(count=ret(post).count())
def accept_visitor(): ip, visitor = get_current_visitor() if ip is None: return if visitor is None: visitor = Visitor(ip, request.path) db.session.add(visitor) db.session.commit() return if visitor.last_visit.date() < datetime.today().date(): visitor.visits_today = 1 visitor.views_today = 1 db.session.add(visitor) db.session.commit() if is_too_fast(visitor): return json_dict(content="Server overload! You're clicking too fast!", title="Fast")