def delete_post(id): post = Post.get_or_none(Post.id == id) if post is not None: if post.user.id == g.user.id: with db.atomic() as tx: try: deleted_post = post q = Post.delete().where(Post.id == post.id) q.execute() return respond(deleted_post.to_dict(), 201) except Exception as e: return respond_error(str(e), 500) else: return respond_error("UNAUTHORIZED USER", 404) else: return respond_error("POST NOT FOUND", 404)
def delete_comment(id): user = g.user comment = Comment.get_or_none(Comment.id == id) if comment is not None: if user.id == comment.user.id: with db.atomic() as tx: try: deleted_comment = comment q = Comment.delete().where(Comment.id == comment.id) q.execute() return respond(deleted_comment.to_dict(), 201) except Exception as e: return respond_error(str(e), 404) else: return respond_error("UNAUTHORIZED USER", 404) else: return respond_error("COMMENT NOT FOUND", 404)
def delete_review(id): user = g.user review = Review.get_or_none(Review.id == id) if review is not None: if user.id == review.user.id: with db.atomic() as tx: try: deleted_review = review q = Review.delete().where(Review.id == review.id) q.execute() return respond(deleted_review.to_dict(), 201) except Exception as e: return respond_error(str(e), 404) else: return respond_error("UNAUTHORIZED USER", 404) else: return respond_error("REVIEW NOT FOUND", 404)
def create_comment(): user = g.user data = request.json comment_text = data['comment'] pub_at = data['pub_at'] post_id = data['post'] post = Post.get_or_none(Post.id == post_id) if post is not None: comment = Comment(comment=comment_text, user=user.id, post=post.id, pub_at=pub_at) with db.atomic() as tx: try: comment.save() return respond(comment.to_dict(), 201) except Exception as e: tx.rollback() return respond_error(str(e), 500) else: return respond_error("No post found!", 404)
def _create_post(): body = request.json print(body) title = body['title'] content = body['content'] pub_at = dt.parse(body['pub_at']) # tags = json.loads(json.dumps(body['tags'])) #images = json.loads(json.dumps(body['images'])) #print(body['images']) p = Post(title=title, content=content, images=body['images'],pub_at=pub_at, user=g.user.id) with db.atomic() as tx: try: p.save() return respond(p.to_dict(), 201) except Exception as e: print(e) return respond_error(str(e), 500)
def create_review(): user = g.user data = request.json comment_text = data['review'] pub_at = dt.parse(data['pub_at']) stars = data['stars'] lawyer_id = data['lawyer'] lawyer = User.get_or_none(User.id == lawyer_id) if lawyer is not None: review = Review(review=comment_text, user=g.user.id, lawyer=lawyer_id, pub_at=pub_at, stars=stars) with db.atomic() as tx: try: review.save() return respond(review.to_dict(), 201) except Exception as e: print(e) tx.rollback() return respond_error(str(e), 500) else: return respond_error("No lawyer found!", 404)
def register_user(): body = request.json print(body, flush=True) name = body['name'] email = body['email'] username = body['username'] profile_picture = body['profile_picture'] password = body['password'] user_type = body['user_type'] about = body["about"] print(password, flush=True) password_encoded = password.encode('utf-8') hashed_password = bcrypt.hashpw(password_encoded, bcrypt.gensalt(12)) user = User(name=name, email=email, username=username, password=hashed_password, profile_picture=profile_picture, user_type=user_type) with db.atomic() as tx: try: user.save() return respond(user.to_dict(), 201) except Exception as e: return respond_error(str(e), 500)
def _create_post(): body = request.json title = body['title'] description = body['description'] pub_at = dt.parse(body['pub_at']) client = body['client'] user = User.get_or_none(User.email == client) if user is not None: c = Cases(title=title, description=description, verified=0, pub_at=pub_at, lawyer=g.user.id, user=user) with db.atomic() as tx: try: c.save() return respond(c.to_dict(), 201) except Exception as e: return respond_error(str(e), 500)
case = Cases.select() case = [c.to_dict() for c in case] return respond(case, 201) @case_bp.route('/<id>', methods=['GET']) @check_auth def _get_by_id(id): case = Cases.get_by_id(id) return respond(case.to_dict(), 201) @case_bp.route('/<id>', methods=['delete']) @check_auth def delete_Case(id): case = Cases.get_or_none(Cases.id == id) if case is not None: if case.user.id == g.user.id or case.layer.id == g.user.id: with db.atomic() as tx: try: deleted_case = case q = Cases.delete().where(Cases.id == case.id) q.execute() return respond(deleted_case.to_dict(), 201) except Exception as e: return respond_error(str(e), 500) else: return respond_error("UNAUTHORIZED USER", 404) else: return respond_error("CASE NOT FOUND", 404)