def user(): user_name = request.form['username'] if request.method == 'POST': if inter.user_exists(user_name): return app.response_class(status=409) max_id = inter.execute_read_query("SELECT COUNT(*) FROM users") idval = max_id[0][0] + 1 email = request.form['email'] sender_name = request.form['sender_name'] sender_street = request.form['sender_street'] sender_city = request.form['sender_city'] sender_state = request.form['sender_state'] sender_zip = request.form['sender_zip'] sender_country = request.form['sender_country'] unencrypted_pw = request.form['password'] encrypted = inter.encrypt_password(unencrypted_pw) quer1 = f"INSERT INTO users VALUES (\'{user_name}\', \'{idval}\', \'000000\', " quer2 = f"\'{email}\', \'{sender_name}\', \'{sender_street}\'," quer3 = f"\'{sender_city}\', \'{sender_state}\', \'{sender_zip}\'," quer4 = f"\'{sender_country}\', \'{encrypted}\')" query = " ".join([quer1, quer2, quer3, quer4]) if inter.execute_query(query) and pay.new_user(idval, email): print("user added") return app.response_class(status=200) elif request.method == 'DELETE': if not inter.user_exists(user_name): return app.response_class(status=404) query = f"DELETE FROM users WHERE username = \'{user_name}\'" if inter.execute_query(query): return app.response_class(status=200) else: return app.response_class(status=400)
def update_user(col_name): idval = request.form['id'] if inter.user_exists(idval, 'id'): replace = request.form[f"{col_name}"] if col_name == "password": replace = inter.encrypt_password(replace) query = f"UPDATE users SET {col_name} = \'{replace}\' WHERE id = \'{idval}\'" print(query) if inter.execute_query(query): query = f"SELECT * FROM users WHERE id = \'{idval}\'" resp = inter.execute_read_query(query) if resp: key_list = [ "username", "id", "recovery_key", "email", "sender", "street", "city", "state", "zip", "country", "password" ] full_resp = dict(zip(key_list, resp[0])) stripe_id = pay.get_customer_id(full_resp["id"]) full_resp["stripe_id"] = stripe_id payment_options = pay.get_payment_options( full_resp["stripe_id"]) full_resp["payment_options"] = payment_options print(full_resp) response = app.response_class(response=json.dumps(full_resp), status=200, mimetype='application/json') return response return app.response_class(status=404)
def addpackage(): user_id = request.form['user_id'] rate_id = request.form['rate_id'] shipment_id = request.form['shipment_id'] resp = ship.buy_label(shipment_id, rate_id) query = f"INSERT INTO labels VALUES (\'{user_id}\', \'{shipment_id}\')" return_dict = { 'label': resp.postage_label.label_url, 'tracker': resp.tracker.id } success_check = inter.execute_query(query) if success_check: return app.response_class(status=200, response=json.dumps(return_dict), mimetype='application/json') else: return app.response_class(status=500)
def new_user(userid, email): acct = stripe.Customer.create(email=email) acct_id = acct["id"] query = f"INSERT INTO stripe VALUES (\'{userid}\', \'{acct_id}\')" return inter.execute_query(query)
def delete_package(package_id): query = f"DELETE FROM labels WHERE \'shipid\' IS \'{package_id}\'" return inter.execute_query(query)