Exemplo n.º 1
0
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 user WHERE id = ?', (user_id,)
        ).fetchone()
        g.items = get_db().execute(
            'SELECT sum(amount) FROM cart WHERE user_id = ?', (g.user["id"],)
        ).fetchone()["sum(amount)"]
Exemplo n.º 2
0
def index():
    db = get_db()
    foods = db.execute(
                'SELECT * FROM menu WHERE type = ?', ('food',)
            ).fetchall()
    
    drinks = db.execute(
                'SELECT * FROM menu WHERE type = ?', ('drink',)
            ).fetchall()
    return render_template('menu/index.html', foods=foods, drinks=drinks)
Exemplo n.º 3
0
def delete():
    data = request.get_json()
    product_id = data["menu_id"]
    user_id = data["user_id"]
    if g.user["id"] == user_id:
        db = get_db()
        cursor = db.cursor()
        cursor.execute('DELETE FROM cart WHERE product_id = ? AND user_id = ?',
                       (
                           product_id,
                           user_id,
                       ))
        db.commit()
        return json.dumps(True)
Exemplo n.º 4
0
def add():
    data = request.get_json()
    product_id = data["menu_id"]
    user_id = data["user_id"]
    amount = data["amount"]
    db = get_db()
    cursor = db.cursor()
    cursor.execute(
        'INSERT INTO cart(product_id, user_id, amount) VALUES(?, ? , ?) ON CONFLICT(product_id, user_id) DO UPDATE SET amount = amount + ?',
        (
            product_id,
            user_id,
            amount,
            amount,
        ))
    db.commit()
    return json.dumps(True)
Exemplo n.º 5
0
def order():
    data = request.get_json()
    user_id = data["user_id"]
    if user_id == g.user["id"]:
        db = get_db()
        # get all of the cart items of user
        cart_items = db.execute(
            'SELECT menu_id, amount, unit_price, amount*unit_price AS subtotal_price FROM cart JOIN menu ON cart.product_id = menu.menu_id WHERE user_id = ?',
            (g.user["id"], )).fetchall()
        cart_data = []
        total = 0
        for items in cart_items:
            item = {}
            item["menu_id"] = items["menu_id"]
            item["amount"] = items["amount"]
            item["unit_price"] = items["unit_price"]
            item["subtotal_price"] = items["subtotal_price"]
            total += items["subtotal_price"]
            cart_data.append(item)
        # insert order
        cursor = db.cursor()
        cursor.execute('INSERT INTO orders(user_id, total) VALUES(?, ?)', (
            g.user["id"],
            total,
        ))
        last_row_id = cursor.lastrowid
        print("Last row id: " + str(last_row_id))
        # insert order items
        for item in cart_data:
            cursor.execute(
                'INSERT INTO order_item(product_id, amount, unit_price, subtotal, order_id) VALUES(?, ?, ?, ?, ?)',
                (
                    item["menu_id"],
                    item["amount"],
                    item["unit_price"],
                    item["subtotal_price"],
                    last_row_id,
                ))
        # delete from cart
        cursor.execute('DELETE FROM cart WHERE user_id = ?', (g.user["id"], ))
        db.commit()
        return json.dumps(True)
    return json.dumps(False)
Exemplo n.º 6
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        db = get_db()
        error = None

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

        if user is None or not check_password_hash(user['password'], password):
            error = 'Incorrect username or password.'
        
        if error is None:
            session.clear()
            session.permanent = True
            session['user_id'] = user['id']
            return redirect(url_for('index'))
        
        flash(error)
    return render_template('auth/login.html')
Exemplo n.º 7
0
def detail(id):
    db = get_db()
    item = db.execute(
        'SELECT * FROM menu WHERE menu_id = ?', (id,)
    ).fetchone()
    return render_template('menu/detail.html', item=item)
Exemplo n.º 8
0
def register():
    if request.method == 'POST':
        name = request.form['name']
        username = request.form['username']
        password = request.form['password']
        email = request.form['email']
        address = request.form['address']
        phone = request.form['phone']
        image_url = url_for('static', filename='img/default.png')
        role = 'customer'

        db = get_db()
        error = None

        if name is '':
            error = "Name required."
        elif username is '':
            error = "Username required."
        elif len(password) < 6:
            error = "Password needs to be at least 6 characters long."
        elif address is '':
            error = "Address required."
        elif email is '':
            error = "Email required."
        elif email_invalid(email):
            error = '{} is not a valid email.'.format(email)
        elif len(phone) < 9 or len(phone) > 12:
            error = "Please enter 9 to 12 digits phone number"
        elif db.execute(
           'SELECT id FROM user WHERE username = ?', (username,)
        ).fetchone() is not None:
            error = 'User {} is already registered.'.format(username)
        elif db.execute(
            'SELECT id FROM user WHERE email = ?', (email,)
        ).fetchone() is not None:
            error = 'Email {} is already registered.'.format(email)
        
        if error is None:
            cursor = db.cursor()
            cursor.execute(
                'INSERT INTO user(name, username, password, email, address, phone, image_url, role) '
                ' VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
                (
                    name,
                    username,
                    generate_password_hash(password),
                    email,
                    address,
                    phone,
                    image_url,
                    role
                )
            )
            last_row_id = cursor.lastrowid
            db.commit()
            session.clear()
            session.permanent = True
            session['user_id'] = last_row_id
            return redirect(url_for('index'))
        flash(error)
    return render_template('auth/register.html')
Exemplo n.º 9
0
def validate_username(username):
    db = get_db()
    user = db.execute('SELECT id FROM user WHERE username = ?', (username,)).fetchone()
    if user is None:
        return json.dumps(True)
    return json.dumps(False)
Exemplo n.º 10
0
def index():
    db = get_db()
    cart_items = db.execute(
        'SELECT menu_id, name, amount, unit_price, amount*unit_price AS total_price, image_url FROM cart JOIN menu ON cart.product_id = menu.menu_id WHERE user_id = ?',
        (g.user["id"], )).fetchall()
    return render_template('cart/index.html', cart_items=cart_items)