def register(): """ Registers the user. """ if g.user: return redirect(url_for('home')) if request.method == 'POST': if not request.form['username']: flash("You have to enter a username.") elif not request.form['email'] or '@' not in request.form['email']: flash("You have to enter a valid email.") elif not request.form['password']: flash("You have to enter a password.") elif request.form['password'] != request.form['password2']: flash("The two passwords do not match.") elif database.get_user_id(email=request.form['email']) is not None: flash("User with this email already exists.") elif database.get_user_id(username=request.form['username']) is not None: flash("User with this username already exists.") else: g.db.execute('''insert into pending_users ( username, email, pw_hash) values (?, ?, ?)''', [request.form['username'], request.form['email'], generate_password_hash(request.form['password'])]) g.db.commit() flash("Successfully requested an account"); return redirect(url_for('login')) return render_template('register.html')
def edit_add_lang(username): if is_not_logged_in(): return redirect("/") if session["csrf_token"] != request.form["csrf_token"]: abort(403) if username != session["username"]: return redirect("/user/" + session["username"] + "/profile/edit") languages = editprofile.get_languages(db, session["username"]) language = request.form["lang"].strip() if language == "" or len(language) > 25: flash("Invalid language. Empty or too long.", category="error") return redirect("/user/" + session["username"] + "/profile/edit") if len(languages) >= 3: flash("Already learning max amount of languages.", category="error") return redirect("/user/" + session["username"] + "/profile/edit") """Method returns true if already learning given language""" if editprofile.check_if_learning(db, username, language): flash("Already learning that language.", category="error") return redirect("/user/" + session["username"] + "/profile/edit") user_id = database.get_user_id(db, username) editprofile.add_language(db, user_id, language) flash("Language added successfully.", category="message") return redirect("/user/" + session["username"] + "/profile/edit")
def edit_profile(): if 'username' not in session: return redirect(url_for('login')) update_form = forms.UpdateForm() password_form = forms.PasswordForm() user = database.get_user(session['username'])[0] update_form.first_name.data = user[1] update_form.last_name.data = user[2] update_form.username.data = user[3] update_form.email.data = user[4] if request.method == 'POST': if 'update_profile' in request.form: update_form = forms.UpdateForm(request.form) if update_form.validate(): update_form.save(session['username']) session['username'] = update_form.username.data elif 'change_password' in request.form: password_form = forms.PasswordForm(request.form) if password_form.validate(): password_form.save(session['username']) elif 'remove' in request.form: db = database.get_db() db.execute('delete from users where id=?', [database.get_user_id(session['username'])]) db.commit() return redirect(url_for('logout')) return render_template('edit_profile.html', register_form=update_form, password_form=password_form)
def delete_language(SQLAlchemy, username, language): user_id = database.get_user_id(SQLAlchemy, username) delete_sql = "DELETE FROM languages WHERE user_id=:id AND language=:lang" SQLAlchemy.session.execute(delete_sql, {"id": user_id, "lang": language}) SQLAlchemy.session.commit() return True
def edit_list(SQLAlchemy, username, listname, new_name): if check_listname(SQLAlchemy, username, new_name): return False user_id = database.get_user_id(SQLAlchemy, username) update_sql = "UPDATE lists SET name=:name WHERE user_id=:user_id AND name=:listname" SQLAlchemy.session.execute(update_sql, {"listname":listname, "name":new_name, "user_id":user_id}) SQLAlchemy.session.commit() return True
def create_new_list(SQLAlchemy, username, listname): user_id = database.get_user_id(SQLAlchemy, username) if check_listname(SQLAlchemy, username, listname): return False insert_sql = "INSERT INTO lists (user_id, name) VALUES (:user_id, :name)" SQLAlchemy.session.execute(insert_sql, {"user_id":user_id, "name":listname}) SQLAlchemy.session.commit() return True
def add_points(): if not request.json or 'Points' not in request.json: abort(400) user_id = db.get_user_id(request.json["Login"]) if(user_id==0): return make_response(jsonify({'error': "This user doesn't exist!"}), 400) print(user_id) rows_affected = db.add_points(request.json, user_id) if rows_affected: return make_response(jsonify({'result': "ok"}), 200) else: return make_response(jsonify({'error': "couldn't add points"}), 404)
def registration(): new_user = True if request.method == 'POST': user_name = request.form.get("user_name") password = request.form.get("password") hashed_password = data_handler.get_hashed_password(password) new_user = database.register_user(user_name, hashed_password) if new_user: session['username'] = user_name session['user_id'] = database.get_user_id(user_name) return redirect("/") return render_template("registration.html", new_user=new_user)
def get_languages(SQLAlchemy, username): user_id = database.get_user_id(SQLAlchemy, username) get_lang_sql = "SELECT l.language FROM languages l, userinfo u WHERE l.user_id=u.id AND u.username=:username" get_languages = SQLAlchemy.session.execute(get_lang_sql, {"username": username}) try: languages = get_languages.fetchall() return languages except: return False
def edit_language(SQLAlchemy, username, language, new_language): user_id = database.get_user_id(SQLAlchemy, username) add_sql = "UPDATE languages SET language=:language WHERE user_id=:user_id"\ " AND language=:new_language" SQLAlchemy.session.execute(add_sql, { "language": language, "user_id": user_id, "new_language": new_language }) SQLAlchemy.session.commit() return True
def post_sign_in(): next_url = request.args['next'] email = request.form['email'] password = request.form['password'] # Check if the fields were filled. email_valid = email is not None and email != '' email_feedback = 'Please enter your email address' password_valid = password is not None and password != '' password_feedback = 'Please enter your password' # Check if the fields match our simple regex. #if email_valid: #email_valid = re.match(r'[^@]+@[^@]+\.[^@]+', email) #email_feedback = 'The value you’ve entered is not a valid email address' if password_valid: password_valid = len(password) >= 6 password_feedback = 'The password you’ve entered is too short to be valid' # Check if the user exists in the DB. if email_valid: email_valid = database.user_exists(email) email_feedback = 'The email you’ve entered doesn’t match any account' # Check if the password is correct. Do not run this check if the email is incorrect, since we can't tell if the user # inputted a correct password or not until they input a correct email. if email_valid and password_valid: salt, password_hash = database.get_password(email) password_valid = password_hash == scrypt.hash(password, salt) password_feedback = 'The password you’ve entered is incorrect' if email_valid and password_valid: # Set the login cookie. session['user_id'] = database.get_user_id(email) session['email'] = email return redirect(next_url) else: return render_template('sign_in.html', next=next_url, email=email, email_valid=email_valid, email_feedback=email_feedback, password=password, password_valid=password_valid, password_feedback=password_feedback)
def insert_question(): if not request.json: abort(400) category_id = db.get_category_id(request.json["Category"]) if(category_id == 0): return make_response(jsonify({'error': "couldn't find given category"}), 404) user_id = db.get_user_id(request.json["User"]) if(user_id == 0): return make_response(jsonify({'error': "couldn't find given user"}), 404) rows_affected = db.insert_question(request.json,category_id,user_id) if rows_affected: return make_response(jsonify({'result': "ok"}), 201) else: return make_response(jsonify({'error': "couldn't insert question"}), 400)
def process_jobs(args, UserSubmissionID, db_conn, sql): """ Submit the job for UserSubmissionID. Detail described in the header of this file. I'll write a more useful comment here once the function has been refactored. Inputs: ------- args - command line arguments UserSubmissionID - (int) from UserSubmissions.UserSubmissionID that drives the submission. db_conn - Active database connection. sql - Cursor object for database. """ logger = logging.getLogger('SubMit') fs.DEBUG = getattr(args, fs.debug_long) # Grabs UserSubmission and gcards as described in respective files username = database.get_username_for_submission(UserSubmissionID, sql) user_id = database.get_user_id(username, sql) scard = scard_helper.scard_class(database.get_scard_text_for_submission( UserSubmissionID, sql)) logging.debug('For UserSubmissionID = {}, user is {}'.format( UserSubmissionID, username)) scard_type = type_manager.manage_type(args, scard) sub_type = 'type_{}'.format(scard_type) print("sub_type is {0}".format(sub_type)) logger.debug('Type manager has determined type is: {}'.format( sub_type)) # Determine the number of jobs this submission # will produce in total. njobs = 1 if scard_type == 1: njobs = int(scard.jobs) elif scard_type == 2: njobs = lund_helper.count_files(scard.generator) # Dynamically load the script generation functions # from the type{sub_type} folder. script_set, script_set_funcs = script_factory.load_script_generators(sub_type) # Setup for different scard types the proper generation options. # If external lund files are provided, we go get them. set_scard_generator_options(scard, scard_type) gcard_loc = scard.configuration file_extension = "_UserSubmission_{0}".format(UserSubmissionID) if fs.use_mysql: DB_path = fs.MySQL_DB_path else: DB_path = fs.SQLite_DB_path params = {'table': 'Scards','UserSubmissionID': UserSubmissionID, 'username': username,'gcard_loc': gcard_loc, 'file_extension': file_extension,'scard': scard} # This is where we actually pass all arguements to write the scripts for index, script in enumerate(script_set): script_factory.script_factory(args, script, script_set_funcs[index], params, db_conn, sql) # Update entries in database submission_string = 'Submission scripts generated' update_tables.update_run_status(submission_string, UserSubmissionID, db_conn, sql) print("Submitting jobs to {0} \n".format("OSG")) #Hardcoded for this moment as we removed farm_name from scard farm_submission_manager.farm_submission_manager(args, UserSubmissionID, file_extension, scard, params, db_conn, sql) submission_string = 'Submitted to {0}'.format("OSG") update_tables.update_run_status(submission_string, UserSubmissionID, db_conn, sql)
def get_user_id(self, email_id): return db.get_user_id(self.db_cursor, email_id)