-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (64 loc) · 2.49 KB
/
app.py
File metadata and controls
72 lines (64 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask, render_template, request, redirect, url_for, flash, session
from models import init_db, get_db_connection
from auth import auth # Kullanıcı giriş işlemleri için auth blueprint
import os
app = Flask(__name__)
app.secret_key = 'your_secret_key'
# Blueprint'i ekle
app.register_blueprint(auth, url_prefix='/auth')
# Giriş yapılmasını zorunlu tutan decorator
@app.before_request
def require_login():
allowed_routes = ['auth.login', 'auth.register', 'static']
if 'user_id' not in session and request.endpoint not in allowed_routes:
return redirect(url_for('auth.login'))
# Ana Sayfa: Ürünlerin listelendiği sayfa
@app.route('/')
def index():
conn = get_db_connection()
products = conn.execute('SELECT * FROM products').fetchall()
conn.close()
return render_template('index.html', products=products)
# Yeni Ürün Ekle
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'POST':
name = request.form['name']
price = request.form['price']
conn = get_db_connection()
conn.execute('INSERT INTO products (name, price) VALUES (?, ?)', (name, price))
conn.commit()
conn.close()
flash('Yeni ürün başarıyla eklendi!', 'success')
return redirect(url_for('index'))
return render_template('add.html')
# Ürün Düzenle
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit(id):
conn = get_db_connection()
product = conn.execute('SELECT * FROM products WHERE id = ?', (id,)).fetchone()
conn.close()
if request.method == 'POST':
name = request.form['name']
price = request.form['price']
conn = get_db_connection()
conn.execute('UPDATE products SET name = ?, price = ? WHERE id = ?', (name, price, id))
conn.commit()
conn.close()
flash('Ürün başarıyla güncellendi!', 'success')
return redirect(url_for('index'))
return render_template('edit.html', product=product)
# Ürün Sil
@app.route('/delete/<int:id>', methods=['POST'])
def delete(id):
conn = get_db_connection()
conn.execute('DELETE FROM products WHERE id = ?', (id,))
conn.commit()
conn.close()
flash('Ürün başarıyla silindi!', 'success')
return redirect(url_for('index'))
# Uygulama çalıştırma
if __name__ == '__main__':
if not os.path.exists('database.db'):
init_db() # Veritabanını oluştur
app.run(debug=True)