def register(): if current_user.is_authenticated: return redirect("/home") form = RegisterationForm() if form.validate_on_submit(): password = form.password.data username = form.username.data email = form.email.data registering_std = Student(email, password) cursor.execute("select * from student") all_users = cursor.fetchall() check = True for user in all_users: if user[2] == email: check = False elif user[1] == username: check = False if check == True: registering_std.register(username) registering_std.set_id() registering_std.set_username() flash(f'Account created for {form.username.data}!', 'Success') return redirect("/login") else: return redirect("/register") return render_template('register.html', title='Register', form=form)
def login(): if current_user.is_authenticated: return redirect("/home") form = LoginForm() if form.validate_on_submit(): email = form.email.data password = form.password.data std = Student(email, password) cursor.execute("select username from student where email = %s", [email]) username = cursor.fetchone()[0] std.username = username cursor.execute("select SID from student where email = %s", [email]) sid = cursor.fetchone()[0] std.id = sid if std.login(): logged_in_users.append(std) login_user(std, remember=form.remember.data) next_page = request.args.get('next') flash('you have been loggd in!', 'success') return redirect(next_page) if next_page else redirect("/home") else: flash('log in unsuccessful, invalid input', 'danger') return redirect("/login") return render_template('login.html', title='login', form=form)
def login(self): cursor.execute( "select * from student where email = %s and password = %s", [self.email, self.password]) user = cursor.fetchall() if len(user) == 1: return True else: return False
def language_class(class_id): cursor.execute('select file_path from class,takes where CID = %s', [class_id]) file_path = cursor.fetchone()[0] cursor.execute('select language, level from class where CID=%s', [class_id]) lang_lvl = cursor.fetchone() value = [file_path, lang_lvl[0], lang_lvl[1]] return render_template("language_class.html", title="Classes", value=value)
def delete(): if current_user.is_authenticated: username = current_user.username cursor.execute("select SID from student where username = %s ", [username]) sid = cursor.fetchone()[0] cursor.execute("DELETE from student where SID = %s ", [sid]) database.commit() flash('your account has been deleted', 'success') logout_user() if len(logged_in_users) != 0: logged_in_users.pop(0) return render_template('home.html', posts=posts)
def languages(): form = SelectLanguageForm() if request.method == 'POST': lan = form.language.data lvl = form.level.data cursor.execute( "select CID from class where language = %s and level = %s", [lan, lvl]) cid = cursor.fetchone()[0] print(current_user.username) cursor.execute( "insert into takes ( std_id , cls_id ) values ( %s, %s)", [current_user.id, cid]) database.commit() flash('Class Added, click here to access it', 'success') return render_template("languages.html", form=form)
def profile(): form = UpdateAccountForm() if form.validate_on_submit(): if form.username.data != current_user.username: current_user.username = form.username.data print(current_user.id) cursor.execute("UPDATE student SET username = %s WHERE SID = %s;", [form.username.data, current_user.id]) database.commit() if form.email.data != current_user.email: current_user.email = form.email.data cursor.execute("UPDATE student SET email = %s WHERE SID = %s;", [form.email.data, current_user.id]) database.commit() flash('your account has been updated', 'success') image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template("profile.html", title='Account', image_file=image_file, form=form)
class DeleteLanguageForm(FlaskForm): cursor.execute('select distinct language from class') langs = cursor.fetchall() choices = [] for lang in langs: choices.append((lang[0], lang[0])) language = SelectField('language', choices=choices) level = SelectField('level', choices=[('A1','A1'), ('A2','A2'), ('B1','B1'), ('B2','B2'), ('C1','C1'), ('C2','C2')]) submit = SubmitField('delete')
def classes(): form = DeleteLanguageForm() if request.method == 'POST': # I was working on this ( not finished ) cursor.execute( 'select distinct language from class,takes where std_id = %s', [current_user.id]) langs = cursor.fetchall() choices = [] for lang in langs: choices.append((lang[0], lang[0])) cursor.execute( 'select level from class,takes where language = %s and std_id=%s', [lang[0], current_user.id]) lvls = cursor.fetchall() print(lvls) choices_lvl = [] for lvl in lvls: choices_lvl.append((lvl[0], lvl[0])) form.language.choices = choices form.level.choices = choices_lvl # level cursor.execute( 'select distinct level from class,takes where std_id = %s', [current_user.id]) lvls = cursor.fetchall() choices = [] for lvl in lvls: choices.append((lvl[0], lvl[0])) # loading classes sid = current_user.id cursor.execute( 'select language,level,CID from class,takes where CID = cls_id and std_id = %s;', [sid]) taken_classes = cursor.fetchall() return render_template("classes.html", title="Classes", posts=taken_classes, form=form)
def set_username(self): cursor.execute('select * from student where email = %s', [self.email]) self.username = cursor.fetchone()[1] return self.username
def set_id(self): cursor.execute('select * from student where email = %s', [self.email]) self.id = cursor.fetchone()[0] return self.id
def register(self, username): cursor.execute( "insert into student(username,email,password)" "values(%s,%s,%s)", (username, self.email, self.password)) database.commit()