def user_follow(): data = get_request_json() follower = data.get('follower') followee = data.get('followee') if not model.user_exists(follower): return result_not_found("User %s doesn't exist" % follower) if follower == followee: return result_invalid_semantic("User %s cannot follow himself" % follower) if not model.user_exists(followee): return result_not_found("User %s doesn't exist" % followee) if not model.user_follows(follower, followee): res = model.user_follow(follower, followee) if res: udata = model.user_data(follower) return result(udata) else: return result_unknown("Couldn't follow %s by %s" % (followee, follower)) else: return result_unknown("User %s already follows %s" % (follower, followee))
def create_account(): username = request.form.get("username") password1 = request.form.get("password") password2 = request.form.get("password_verify") print "top of create account", username,password1,password2 if len(username) < 1: flash("username must be longer than 0 characters") return redirect(url_for("register")) if password1 != password2: flash ("passwords must match, try again") return redirect(url_for("register")) else: print "I'm connecting to the database cause the passwords matched!" model.connect_to_db() if model.user_exists(username): flash ("That user already exists") else: model.create_new_account(username, password1) flash ("Welcome") model.CONN.close() return redirect(url_for("index"))
def post_create(): pdata = get_request_json() email = pdata.get('user') if not model.user_exists(email): return result_not_found("User %s doesn't exist" % email) forum = pdata.get('forum') if not model.forum_exists(forum): return result_not_found("Forum %s doesn't exist" % forum) thread = pdata.get('thread') if not model.thread_exists(thread): return result_not_found("Thread %s doesn't exist" % thread) post_id = model.post_create(pdata) pdata = model.post_data(post_id, counters=False) if pdata: return result(pdata) else: return result_not_found("Couldn't create post")
def thread_list(): limit = get_request_arg('limit', 0) since_date = get_request_arg('since') order = get_request_arg('order', 'desc') if not check_arg(order, ['desc', 'asc']): return result_invalid_semantic("Wrong value for order") forum = get_request_arg('forum') if forum is not None: if not model.forum_exists(forum): return result_not_found("Forum %s doesn't exist" % forum) threads = model.forum_threads(forum, limit=limit, order=order, since_date=since_date) return result(threads) email = get_request_arg('user') if email is not None: if not model.user_exists(email): return result_not_found("User %s doesn't exist" % email) threads = model.user_threads(email, limit=limit, order=order, since_date=since_date) return result(threads) return result_invalid_semantic("User and forum are not set")
def login(): """ Log in the user to the system using Google oauth login. Note: What gets done here depends on what phase of the login process we are in. If this is the INITIAL PHASE, then send the user to the Google login. If we are COMING BACK from a Google login, use the code to get the email and display name set up for the user. :return: An appropriate redirect (depending on what step of the login process this is. """ domain = parser["learningmachine"]["domain"] secrets_file = "{}/{}".format(dir_path, "client_secret.json") scope = "https://www.googleapis.com/auth/userinfo.email" redirect_uri = "http://{}/login".format(domain) login_handler = LoginHandler(secrets_file, scope, redirect_uri) if "code" in request.args: login_handler.setup_user_info(request.args["code"]) session["email"] = login_handler.email session["display_name"] = login_handler.display_name if not model.user_exists(login_handler.email): msg = "Adding user: {} with ID of {} to the database."\ .format(login_handler.email, login_handler.display_name) model.add_user(login_handler.email, login_handler.display_name) msg = "Sending user: {} to main page".format(login_handler.email) app.logger.info(msg) return redirect("/static/main.html") else: msg = "No login code yet. Letting Google handle the login process at: {}"\ .format(login_handler.auth_url) app.logger.info(msg) return redirect(login_handler.auth_url)
def login(): if request.method == 'POST': session.pop('username', None) username = request.form['username'] pwd = request.form['password'] if model.user_exists(username, pwd): app.logger.info("USER EXISTS") session['username'] = request.form['username'] return redirect(url_for('home_page')) return render_template('index.html')
def POST(self): if not self.forgotform.validates(): return render.forgotpassword('You did not type a valid e-mail address.') else: if model.user_exists(self.forgotform['user'].value): confirmationcode = model.update_confirmation_string(self.forgotform['user'].value) model.send_confirmation(self.forgotform['user'].value, confirmationcode, 'forgotpassword') return render.forgotpassword('a message was sent to your email account, klick the included link for confirmation and to reset your password.') else: return render.generalpagewithform('You did not type a valid e-mail address.')
def POST(self): if not self.createaccountform.validates(): return render.landing('', 'you left a field empty or e-mail address in wrong format', '', '#createaccounts') else: #check if account already exists for email if model.user_exists(self.createaccountform['emailaddress'].value): return render.landing('', 'An account already exists for your email address.', '' , '#createaccounts') else: confirmationcode = model.create_user(self.createaccountform['emailaddress'].value, self.createaccountform['firstname'].value, self.createaccountform['lastname'].value) model.send_confirmation(self.createaccountform['emailaddress'].value, confirmationcode, '') userid = model.get_user_details_by_email(self.createaccountform['emailaddress'].value)[0]["userid"] model.set_confirmation_message_sent(userid) return render.landing('', 'a message was sent to your email account, klick the included link for confirmation and to set your password.','', '#createaccounts')
def user_unfollow(): data = get_request_json() follower = data.get('follower') followee = data.get('followee') if not model.user_exists(follower): return result_not_found("User %s doesn't exist" % follower) if not model.user_exists(followee): return result_not_found("User %s doesn't exist" % followee) if model.user_follows(follower, followee): res = model.user_unfollow(follower, followee) if res: udata = model.user_data(follower) return result(udata) else: return result_unknown("Couldn't unfollow %s by %s" % (followee, follower)) else: return result_unknown("User %s doesn't follow %s" % (follower, followee))
def signup(): if request.method == 'GET': return render_template('signup.html') else: username = request.form['username'] password = request.form['password'] if model.user_exists(username): error_message = 'Username already exists' return render_template('signup.html', message=error_message) message = model.create_account(username, password) return render_template('signup.html', message=message)
def home(): if request.method == 'GET': return render_template('index.html') else: username = request.form['username'] password = request.form['password'] if model.user_exists(username, password): success_message = 'Login Successful' return redirect(url_for('dashboard')) else: error_message = 'Invalid credentials' return render_template('index.html', message=error_message)
def user_list_followers(): email = get_request_arg('user') limit = get_request_arg('limit', 0) since_id = get_request_arg('since_id') order = get_request_arg('order', 'desc') if not check_arg(order, ['desc', 'asc']): return result_invalid_semantic("Wrong value for order") if not model.user_exists(email): return result_not_found("User %s doesn't exist" % email) res = model.user_list_followers(email, limit=limit, order=order, since_id=since_id, full=True) return result(res)
def user_list_posts(): email = get_request_arg('user') limit = get_request_arg('limit', 0) since_date = get_request_arg('since') order = get_request_arg('order', 'desc') if not check_arg(order, ['desc', 'asc']): return result_invalid_semantic("Wrong value for order: %s" % order) if not model.user_exists(email): return result_not_found("User %s doesn't exist" % email) posts = model.user_posts(email, limit=limit, order=order, since_date=since_date) return result(posts)
def forum_create(): fdata = get_request_json() email = fdata.get('user') if not model.user_exists(email): return result_not_found("User %s doesn't exist" % email) forum = fdata.get('short_name') res = model.forum_create(fdata) fdata = model.forum_data(forum) if fdata: return result(fdata) else: return result_not_found("Couldn't create forum %s" % forum)
def user_update_profile(): data = get_request_json() email = data.get('user') name = data.get('name') about = data.get('about') if not model.user_exists(email): return result_not_found("User %s doesn't exist" % email) res = model.user_update(email, data) if res: udata = model.user_data(email) return result(udata) else: return result_unknown("Couldn't update user profile for %s" % (email))
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] result = user_exists(username) if result: if result['password'] != password: return render_template('access_denied.html', error_msg = "Password doesn't match. Go back and re-renter the password") session['username'] = username session['c_type'] = result['c_type'] return redirect(url_for('home')) return render_template('access_denied.html', error_msg = "Username doesn't exist") return redirect(url_for('home'))
def create_user(): email = request.form.get("email") age = request.form.get("age") zipcode = request.form.get("zipcode") password = request.form.get("password") verify_password = request.form.get("password_verify") if password != verify_password: flash("Passwords do not match") return redirect(url_for("register")) if model.user_exists(email): flash("Account already exists for user email") return redirect(url_for("register")) model.create_user(email, password, age, zipcode) flash("You've successfully made an account!") return redirect(url_for("index"))
def create_account(): if session.get("user_id"): go_home() model.connect_to_db() username = request.form.get("username") if model.user_exists(username): flash("This username already exists.") return redirect(url_for("register")) password = request.form.get("password") verify = request.form.get("password_verify") if password == verify: model.create_user(username, password) flash("New user account was created.") return redirect(url_for("index")) else: flash("Passwords do not match.") return redirect(url_for("register"))
def thread_create(): tdata = get_request_json() email = tdata.get('user') if not model.user_exists(email): return result_not_found("User %s doesn't exist" % email) forum = tdata.get('forum') if not model.forum_exists(forum): return result_not_found("Forum %s doesn't exist" % forum) thread_id = model.thread_create(tdata) tdata = model.thread_data(thread_id, counters=False) if tdata: return result(tdata) else: return result_not_found("Couldn't create thread %s" % tdata.get('title'))
def signup(): if request.method == 'POST': user_info = {} user_info['username'] = request.form['username'] user_info['password'] = request.form['password1'] password2 = request.form['password2'] user_info['c_type'] = request.form['type'] if user_info['c_type'] == 'buyer': user_info['cart'] = [] if user_exists(user_info['username']): return render_template('access_denied.html', error_msg = "Username already exist") if user_info['password'] != password2: return render_template('access_denied.html', error_msg = "Password doesn't match. Go back and re-renter the password") save_user(user_info) return redirect(url_for('home'))
def signup(): if request.method == 'POST': user_info = {} user_info['username'] = request.form['username'] user_info['password'] = request.form['password1'] password2 = request.form['password2'] user_info['c_type'] = request.form['type'] user_profile_image = {} user_profile_image['username'] = user_info['username'] if 'profile_image' in request.files: # The image will be stored in the request.files and not in request.form profile_image = base64.b64encode( request.files['profile_image'].read()) user_profile_image['image'] = request.files[ 'profile_image'].filename user_profile_image['data'] = profile_image user_info['profile_image'] = user_profile_image['image'] if user_info['c_type'] == 'buyer': user_info['cart'] = [] if user_exists(user_info['username']): return render_template('access_denied.html', error_msg="Username already exist") if user_info['password'] != password2: return render_template( 'access_denied.html', error_msg= "Password doesn't match. Go back and re-renter the password") save_profile_image(user_profile_image) save_user(user_info) return redirect(url_for('home'))
def thread_unsubscribe(): data = get_request_json() email = data.get('user') thread = data.get('thread') if not model.user_exists(email): return result_not_found("User %s doesn't exist" % email) if not model.thread_exists(thread): return result_not_found("Thread %s doesn't exist" % thread) if model.user_subscribed(email, thread): res = model.thread_unsubscribe(email, thread) if res: return result({ 'user': email, 'thread': thread }) else: return result_unknown("Couldn't unsubscribe %s by %s" % (thread, email)) else: return result_unknown("User %s is not subscribed to %s" % (email, thread))