def remove_follow(user_id): '''Unfollow: json data: whom_id''' if not request.json: return make_error( 400, "Bad Request", "The browser (or proxy) sent a request that this server could not understand." ) if request.method != 'DELETE': return make_error(405, 'Method Not Allowed', 'The method is not allowed for the requested URL.') data = request.json get_credentials_by_user_id(user_id) if not basic_auth.check_credentials(data["username"], data["pw_hash"]): return make_error(401, 'Unauthorized', 'Correct username and password are required.') if data: '''Check who_id and whom_id existing''' cur = minitwit.query_db( 'select count(*) from follower where who_id = ? and whom_id = ?', [user_id, data["whom_id"]], one=True) if cur[0] == 0: return make_error( 404, 'Not Found', 'The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.' ) db = minitwit.get_db() db.execute( '''delete from follower where who_id = ? and whom_id = ?''', [user_id, data["whom_id"]]) db.commit() print 'You are no longer following user has ', data["whom_id"] return jsonify(data)
def add_follow(user_id): '''Insert follow: json data: whom_id''' if not request.json: return make_error( 400, "Bad Request", "The browser (or proxy) sent a request that this server could not understand." ) if request.method != 'POST': return make_error(405, 'Method Not Allowed', 'The method is not allowed for the requested URL.') data = request.json get_credentials_by_user_id(user_id) if not basic_auth.check_credentials(data["username"], data["pw_hash"]): return make_error(401, 'Unauthorized', 'Correct username and password are required.') if data: '''Check duplicate''' cur = minitwit.query_db( 'select count(*) from follower where who_id = ? and whom_id = ?', [user_id, data["whom_id"]], one=True) if cur[0] > 0: return make_error(422, "Unprocessable Entity", "Data duplicated") db = minitwit.get_db() db.execute( '''insert into follower (who_id, whom_id) values (?, ?)''', [user_id, data["whom_id"]]) db.commit() print 'You are following user has user_id ', data['whom_id'] return jsonify(data)
def add_message(user_id): '''Insert a message into table message: json data: author_id, text''' if not request.json: return make_error( 400, "Bad Request", "The browser (or proxy) sent a request that this server could not understand." ) if request.method != 'POST': return make_error(405, 'Method Not Allowed', 'The method is not allowed for the requested URL.') data = request.json get_credentials_by_user_id(user_id) if not basic_auth.check_credentials(data["username"], data["pw_hash"]): return make_error(401, 'Unauthorized', 'Correct username and password are required.') if data: username = get_username(user_id) get_credentials(username) if not basic_auth.check_credentials(data["username"], data["pw_hash"]): return make_error(401, 'Unauthorized', 'Invalid Username ad/or Password') db = minitwit.get_db() db.execute( '''insert into message (author_id, text) values (?, ?)''', [data["author_id"], data["text"]]) db.commit() print 'Your message was successfully recorded' return jsonify(data)
def add_follow_user(username1, username2): """Adds the username1 as follower of the given username2.""" data = request.get_json() get_credentials(username1) if not basic_auth.check_credentials(data["username"], data["pw_hash"]): return make_error(401, 'Unauthorized', 'Correct username and password are required.') who_id = minitwit.get_user_id(username1) whom_id = minitwit.get_user_id(username2) if whom_id is None: return make_error( 404, 'Not Found', 'The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.' ) cur = minitwit.query_db( 'select count(*) from follower where who_id = ? and whom_id = ?', [who_id, whom_id], one=True) if cur[0] > 0: return make_error(422, "Unprocessable Entity", "Data duplicated") if request.method == 'POST': db = minitwit.get_db() db.execute('insert into follower (who_id, whom_id) values (?, ?)', [who_id, whom_id]) db.commit() print 'You are now following %s' % username2 return jsonify(data) return make_error(405, 'Method Not Allowed', 'The method is not allowed for the requested URL.')
def insert_message(username): """Inserts a new message from current <username>""" if request.method == 'POST': data = request.get_json() user_id = minitwit.get_user_id(username) get_credentials(data["username"]) if not basic_auth.check_credentials(data["username"], data["pw_hash"]): return make_error(401, 'Unauthorized', 'Correct username and password are required.') if data: db = minitwit.get_db() db.execute( '''insert into message (author_id, text, pub_date) values (?, ?, ?)''', [user_id, data["text"], int(time.time())]) db.commit() print 'Your message was recorded' return jsonify(data) return make_error(405, 'Method Not Allowed', 'The method is not allowed for the requested URL.')
def Sign_up(): '''User Sign up: json data: username, email, password, confirmed_password''' if not request.json: return make_error( 400, "Bad Request", "The browser (or proxy) sent a request that this server could not understand." ) if request.method != 'POST': return make_error(405, 'Method Not Allowed', 'The method is not allowed for the requested URL.') data = request.json if data: if not data["username"] or not data["email"] or not data["password"] \ or not data["confirmed_password"] or data["password"] != data["confirmed_password"]: return make_error( 400, "Bad Request", "The browser (or proxy) sent a request that this server could not understand." ) '''check duplicate''' cur = minitwit.query_db('select count(*) from user where username = ?', [data["username"]], one=True) cur1 = minitwit.query_db('select count(*) from user where email = ?', [data["email"]], one=True) if cur[0] > 0: return make_error(422, "Unprocessable Entity", "Duplicated Username") if cur1[0] > 0: return make_error(422, "Unprocessable Entity", "Duplicated email") pw = generate_password_hash(data["password"]) db = minitwit.get_db() db.execute( '''insert into user (username, email, pw_hash) values (?, ?, ?)''', [data["username"], data["email"], pw]) db.commit() print 'You were successfully registered' return jsonify(data)
def change_email(user_id): '''Change email: json data: email, confirmed_email''' if not request.json: return make_error( 400, "Bad Request", "The browser (or proxy) sent a request that this server could not understand." ) if request.method != 'PUT': return make_error(405, 'Method Not Allowed', 'The method is not allowed for the requested URL.') data = request.json get_credentials_by_user_id(user_id) if not basic_auth.check_credentials(data["username"], data["pw_hash"]): return make_error(401, 'Unauthorized', 'Correct username and password are required.') if data: '''Check user_id existing''' cur = minitwit.query_db('select count(*) from user where user_id = ?', [user_id], one=True) if cur[0] == 0: return make_error( 404, 'Not Found', 'The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.' ) '''check password and confirmed password are equal''' if data["email"] != data["confirmed_email"]: return make_error( 422, "Unprocessable Entity", "password and confirmed password not consistent") db = minitwit.get_db() email = data["email"] db.execute( '''update user set email = ? where user_id = ?''', [email, user_id]) db.commit() print 'Your email was successfully changed' return jsonify(data)
def populate_db(): """Re-populates the database with test data""" db = minitwit.get_db() with app.open_resource('population.sql', mode='r') as f: db.cursor().executescript(f.read()) db.commit()