def change_pass(): if (request.method == 'POST'): old_pass = request.form['old_password'] new_pass = request.form['new_password'] error = None db = get_db() user = db.execute('SELECT * FROM user').fetchone() if (not check_password_hash(user['password'], old_pass)): error = 'Old password incorrect.' if (error is None): db.execute( ''' UPDATE user SET password = ? WHERE id = ? ''', (generate_password_hash(new_pass), user['id'])) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/change-pass.html')
def register(): db = get_db() if (db.execute('SELECT id FROM user LIMIT 1').fetchone() is not None): flash('Already exist a user.') return redirect(url_for('auth.login')) if (request.method == 'POST'): password = request.form['password'] error = None if (not password): error = 'Password is required.' elif (db.execute('SELECT id FROM user WHERE password = ?', (password, )).fetchone() is not None): error = 'por enquanto nada!' if (error is None): db.execute('INSERT INTO user (password) VALUES (?)', (generate_password_hash(password), )) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def create(): if(request.method == 'POST'): name = request.form['name'] surname = request.form['surname'] email = request.form['email'] number = request.form['number'] error = None if(not name): error = 'Name is required.' elif(not number): error = 'Number is required.' if(error is not None): flash(error) else: db = get_db() db.execute( '''INSERT INTO contact (name, surname, email, num, user_id) VALUES (?, ?, ?, ?, ?)''', (name, surname, email, number, g.user['id']) ) db.commit() return redirect(url_for('contacts.index')) return render_template('contact/create.html')
def delete(id): get_contact(id) #<<< dúvida, para quê isso ? db = get_db() db.execute('DELETE FROM contact WHERE id = ?', (id,)) db.commit() return redirect(url_for('contacts.index'))
def update(id): contact = get_contact(id) if(request.method == 'POST'): name = request.form['name'] surname = request.form['surname'] email = request.form['email'] number = request.form['number'] error = None if(not name): error = 'Name is required.' elif(not number): error = 'Number is required.' if(error is not None): flash(error) else: db = get_db() db.execute( '''UPDATE contact SET name = ?, surname = ?, email = ?, num = ? WHERE id = ?''', (name, surname, email, number, id) ) db.commit() return redirect(url_for('contacts.index')) return render_template('contact/update.html', contact=contact)
def load_logged_in_user(): user_id = session.get('user_id') if (user_id is None): g.user = None else: g.user = get_db().execute('SELECT * FROM user WHERE id = ?', (user_id, )).fetchone()
def get_contact(id, check_user=True): contact = get_db().execute( '''SELECT c.id, create_date, user_id, name, surname, email, num FROM contact c JOIN user u ON c.user_id = u.id WHERE c.id = ?''', (id,) ).fetchone() if(contact is None): abort(404, "Contact id {0} doesn't exist." .format(id)) if(check_user and contact['user_id'] != g.user['id']): abort(403) return contact
def index(): db = get_db() contacts = None contactsASC = None contactsDESC = None contactsRecently = db.execute( ''' SELECT c.id, create_date, user_id, name, surname, email, num FROM contact c JOIN user u ON c.user_id = u.id ORDER BY create_date DESC ''' ).fetchall() # testando as posibilidades if(request.method == 'POST'): print('Houve um POST') contactsASC = db.execute( ''' SELECT c.id, create_date, user_id, name, surname, email, num FROM contact c JOIN user u ON c.user_id = u.id ORDER BY name ASC ''' ).fetchall() contactsDESC = db.execute( ''' SELECT c.id, create_date, user_id, name, surname, email, num FROM contact c JOIN user u ON c.user_id = u.id ORDER BY name DESC ''' ).fetchall() contacts = contactsRecently return render_template('contact/index.html', contacts=contacts)
def login(): db = get_db() if (db.execute('SELECT id FROM user LIMIT 1').fetchone() is None): return redirect(url_for('auth.register')) if (request.method == 'POST'): password = request.form['password'] error = None user = db.execute('SELECT * FROM user').fetchone() if (not check_password_hash(user['password'], password)): error = 'Incorrect password.' if (error is None): session.clear() session['user_id'] = user['id'] return redirect(url_for('index')) flash(error) return render_template('auth/login.html')