-
Notifications
You must be signed in to change notification settings - Fork 0
/
aa.py
163 lines (128 loc) · 4.84 KB
/
aa.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# app.py
from flask import Flask, render_template, redirect, url_for, request, flash
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired, NumberRange
from flask_login import LoginManager, UserMixin, login_user, login_required, current_user, logout_user
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///products.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
# User model for login management
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True, nullable=False)
password = db.Column(db.String(50), nullable=False)
is_admin = db.Column(db.Boolean, default=False)
# Product model for the products database
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
price = db.Column(db.Float, nullable=False)
# Login form
class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = StringField('Password', validators=[DataRequired()])
# Product form for adding/modifying products
class ProductForm(FlaskForm):
name = StringField('Product Name', validators=[DataRequired()])
price = IntegerField('Product Price', validators=[DataRequired(), NumberRange(min=0)])
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data, password=form.password.data).first()
if user:
login_user(user)
flash('Logged in successfully!', 'success')
return redirect(url_for('index'))
else:
flash('Login failed. Check your username and password.', 'danger')
return render_template('login.html', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/')
@login_required
def index():
products = Product.query.all()
return render_template('index.html', products=products)
@app.route('/admin')
@login_required
def admin():
if not current_user.is_admin:
flash('You do not have admin privileges.', 'danger')
return redirect(url_for('index'))
products = Product.query.all()
return render_template('admin.html', products=products)
@app.route('/add_product', methods=['GET', 'POST'])
@login_required
def add_product():
if not current_user.is_admin:
flash('You do not have admin privileges.', 'danger')
return redirect(url_for('index'))
form = ProductForm()
if form.validate_on_submit():
new_product = Product(name=form.name.data, price=form.price.data)
db.session.add(new_product)
db.session.commit()
flash('Product added successfully!', 'success')
return redirect(url_for('admin'))
return render_template('add_product.html', form=form)
@app.route('/edit_product/<int:product_id>', methods=['GET', 'POST'])
@login_required
def edit_product(product_id):
if not current_user.is_admin:
flash('You do not have admin privileges.', 'danger')
return redirect(url_for('index'))
product = Product.query.get_or_404(product_id)
form = ProductForm(obj=product)
if form.validate_on_submit():
product.name = form.name.data
product.price = form.price.data
db.session.commit()
flash('Product updated successfully!', 'success')
return redirect(url_for('admin'))
return render_template('edit_product.html', form=form, product=product)
@app.route('/delete_product/<int:product_id>', methods=['POST'])
@login_required
def delete_product(product_id):
if not current_user.is_admin:
flash('You do not have admin privileges.', 'danger')
return redirect(url_for('index'))
product = Product.query.get_or_404(product_id)
db.session.delete(product)
db.session.commit()
flash('Product deleted successfully!', 'success')
return redirect(url_for('admin'))
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)
<!-- login.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="POST">
{{ form.hidden_tag() }}
<p>Username: {{ form.username }}</p>
<p>Password: {{ form.password }}</p>
<p><input type="submit" value="Login"></p>
</form>
</body>
</html>