Beispiel #1
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        elif db.execute('SELECT id FROM aquarist WHERE username = ?',
                        (username, )).fetchone() is not None:
            error = f'Aquarist {username} is already registered.'

        if error is None:
            db.execute(
                'INSERT INTO aquarist (username, password) VALUES (?, ?)',
                (username, generate_password_hash(password)))
            db.commit()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
Beispiel #2
0
def update(id):
    tank = get_tank(id)

    if request.method == 'POST':
        name = request.form['name']
        error = None

        if not name:
            error = 'Name is required.'

        if error is not None:
            flash(error)
        else:
            length = float(request.form['length'])
            width = float(request.form['width'])
            depth = float(request.form['depth'])
            volume = Volumetrics.calculate_volume(length, width, depth)

            db = get_db()
            db.execute(
                'UPDATE tank SET name = ?, length = ?, width = ?, depth = ?, volume = ?'
                ' WHERE id = ?', (name, length, width, depth, volume, id))
            db.commit()
            flash('Tank updated!')

            return redirect(url_for('aquarium.index'))

    return render_template('aquarium/index.html', tank=tank)
Beispiel #3
0
def create():
    if request.method == 'POST':
        name = request.form['name']
        length = float(request.form['length'])
        width = float(request.form['width'])
        depth = float(request.form['depth'])
        volume = Volumetrics.calculate_volume(length, width, depth)

        error = None

        if not (name and length and width and depth):
            error = 'Please enter all fields.'

        if error is not None:
            flash(error)
        else:

            db = get_db()
            db.execute(
                'INSERT INTO tank (tank_owner, name, length, width, depth, volume)'
                ' VALUES (?, ?, ?, ?, ?, ?)',
                (g.user['id'], name, length, width, depth, volume))

            db.commit()
            flash('New tank created!')

            return redirect(url_for('aquarium.index'))

    return render_template('aquarium/index.html')
Beispiel #4
0
def index():
    db = get_db()
    tanks = db.execute(
        'SELECT t.id, name, length, width, depth, volume, tank_owner, username'
        ' FROM tank t JOIN aquarist a ON t.tank_owner = a.id'
        ' ORDER BY name DESC').fetchall()
    return render_template('aquarium/index.html', tanks=tanks)
Beispiel #5
0
def load_logged_in_user():
    aquarist_id = session.get('aquarist_id')

    if aquarist_id is None:
        g.user = None
    else:
        g.user = get_db().execute('SELECT * FROM aquarist WHERE id = ?',
                                  (aquarist_id, )).fetchone()
Beispiel #6
0
def delete(id):
    get_tank(id)
    db = get_db()
    db.execute('DELETE FROM tank WHERE id = ?', (id, ))
    db.commit()
    flash('Tank deleted!')

    return redirect(url_for('aquarium.index'))
Beispiel #7
0
def get_tank(id, check_aquarist=True):
    tank = get_db().execute(
        'SELECT t.id, name, length, width, depth, volume, tank_owner'
        ' FROM tank t JOIN aquarist a ON t.tank_owner = a.id'
        ' WHERE t.id = ?', (id, )).fetchone()

    if tank is None:
        abort(404, "Tank id {0} doesn't exist.".format(id))

    if check_aquarist and tank['tank_owner'] != g.user['id']:
        abort(403)

    return tank
Beispiel #8
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None

        aquarist = db.execute('SELECT * FROM aquarist WHERE username = ?',
                              (username, )).fetchone()

        if aquarist is None:
            error = 'Username is incorrect or doesnt exist.'
        elif not check_password_hash(aquarist['password'], password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()
            session['aquarist_id'] = aquarist['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('aquarium/index.html')