def cart(): if request.method == 'GET': token = request.headers.get('Authorization').replace("Bearer ", "") print(token) payload = jwt.decode(token, app.config.get('JWT_SECRET_KEY'), algorithms=['HS256']) auth = payload['sub'] cur = mysql.cursor(buffered=True) cur.execute( "SELECT * FROM cart c, product p where c.uid = %s and p.pid = c.pid", (auth['uid'], )) #cur.execute("SELECT * FROM cart where uid = %s", (auth['uid'],)) row_headers = [x[0] for x in cur.description] rv = cur.fetchall() json_data = [] for result in rv: json_data.append(dict(zip(row_headers, result))) res = json.loads(json.dumps(json_data)) return jsonify(res) elif request.method == 'POST': data = request.get_json() cur = mysql.cursor(buffered=True) cur.execute("SELECT * FROM cart where uid = %s and pid = %s", ( data['uid'], data['pid'], )) if cur.rowcount == 1: rv = cur.fetchone() quantity = int(rv[2]) + int(data['itemQuantity']) print(quantity) cur.execute( "UPDATE cart SET total = %s where uid = %s and pid = %s", ( quantity, data['uid'], data['pid'], )) mysql.commit() return "Success" cur.execute("INSERT INTO cart (uid, pid, total) VALUES (%s, %s, %s)", (data['uid'], data['pid'], int(data['itemQuantity']))) mysql.commit() return 'Success' return 'Success'
def history_view(): def myconverter(o): if isinstance(o, datetime.datetime): return o.isoformat() if request.method == 'GET': token = request.headers.get('Authorization').replace("Bearer ", "") print(token) payload = jwt.decode(token, app.config.get('JWT_SECRET_KEY'), algorithms=['HS256']) auth = payload['sub'] cur = mysql.cursor(buffered=True) cur.execute( "SELECT * FROM history h, product p WHERE h.uid = %s and h.pid = p.pid", (auth['uid'], )) row_headers = [x[0] for x in cur.description] rv = cur.fetchall() print("rv:", rv) json_data = [] for result in rv: asd = list(result) asd[4] = result[4].isoformat() json_data.append(dict(zip(row_headers, asd))) res = json.loads(json.dumps(json_data, default=myconverter)) print(res) return jsonify(res)
def edit_address(): if request.method == 'POST': data = request.get_json() cur = mysql.cursor(buffered=True) cur.execute("UPDATE users SET address = %s where uid = %s", ( data['address'], data['uid'], )) mysql.commit() cur.close() return 'Success'
def index(): if request.method == 'GET': cur = mysql.cursor(buffered=True) cur.execute("SELECT * FROM product") row_headers= [x[0] for x in cur.description] rv = cur.fetchall() json_data = [] for result in rv: json_data.append(dict(zip(row_headers,result))) res = json.loads(json.dumps(json_data)) return jsonify(res)
def cart_delete(): if request.method == 'POST': data = request.get_json() cur = mysql.cursor(buffered=True) cur.execute("DELETE FROM cart where uid = %s and pid = %s", ( data['uid'], data['pid'], )) mysql.commit() cur.close() return "Success" return "Success"
def comment(): if request.method == 'POST': data = request.get_json() print(data) cur = mysql.cursor(buffered=True) cur.execute( "INSERT INTO comment (uid, pid, rating, comment) VALUES (%s, %s, %s, %s)", ( data['uid'], data['pid'], data['currentValue'], data['textArea'], )) mysql.commit() cur.close() return "Success give comment" return "Success"
def inpaid(): if request.method == 'POST': cur = mysql.cursor(buffered=True) data = request.get_json() uid = data['uid'] pids = data['pids'] total = data['checkoutItemsTotal'] date = (time.strftime('%Y-%m-%d %H:%M:%S')) ticket = random.randint(1000000, 2000000000) for i in range(len(pids)): cur.execute( "INSERT INTO history (`uid`, `pid`, `total_cost`, `date`, `ticket`) VALUES (%s, %s, %s, %s, %s)", (uid, pids[i], total, date, ticket)) mysql.commit() return jsonify({ "transaction_id": ticket, "date": (time.strftime('%Y-%m-%d %H:%M:%S')) })
def edit_profile(): if request.method == 'POST': cur = mysql.cursor(buffered=True) data = dict(request.form) if (request.files): #handle image profile = request.files['profile_image'] bg = request.files['background_image'] bg_temp = bg.filename.split(".") profile_temp = profile.filename.split(".") profile.filename = data['uid'] + "_profile." + profile_temp[1] bg.filename = data['uid'] + "_background." + bg_temp[1] print(bg.filename, profile.filename) if profile.filename == '' and bg.filename == '': return "No Selected File" if profile and allowed_file( profile.filename) and bg and allowed_file(bg.filename): profilename = secure_filename(profile.filename) bgname = secure_filename(bg.filename) profile.save( os.path.join(app.config['UPLOAD_FOLDER'], profilename)) bg.save(os.path.join(app.config['UPLOAD_FOLDER'], bgname)) cur.execute("SELECT password FROM users where uid = %s", (data['uid'], )) rv = cur.fetchone() new_password = bcrypt.generate_password_hash( data['newPassword']).decode('utf-8') if bcrypt.check_password_hash(rv[0], data['oldPassword']): cur.execute( "UPDATE users SET first_name = %s, last_name = %s, password = %s, address = %s where uid = %s", (data['firstName'], data['lastName'], new_password, data['address'], data['uid'])) mysql.commit() print("Success") else: print("missmatch") return "Password missmatch!" return "Success"
def shop(pid): if request.method == 'GET': cur = mysql.cursor(buffered=True) cur.execute("SELECT * FROM product comment where pid = %s", (pid,)) row_headers = [x[0] for x in cur.description] rv = cur.fetchall() json_data = [] for result in rv: json_data.append(dict(zip(row_headers,result))) res = json.loads(json.dumps(json_data))[0] cur.execute("SELECT * FROM comment c, users u where c.pid = %s AND u.uid = c.uid", (pid,)) if cur.rowcount >= 1: print("Masuk") com_headers = [x[0] for x in cur.description] rv = cur.fetchall() json_data = [] for result in rv: json_data.append(dict(zip(com_headers,result))) com = json.loads(json.dumps(json_data)) final = ({"Item":res, "Comment":com}) print("Final:",final) return jsonify(final) else: final = ({"Item":res, "Comment":[]}) return jsonify(final) elif request.method == 'POST': data = request.get_json()['data'] token = request.cookies.get('auth') payload = jwt.decode(token, app.config.get('JWT_SECRET_KEY'), algorithms=['HS256']) auth = payload['sub'] cur = mysql.connection.cursor(buffered=True) cur.execute("INSERT INTO cart (uid, pid) VALUES (%s, %s)", (auth['uid'], data['pid'])) mysql.connection.commit() cur.close() print("Success Add to Cart") return 'Success Add to Cart'