def register(): if request.method == 'POST': username = request.form['username'] email = request.form['useremail'] password = request.form['password'] confirm_password = request.form['confirm_password'] db = get_db() error = None if not username: error = 'Username is required.' elif not email: error = 'Email is required' elif not password: error = 'Password is required.' elif password != confirm_password: error = 'Passwords does not match' elif db.execute( 'SELECT id FROM users WHERE username = ?', (username,) ).fetchone() is not None: error = 'User {} is already registered.'.format(username) if error is None: db.execute( 'INSERT INTO users (username,email, password) VALUES (?,?,?)', (username, email, generate_password_hash(password)) ) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def joined(): db = get_db() deeds = db.execute( "SELECT dISTINCT deeds.* FROM deeds INNER JOIN eventreg ON deeds.id = eventreg.eventid WHERE eventreg.userid =:id", {"id": g.user['id']}) return render_template('gooddeeds/joined.html', deeds=deeds)
def create(): if request.method == 'POST': title = request.form['title'] description = request.form['description'] location = request.form['location'] address = request.form['address'] date = request.form['date'] error = None if not title: error = 'Title is required.' elif not description: error = 'description is needed' elif not location: error = 'location is needed' elif not address: error = 'address is needed' elif not date: error = "date is required" if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO deeds (title, description, created, location, isdone, address, userid, date)' ' VALUES (?, ?, ?, ?, ?, ?, ?, ?)', (title, description, datetime.datetime.now(), location, False, address, g.user['id'], date)) db.commit() flash('{} created'.format(title)) return redirect(url_for('gooddeeds.dashboard')) return render_template('gooddeeds/createdeed.html')
def join(id): db = get_db() deedid = db.execute("SELECT id, title FROM deeds WHERE id =:theid", {"theid": id}) error = None title = None for item in deedid: title = item['title'] if db.execute( 'SELECT id FROM eventreg WHERE userid =:userid AND eventid =:event', { "userid": g.user['id'], "event": id }).fetchone() is not None: error = 'You are already registered.' if error is not None: flash(error) else: db.execute('INSERT INTO eventreg (userid, eventid) VALUES (?,?)', (g.user['id'], id)) db.commit() flash( 'Congratulations, you successfully registered for {}. Check back on the date provided' .format(title)) return redirect(url_for('gooddeeds.dashboard', id=id)) return render_template('gooddeeds/join.html')
def dashboard(): db = get_db() deeds = db.execute( "SELECT id, userid, address, description, title, date, location, isdone FROM deeds WHERE isdone =:bool ORDER BY created DESC", {"bool": False}) return render_template('gooddeeds/dashboard.html', deeds=deeds)
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 users WHERE id = ?', (user_id,) ).fetchone()
def update(id): db = get_db() deeds = db.execute("SELECT * FROM deeds WHERE id =:theid", { "theid": id }).fetchone() # is_id = None # for item in deedid: is_id = deeds['id'] if request.method == 'POST': title = request.form['title'] description = request.form['description'] location = request.form['location'] address = request.form['address'] date = request.form['date'] error = None if not title: error = 'Title is required.' elif not description: error = 'description is needed' elif not location: error = 'location is needed' elif not address: error = 'address is needed' elif not date: error = 'date is required' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE deeds SET title = ?, description = ?, date = ?, location = ?, address = ? WHERE id = ?', (title, description, date, location, address, is_id)) db.commit() flash('{} updated'.format(title)) return redirect(url_for('gooddeeds.dashboard')) return render_template('gooddeeds/update.html', deeds=deeds)
def delete(id): db = get_db() deedid = db.execute("SELECT id, title FROM deeds WHERE id =:theid", {"theid": id}) is_id = None title = None for item in deedid: is_id = item['id'] title = item['title'] db.execute('DELETE FROM deeds WHERE id = ?', (is_id, )) db.commit() flash('{} deleted'.format(title)) return redirect(url_for('gooddeeds.dashboard'))
def details(id): db = get_db() deeds = db.execute("SELECT * FROM deeds WHERE id =:ide", {"ide": id}) user_name = db.execute( "SELECT users.username FROM users INNER JOIN deeds ON users.id = deeds.userid WHERE deeds.id=:ide ", { "ide": id }).fetchone() event_data = db.execute( "SELECT COUNT(*) FROM eventreg WHERE eventid =:ide", { "ide": id }).fetchone() username = user_name['username'] eventdata = event_data[0] return render_template('gooddeeds/details.html', deeds=deeds, eventdata=eventdata, username=username)
def isdone(deed_id): db = get_db() deedid = db.execute("SELECT id, title, isdone FROM deeds WHERE id =:theid", {"theid": deed_id}) is_done = None is_id = None title = None for item in deedid: is_done = item['isdone'] is_id = item['id'] title = item['title'] db.execute("UPDATE deeds SET isdone = ? WHERE id = ?", ( not is_done, is_id, )) db.commit() flash('{} Completed status changed'.format(title)) return redirect(url_for('gooddeeds.dashboard'))
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() error = None user = db.execute( 'SELECT * FROM users WHERE username = ?', (username,) ).fetchone() if user is None: error = 'Incorrect username.' elif 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('gooddeeds.dashboard')) flash(error) return render_template('auth/login.html')
def completed(): db = get_db() deeds = db.execute( "SELECT id, userid, title, location, address, date, description, isdone FROM deeds WHERE isdone =:bool", {"bool": True}) return render_template('gooddeeds/completed.html', deeds=deeds)