def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] confirm_password = request.form['confirmPass'] db = flaskr.db.Database() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif not password == confirm_password: error = 'Passwords do not match' elif db.select( "SELECT id FROM user WHERE username = '******'".format(username) ) is not None: error = 'User {} is already registered.'.format(username) if error is None: db.insert( "INSERT INTO user (username, password) VALUES ('"+username+"', '"+generate_password_hash(password)+"')" ) # come back to this; db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def finishEdit(expense_id, expense_category, expense_title, expense_cost, expense_rate): if g.user is None: return redirect(url_for("auth.login")) if request.method == 'POST': category = request.form['category'] title = request.form['title'] cost = request.form['cost'] rate = request.form['rate'] error = None if not category: error = "Category Required" if not title: error = "Expense name required" if not cost: error = "Cost required" if not rate: error = "Rate required" if error is not None: flash(error) db = flaskr.db.Database() db.insert( "UPDATE expense SET title = '{}' , cost= '{}' , category = '{}', rate = '{}' WHERE id='{}' " .format(title, cost, category, rate, expense_id) ) return redirect(url_for('quickEntry.quickEntry')) return render_template('quickEntry/edit_entry.html', expense_category=expense_category, expense_title=expense_title, expense_cost=expense_cost, expense_rate=expense_rate)
def chooseBadge(badgeID): db = flaskr.db.Database() db.insert("UPDATE user SET displayedBadge='{}' WHERE id='{}'".format( badgeID, session.get('user_id'))) db.insert( "UPDATE friends SET displayedBadge='{}' WHERE user_id='{}'".format( badgeID, session.get('user_id'))) return redirect(url_for('profiles.profile'))
def deleteEntry(expense_id): if g.user is None: return redirect(url_for("auth.login")) db = flaskr.db.Database() if request.method == 'POST': db.insert( "DELETE FROM expense WHERE id = '{}'".format(expense_id) ) return redirect(url_for('quickEntry.quickEntry'))
def addFriends(): if request.method == 'POST': username = request.form['username'] db = flaskr.db.Database() name = None name = db.select( "SELECT * FROM user WHERE username='******'".format(username)) id = None if name is not None: id = db.select( "SELECT friend_id FROM friends WHERE friend_id = '{}' AND user_name='{}'" .format(session.get('user_id'), username)) if id is None: db.insert( "INSERT INTO friends VALUES ( '{}' , '{}' , '{}' , '{}' , '{}' , '{}' , '{}','{}' ,'{}','{}')" .format(name['username'], session.get('user_id'), name['occupation'], name['location'], name['name1'], name['age'], name['income'], name['anonymous'], name['id'], name['displayedBadge'])) return redirect(url_for('profiles.profile')) flash(id) if id['friend_id'] != session.get('user_id'): db.insert( "INSERT INTO friends VALUES ( '{}' , '{}' , '{}' , '{}' , '{}' , '{}', '{}','{}','{}')" .format(name['username'], session.get('user_id'), name['occupation'], name['location'], name['name1'], name['age'], name['income'], name['anonymous'], name['id'])) else: err = "already friends" flash(err) return redirect(url_for('profiles.addFriends')) else: name = "UserName not found" flash(name) return redirect(url_for('profiles.addFriends')) return redirect(url_for('profiles.profile')) return render_template('profiles/addFriends.html')
def settings(): if g.user is None: return redirect(url_for("auth.login")) db = flaskr.db.Database() user = db.selectall("SELECT * FROM user WHERE id = '{}'".format( session.get('user_id'))) if request.method == 'POST': name1 = request.form['fullname'] username = request.form['username'] anonymous = request.form['anonymous'] db.insert("UPDATE user SET name1 = '{}' WHERE id='{}' ".format( name1, session.get('user_id'))) db.insert("UPDATE user SET username = '******' WHERE id='{}' ".format( username, session.get('user_id'))) db.insert("UPDATE user SET anonymous = '{}' WHERE id='{}' ".format( anonymous, session.get('user_id'))) return redirect(url_for('settings.settings')) return render_template('settings/index.html', user=user)
def profile(): if g.user is None: return redirect(url_for("auth.login")) db = flaskr.db.Database() print(session.get('user_id')) expenses = db.selectall("SELECT * FROM expense WHERE author_id = '{}'" "ORDER BY category, cost".format( session.get('user_id'))) user = db.selectall("SELECT * FROM user WHERE id = '{}'".format( session.get('user_id'))) friends = db.selectall( "SELECT * FROM friends WHERE friend_id= '{}'".format( session.get('user_id'))) numFriends = len(friends) now = datetime.datetime.now() total_expenses = { "daily": 0, "weekly": 0, "monthly": 0, "yearly": 0, "oneTime": 0, "total": 0 } total_category = { "Food": 0, "Utilities": 0, "Recreational": 0, "Medical": 0, "Rent / Mortgage": 0, "Phone": 0, "Vehicle": 0, "Other": 0 } for index, expense in enumerate(expenses): total_category[expense['category']] = total_category[ expense['category']] + expense['cost'] timeago = now - expense['created'] secs = timeago.total_seconds() days = round(secs // 86400) hours = round((secs - days * 86400) // 3600) minutes = round((secs - days * 86400 - hours * 3600) // 60) seconds = round(secs - days * 86400 - hours * 3600 - minutes * 60) if expense['rate'] == 'Daily': total_expenses['daily'] = total_expenses['daily'] + expense['cost'] elif expense['rate'] == 'Weekly': total_expenses[ 'weekly'] = total_expenses['weekly'] + expense['cost'] elif expense['rate'] == 'Monthly': total_expenses[ 'monthly'] = total_expenses['monthly'] + expense['cost'] elif expense['rate'] == 'Yearly': total_expenses[ 'yearly'] = total_expenses['yearly'] + expense['cost'] elif expense['rate'] == 'One Time': total_expenses[ 'oneTime'] = total_expenses['oneTime'] + expense['cost'] if days == -1: expense['timeago'] = 'Just created' else: expense['timeago'] = str(days) + " days " + str( hours) + " hours " + str(minutes) + " minutes " + str( seconds) + " seconds ago" expenses[index] = expense total_expenses['total'] = total_expenses['daily'] * 365 + total_expenses[ 'weekly'] * 52 + total_expenses['monthly'] * 12 + total_expenses[ 'yearly'] + total_expenses['oneTime'] total = total_expenses['total'] // 12 if request.method == 'POST': name1 = request.form['fullname'] occupation = request.form['occupation'] age = request.form['age'] location = request.form['location'] income = request.form['income'] badge2 = 1 error = None anonymous = request.form['anonymous'] if not name1: error = "Please enter your name!" if not occupation: error = "Please enter your occupation or field!" if not age: error = "Please enter your age!" if not location: error = "Please enter your Location!" if not income: error = "Please enter an estimate of your income value!" if error is not None: flash(error) else: db.insert("UPDATE user SET name1 = '{}' WHERE id='{}' ".format( name1, session.get('user_id'))) db.insert( "UPDATE user SET occupation = '{}' WHERE id='{}' ".format( occupation, session.get('user_id'))) db.insert("UPDATE user SET age = '{}' WHERE id='{}' ".format( age, session.get('user_id'))) db.insert("UPDATE user SET location = '{}' WHERE id='{}' ".format( location, session.get('user_id'))) db.insert("UPDATE user SET income = '{}' WHERE id='{}' ".format( income, session.get('user_id'))) db.insert("UPDATE user SET badge2 = '{}' WHERE id='{}' ".format( badge2, session.get('user_id'))) db.insert("UPDATE user SET anonymous = '{}' WHERE id='{}' ".format( anonymous, session.get('user_id'))) return redirect(url_for('profiles.profile')) # Will list all of a users expenses if user[0]['income'] is None: user[0]['income'] = 0 return render_template('profiles/profile.html', infographics=infographics(total, user[0]['income'] // 12), total_category=total_category, expenses=expenses, total_expenses=total_expenses, user=user[0], friends=friends, numFriends=numFriends)
def quickEntry(): if g.user is None: return redirect(url_for("auth.login")) db = flaskr.db.Database() print(session.get('user_id')) #random number for tip gen range should correspond to the range of tip_ids in database randId = random.randint(1, 9) #gets a random tip tips = db.select( "SELECT * FROM tips WHERE tip_id = '{}'".format(randId) ) expenses = db.selectall( "SELECT * FROM expense WHERE author_id = '{}'" "ORDER BY category, cost".format(session.get('user_id')) ) user = db.selectall( "SELECT * FROM user WHERE id = '{}'".format(session.get('user_id')) ) now = datetime.datetime.now() total_expenses = { "daily": 0, "weekly": 0, "monthly": 0, "yearly": 0, "oneTime": 0, "total": 0 } for index, expense in enumerate(expenses) : timeago = now - expense['created'] secs = timeago.total_seconds() days = round(secs//86400) hours = round((secs - days*86400)//3600) minutes = round((secs - days*86400 - hours*3600)//60) seconds = round(secs - days*86400 - hours*3600 - minutes*60) if expense['rate'] == 'Daily' : total_expenses['daily'] = total_expenses['daily'] + expense['cost'] elif expense['rate'] == 'Weekly' : total_expenses['weekly'] = total_expenses['weekly'] + expense['cost'] elif expense['rate'] == 'Monthly' : total_expenses['monthly'] = total_expenses['monthly'] + expense['cost'] elif expense['rate'] == 'Yearly' : total_expenses['yearly'] = total_expenses['yearly'] + expense['cost'] elif expense['rate'] == 'One Time' : total_expenses['oneTime'] = total_expenses['oneTime'] + expense['cost'] if days == -1 : expense['timeago'] = 'Just created' else : expense['timeago'] = str(days) + " days " + str(hours) + " hours " + str(minutes) + " minutes " + str(seconds) + " seconds ago" expenses[index] = expense total_expenses['total'] = total_expenses['daily'] * 365 + total_expenses['weekly'] * 52 + total_expenses['monthly'] * 12 + total_expenses['yearly'] + total_expenses['oneTime'] if request.method == 'POST': category = request.form['category'] title = request.form['title'] cost = request.form['cost'] rate = request.form['rate'] badge3 = 1 badge4 = 1 error = None if not title: error = "Category required" if not title: error = "Expense name required" if not cost: error = "Cost required" if not rate: error = "Rate Required" if error is not None: flash(error) db.insert( "UPDATE user SET badge3 = '{}' WHERE id='{}' ".format(badge3, session.get('user_id')) ) if rate != "One Time": db.insert( "UPDATE user SET badge4 = '{}' WHERE id='{}' ".format(badge4, session.get('user_id')) ) print(db.insert( "INSERT INTO expense (title, cost, author_id, category, rate) VALUES " "('" + title + "', '" + str(cost) + "', '" + str(g.user['id']) + "', '" + category + "', '" + rate + "')" )) return redirect(url_for('quickEntry.quickEntry')) if user[0]['income'] is None: user[0]['income'] = 0 return render_template('quickEntry/default_entry.html', expenses=expenses, total_expenses=total_expenses, user=user[0],tips = tips)