def addsUser(): try: data=request.json hashed_password=generate_password_hash(data[0]['password'],method='sha256') conn=mysql.connect() cursor=conn.cursor() # username=str(uuid.uuid4()) sql="Insert into User(name,email_id,city,password) VALUES(%s,%s,%s,%s)" values=(data[0]['name'],data[0]['email_id'],data[0]['city'],hashed_password) cursor.execute(sql,values) conn.commit() id=get_last_id(cursor) token=jwt.encode({ 'public_id':id, 'exp':datetime.datetime.utcnow()+datetime.timedelta(minutes=2) }, app.config['SECRET_KEY']) resp=jsonify([{"name":data[0]['name']}]) # resp=jsonify("success") resp.headers.add('token',token.decode("UTF-8")) return resp except Exception as e: print("ERROR ",e," ERROR") return "error" finally: conn.close() cursor.close()
def addBookmark(current_user): try: conn = mysql.connect() cursor = conn.cursor() data = request.json[0] date = data['date'] time = data['time'] res_id = data['restaurant_id'] usr_id = current_user['id'] sql = "Insert into Bookmark(user_id,restaurant_id,date,time) VALUES(%s,%s,%s,%s)" values = (usr_id, res_id, date, time) cursor.execute(sql, values) id = get_last_id(cursor) conn.commit() resp = jsonify({ "id": id, "restaurant_id": res_id, "date": date, "time": time }) resp.status_code = 201 return resp except Exception as e: print("ERROR ", e, " ERROR") resp = jsonify("ERROR") resp.status_code = 400 return resp finally: conn.close() cursor.close()
def add_restaurant(): try: data=request.json conn=mysql.connect() cursor=conn.cursor() cursor2=conn.cursor(pymysql.cursors.DictCursor) insert_location(cursor,data[0]['location']) loc_id=get_last_id(cursor) try: insert_restaurant(cursor,data[0],loc_id) except Exception as e: return jsonify("Duplicate email"),400 res_id=get_last_id(cursor) insert_days(cursor,data[0]['days'],res_id) insert_slot(cursor,data[0]['slots'],res_id) print("sss") conn.commit() cursor2.execute("SELECT * FROM Restaurant where id=%s",res_id) rows=cursor2.fetchall() convert_restaurant(cursor2, rows) token=jwt.encode({ 'public_id':res_id, 'exp':datetime.datetime.utcnow()+datetime.timedelta(minutes=app.config['token_expire_time']), 'name':data[0]['name'], 'email':data[0]['email'], 'restaurant':1 }, app.config['SECRET_KEY']) resp=jsonify(rows) resp.status_code=201 resp.headers.add('x-token',token.decode("UTF-8")) resp.headers.add("access-control-expose-headers",'x-token') return resp except Exception as e: print(e) resp=jsonify("ERROR") resp.status_code=500 return resp finally: conn.close() cursor.close() cursor2.close()
def insert_review(current_user): try: data = request.json[0] _comment = data['comment'] _rating = float("0" + str(data['rating'])) _rating_text = data['rating_text'] _res_id = data['restaurant_id'] _user_id = current_user['id'] _date = data['date'] _time = data['time'] conn = mysql.connect() cursor = conn.cursor() cursor.execute("SELECT rating,votes FROM Restaurant where id=%s", _res_id) (rating, votes) = cursor.fetchall()[0] cursor.execute("UPDATE Restaurant SET rating=%s,votes=%s where id=%s", ((rating * votes + _rating) / (votes + 1), votes + 1, _res_id)) print("working") sql = "INSERT INTO Review(restaurant_id,user_id,comment,rating,rating_text,date,time) values(%s,%s,%s,%s,%s,%s,%s)" values = (_res_id, _user_id, _comment, _rating, _rating_text, _date, _time) cursor.execute(sql, values) review_id = get_last_id(cursor) for url in data['photos']: sql = "INSERT INTO Photo(review_id,url) values(%s,%s)" values = (review_id, url) cursor.execute(sql, values) conn.commit() print("sucess") resp = jsonify("Comment Insertion Successfull") resp.status_code = 201 return resp except Exception as e: print("review ", e, " review") resp = jsonify("Error") resp.status_code = 500 return resp finally: conn.close() cursor.close()
def addUser(): try: data = request.json hashed_password = generate_password_hash(data[0]['password'], method='sha256') conn = mysql.connect() cursor = conn.cursor() # username=str(uuid.uuid4()) sql = "Insert into User(name,email_id,city,password) VALUES(%s,%s,%s,%s)" values = (data[0]['name'], data[0]['email_id'], data[0]['city'], hashed_password) cursor.execute(sql, values) conn.commit() id = get_last_id(cursor) token = jwt.encode( { 'public_id': id, 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=app.config['token_expire_time']), 'name': data[0]['name'], 'email': data[0]['email_id'], 'restaurant': 0 }, app.config['SECRET_KEY']) resp = jsonify([{"name": data[0]['name'], "city": data[0]['city']}]) resp.status_code = 201 resp.headers.add('x-token', token.decode("UTF-8")) resp.headers.add("access-control-expose-headers", 'x-token') return resp except Exception as e: print("ERROR ", e, " ERROR") resp = jsonify("Duplicate Email") resp.status_code = 400 return resp finally: conn.close() cursor.close()