Exemplo n.º 1
0
def login():
    """This function logs a user into the system.
        Upon a GET request a LoginForm will be shown to the user.
        Upon a POST request the form will be validated and if valid the users
            specified password will be hashed and compared to the stored
            password.
            Should they be equal the user will be logged in (as such
                his User object will be stored in the session) and redirected to
                    the default page of the authentication-module.
                Is this not the case or if the form was invalid in the first
                    place, he will be shown the form again.
    """
    form = LoginForm(request.form)

    if request.method == 'POST' and form.validate():
        user = User.objects(username = form.username.data).first()
        if user is not None:
            if user.password == generateHash(form.password.data):
                session['user'] = user
                session['currency'] = u"\u20AC"
                return redirect(session.get('next', url_for('budget.showSummary')))

        logger.info('User %s has logged in.' % user.username)
        flash('The specified username and/or password were incorrect.')
    return render_template('auth/login.html', form = form)
Exemplo n.º 2
0
def addEntry(template, asAsset = False):
    form = AddEntryForm(request.form)
    # Load the categories from the DB into the SelectField
    form.loadCategories()

    logger.debug('addEntry has been called.')

    if request.method == 'POST' and form.validate():
        logger.debug('A form has been submitted to addEntry.')

        entry = Entry()
        logger.debug('Trying to populate form.')
        form.populate_obj(entry)

        logger.debug('Entry: {0}, {1}, {2}'.format(entry.amount, entry.description, entry.category))

        # If this is an expense, multiply the amount by (-1).
        # And also add a category to it.
        if not asAsset:
            entry.amount = entry.amount * (-1)
            entry.category = Category.objects(id = ObjectId(entry.category)).first()
        else:
            entry.category = Category.objects(name = 'None').first()

        # Insert owner into the ReferenceField.
        userId = ObjectId(session.get('user')['_id']['$oid'])
        entry.owner = User.objects(id = userId).first()
        entry.save()

        logger.debug('{0} added Income({1}, {2}, {3})'.format(
            session.get('user')['username'], entry.amount, entry.description,
                entry.category.name))

        flash('Your entry has been added.')
        return redirect(url_for('budget.default'))
    return render_template(template, form = form)