def commentsSub(): user_id=request.form["user_id"] company_id=request.form["company_id"] contents=request.form["contents"] if user_id==None or company_id==None or contents==None: abort(404) user=User.query.filter_by(id=user_id).first() company=Company.query.filter_by(id=company_id).first() comments=Comments(contents,time.strftime("%Y-%m-%d %T"),user,company) Comments.add(comments) return redirect(url_for("index.company",id=company_id))
def comment_create(pizza_id): comment = comment_schema.load(request.json) new_comment = Comments() new_comment.comment = comment_fields["comment"] new_comment.user_id = users.user_id new_comment.pizza_id = pizza_id user.comment.append(new_comment) db.session.commit() return jsonify(comment_schema.dump(new_comment))
def seed_db(): from models.Pizza import Pizza from models.User import User from models.Comments import Comments from main import bcrypt from faker import Faker import random faker = Faker() users = [] pizzas = [] #create a user table and add in 5 users to test for i in range(5): user = User() user.email = f"test{i}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") user.user_name = f"test{i}" db.session.add(user) users.append(user) db.session.commit() #create table to contain pizzas - randomlyh created with random text - users cant crete pizza - for i in range(10): pizza = Pizza() pizza.pizza_name = faker.catch_phrase() pizza.description = faker.catch_phrase() pizza.price = 20 pizza.location = faker.catch_phrase() #pizza.user_id = random.choice(users).user_id db.session.add(pizza) pizzas.append(pizza) print(f"{i + 1} pizza record(s) created") db.session.commit() #create a table test for comments - random pizzas assigned comment from random users for i in range(10): comment = Comments() comment.pizza_id = random.choice(pizzas).pizza_id comment.user_id = random.choice(users).user_id comment.comment = faker.catch_phrase() db.session.add(comment) print(f"{i + 1} comments created") db.session.commit() print("Tables seeded")
def create_comment(): userAuthenticated, user = isAuthenticated( flaskRequest.headers['authorization']) if userAuthenticated: request = json.loads(flaskRequest.data) post = Posts.objects(id=request['postId']).first() if not post: return 'No post was found with this id', 400 comment = Comments(content=request['content'], author=user['id'], identifier=f'{uuid4()}', authorName=user['name'], imageUrl=user['imageUrl']) post['comments'].append(comment) post.save() return jsonify({"data": post}) else: return "Token missing or expired. Please login again.", 403
def get(self, adviceID): return Comments.getAdviceComments(self, adviceID)
def delete(self, commentID): return Comments.deleteSingleComment(self, commentID)
def put(self, commentID): return Comments.updateLikesQuantity(self, commentID)
def get(self, commentID): return Comments.getSingleComment(self, commentID)
def get(self): return Comments.getAllComments(self)
def post(self): return Comments.createComment(self)