def register(): """Register a new user. Validates that the username is not already taken. Hashes the password for security. """ if request.method == "POST": username = request.form["username"] password = request.form["password"] error = None if not username: error = "Username is required." elif not password: error = "Password is required." elif (db.execute("SELECT id FROM user WHERE username = ?", (username, )).fetchone() is not None): error = "User {0} is already registered.".format(username) if error is None: # the name is available, store it in the database and go to # the login page db.execute( "INSERT INTO user (username, password) VALUES (?, ?)", (username, generate_password_hash(password)), ) db.commit() return redirect(url_for("auth.login")) flash(error) return render_template("auth/register.html")
def add_feedback_on_tweet_classification_to_db(db, form, user_id): tweet = form['tweet_to_classify'] classification = form['classification_result'] feedback = form['result_feedback'] db.execute( "INSERT into twitter_classification_plus_feedback (author_id,data,classification,user_feedback) VALUES (?, ?,?,?)", (user_id, tweet, feedback, classification)) db.commit()
def delete(id): """Delete a post. Ensures that the post exists and that the logged in user is the author of the post. """ get_post(id) db = flaskr.db.get_db() db.execute('DELETE FROM post WHERE id = ?', (id, )) db.commit() return flask.redirect(flask.url_for('blog.index'))
def update_user_details(db, g_user, details): update_str = 'UPDATE user SET ' + ''.join([ x[0] + ' = "' + x[1] + '", ' for x in details ]) + 'username="******" where username like "' + g_user['username'] + '";' print(update_str) db.execute(update_str) db.commit() return True
def add_feedback_on_generated_text_to_db(db, form, user_id): # form['data[seed]'], form['data[length]'],form['data[method]'],form['data[output]'],form['data[rank]'] # classification = form['classification_result'] # feedback = form['result_feedback'] # text = form['output'] db.execute( "INSERT into generated_text_plus_feedback (author_id,param_method,param_length,param_output_size,seed,data,score) VALUES (?, ?,?,?,?,?,?)", (user_id, text_data.GENERATION_METHOD[form['data[method]']], len(form['data[seed]']), len(form['data[seed]']), form['data[seed]'], form['data[output]'], form['data[ranking]'])) db.commit()
def deleteUserFromDB(conn, user_id): if user_id in ("", None): raise Exception return execute( conn, "DELETE FROM Users WHERE user_id = :user_id;", {'user_id': user_id} )
def getAllAddressesForUserHelper(conn, user_id): if user_id in ("", None): raise Exception return execute( conn, "SELECT DISTINCT Users.user_id, Users.fname, Users.lname, Address.address_id, Address.address, Address.start_date, Address.end_date FROM (Address INNER JOIN Users ON Address.user_id = Users.user_id) WHERE Users.user_id = :user_id;", {'user_id': user_id} )
def changeAddressForAUserHelper(conn, address_id, address): if address_id in ("", None) or address_id in ("", None): raise Exception return execute( conn, "UPDATE Address SET address = :address WHERE address_id = :address_id;", {'address_id' : address_id, 'address' : address} )
def getAllVendprsForAddressHelper(conn, address_id): if address_id in ("", None): raise Exception return execute( conn, "SELECT AddressVendorsMap.address_id, Address.address, AddressVendorsMap.vendor_id, Vendors.vendor_name , AddressVendorsMap.vendor_access FROM ((AddressVendorsMap INNER JOIN Vendors ON AddressVendorsMap.vendor_id = Vendors.vendor_id) INNER JOIN Address ON AddressVendorsMap.address_id = Address.address_id) WHERE AddressVendorsMap.address_id = :address_id;", {'address_id': address_id})
def insertUserInDB(conn, fname, lname, email, phone): invalid = ("", None) if fname in invalid or lname in invalid or email in invalid: raise Exception return execute( conn, "INSERT INTO Users (fname, lname, email, phone) VALUES (:fname, :lname, :email, :phone);", {'fname': fname, 'lname': lname, 'email': email, 'phone': phone} )
def index(): posts = db.execute( 'SELECT p.id, title, body, created, author_id, username' ' FROM post p JOIN user u ON p.author_id = u.id' ' ORDER BY created DESC' ).fetchall() posts = Post.query\ .join(User) .all()
def deleteVendorAddressAssociationFromDB(conn, vendor_id, address_id): if address_id in ("", None) and vendor_id in ("", None): raise Exception return execute( conn, "DELETE FROM AddressVendorsMap WHERE address_id = :address_id AND vendor_id = :vendor_id;", { 'address_id': address_id, 'vendor_id': vendor_id })
def create(): if request.method == 'POST': title = request.form['title'] body = request.form['body'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db.execute( 'INSERT INTO post (title, body, author_id)' ' VALUES (?, ?, ?)', (title, body, g.user['id'])) db.commit() return redirect(url_for('blog.index')) return render_template('blog/create.html')
def load_logged_in_user(): """If a user id is stored in the session, load the user object from the database into ``g.user``.""" user_id = session.get("user_id") if user_id is None: g.user = None else: g.user = (db.execute("SELECT * FROM user WHERE id = ?", (user_id, )).fetchone())
def grantVendorAddressAssociationFromDB(conn, vendor_id, address_id): if address_id in ("", None) and vendor_id in ("", None): raise Exception return execute( conn, "UPDATE AddressVendorsMap SET vendor_access = 1 WHERE address_id = :address_id AND vendor_id = :vendor_id;", { 'address_id': address_id, 'vendor_id': vendor_id })
def insertVendorInDB(conn, vendor_name, phone): print(vendor_name, phone) if vendor_name in ("", None): raise Exception return execute( conn, "INSERT INTO Vendors (vendor_name, phone) VALUES (:vendor_name, :phone);", { 'vendor_name': vendor_name, 'phone': phone })
def getUserAddressAsVendorHelper(conn, vendor_id, user_id): if vendor_id in ("", None) or user_id in ("", None): raise Exception print("see this --------", vendor_id, user_id) return execute( conn, "SELECT Vendors.vendor_id, Vendors.vendor_name, Address.address_id, Users.user_id, Users.fname, Users.lname, Address.address, AddressVendorsMap.vendor_access FROM (((AddressVendorsMap INNER JOIN Address ON AddressVendorsMap.address_id = Address.address_id) INNER JOIN Users ON Address.user_id = Users.user_id) INNER JOIN Vendors ON Vendors.vendor_id = AddressVendorsMap.vendor_id) WHERE AddressVendorsMap.vendor_access=1 AND Users.user_id = :user_id and Vendors.vendor_id = :vendor_id;", { "user_id": user_id, "vendor_id": vendor_id })
def update_post(id): post = get_post(id) if flask.request.method == 'POST': title = flask.request.form['title'] content = flask.request.form['content'] error = None if not title: error = 'Title is required' if not content: error = 'No content' if error is not None: flask.flash(error) else: db = flaskr.db.get_db() db.execute('UPDATE post SET title = ?, content = ? WHERE id = ?', (title, content, id)) db.commit() return flask.redirect(flask.url_for('blog.index')) return flask.render_template('blog/update.html', post=post)
def delete_history_by_author_id(db, id): try: db.execute( "DELETE FROM generated_text_plus_feedback where generated_text_plus_feedback.author_id=?", (id, )) db.execute( "DELETE FROM twitter_classification_plus_feedback where twitter_classification_plus_feedback.author_id=?", (id, )) db.execute("commit") return True except Exception: print("failed!") db.execute("rollback") return False
def insertAddressVendorInDB(conn, vendor_id, address_id): print(vendor_id, address_id) vendor_access = 1 # if vendor_id or not address: # raise Exception return execute( conn, "INSERT INTO AddressVendorsMap (vendor_id, address_id, vendor_access) VALUES (:vendor_id, :address_id, :vendor_access);", { 'vendor_id': vendor_id, 'address_id': address_id, 'vendor_access': vendor_access })
def insertAddressInDB(conn, user_id, address, start_date, end_date): print(user_id, address, start_date, end_date) invalid = ("", None) if not user_id or address in invalid or user_id in invalid: raise Exception return execute( conn, "INSERT INTO Address (user_id, address, start_date, end_date) VALUES (:user_id, :address, :start_date, :end_date);", { 'user_id': user_id, 'address': address, 'start_date': start_date, 'end_date': end_date })
def update(id): post = get_post(id) if request.method == 'POST': title = request.form['title'] body = request.form['body'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE post SET title = ?, body = ?' ' WHERE id = ?', (title, body, id) ) db.commit() return redirect(url_for('blog.index')) return render_template('blog/update.html', post=post)
def login(): """Log in a registered user by adding the user id to the session.""" if request.method == "POST": username = request.form["username"] password = request.form["password"] error = None user = db.execute("SELECT * FROM user WHERE username = ?", (username, )).fetchone() if user is None: error = "Incorrect username." elif not check_password_hash(user["password"], password): error = "Incorrect password." if error is None: # store the user id in a new session and return to the index session.clear() session["user_id"] = user["id"] return redirect(url_for("index")) flash(error) return render_template("auth/login.html")
def viewAddressVendorsMapHelper(conn): return execute( conn, "SELECT vendor_id, address_id, vendor_access FROM AddressVendorsMap")
def viewUsersHelper(conn): return execute(conn, "SELECT user_id, fname, email, phone FROM Users")
def twitter_classification_plus_feedback_by_user_id(db, id): return db.execute( 'Select * from twitter_classification_plus_feedback where author_id = ?', (id, )).fetchall()
def get_user_by_name(db, username, with_pass=False): return db.execute('Select * from user where username = ? ', (username, )).fetchone()
def get_user_by_id(db, id): return db.execute('Select * from user where id = ? ', (id, )).fetchone()
def get_generated_text_plus_feedback_by_user_id(db, id): return db.execute( 'Select * from generated_text_plus_feedback where author_id = ?', (id, )).fetchall()
def get_user_field_by_username(db, username, fieldname): return db.execute('Select * from user where username = ? ', (username, )).fetchone()[fieldname]