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 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 login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = flaskr.db.Database()
        error = None
        user = db.select(
            "SELECT * FROM user WHERE username = '******'".format(username)
        )

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()  # stores user data into an encrypted cookie
            session['user_id'] = user['id']
            return redirect(url_for('homePages.homePage'))

        flash(error)

    return render_template('auth/login.html')
def viewFriend(username, field, location, name, age, income, anon, id):
    db = flaskr.db.Database()
    badge = db.select(
        "SELECT displayedBadge FROM user WHERE username = '******'".format(
            username))
    badge1 = badge['displayedBadge']
    expenses = db.selectall("SELECT * FROM expense WHERE author_id = '{}'"
                            "ORDER BY category, cost".format(id))
    user = db.selectall("SELECT * FROM user WHERE id = '{}'".format(id))
    user = user[0]

    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']
        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']
        expenses[index] = expense

    total_expenses['total'] = total_expenses['daily'] * 365 + total_expenses[
        'weekly'] * 52 + total_expenses['monthly'] * 12 + total_expenses[
            'yearly'] + total_expenses['oneTime']
    the_total_expenses = total_expenses['total'] // 12

    return render_template('profiles/viewFriend.html',
                           badge=badge1,
                           total_category=total_category,
                           total_expenses=total_expenses,
                           infographics=infographics(the_total_expenses,
                                                     (int(income) // 12)),
                           user=user,
                           username=user['username'],
                           field=field,
                           location=user['location'],
                           name=user['name1'],
                           age=user['age'],
                           income=user['income'],
                           anon=anon,
                           id=id)
Esempio n. 5
0
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)