Skip to content

Commit dde7b64

Browse files
committed
Add an example fomanticui-test
1 parent dbc3be6 commit dde7b64

13 files changed

Lines changed: 1388 additions & 0 deletions

File tree

examples/fomanticui-test/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# An example to test FomanticUI.
2+
3+
## Installation
4+
5+
Activate your virtual environment. Then use the package manager [pip](https://pip.pypa.io/en/stable/) to install the required packages.
6+
7+
```bash
8+
pip install -r requirements.txt
9+
```
10+
To run the Flask application, navigate to the example's directory and execute the following command:
11+
```bash
12+
flask run
13+
```
14+
or
15+
```bash
16+
python app.py
17+
```
18+
The application will start running on http://127.0.0.1:5000/.

examples/fomanticui-test/app.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from bp import bp
2+
from config import MyConfig
3+
from fake import generate_fake_data
4+
from flask import Flask
5+
from models import db_init
6+
from my_admin.basic_admin import admin as basic_admin
7+
from my_admin.pro_admin import admin as pro_admin
8+
from my_admin.super_admin import admin as super_admin
9+
10+
app = Flask(__name__)
11+
12+
app.config.from_object(MyConfig)
13+
db_init(app)
14+
generate_fake_data(app)
15+
16+
# Init admin instances
17+
18+
basic_admin.init_app(app)
19+
super_admin.init_app(app)
20+
pro_admin.init_app(app)
21+
22+
23+
app.register_blueprint(bp)
24+
25+
26+
if __name__ == "__main__":
27+
app.run(debug=True)

examples/fomanticui-test/bp.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from flask import Blueprint
2+
from flask import redirect
3+
from flask import render_template_string
4+
from flask import url_for
5+
6+
bp = Blueprint("main", __name__, template_folder="my_admin/templates")
7+
8+
9+
@bp.route("/")
10+
def index():
11+
return render_template_string("""
12+
<h1>Hello, World!</h1>
13+
<ul>
14+
<li>
15+
<a href="{{ url_for('basicadmin.index') }}">
16+
Basic Admin Panel
17+
</a>
18+
</li>
19+
<li>
20+
<a href="{{ url_for('superadmin.index') }}">
21+
Super Admin Panel
22+
</a>
23+
</li>
24+
<li>
25+
<a href="{{ url_for('proadmin.index') }}">
26+
Pro Admin Panel
27+
</a>
28+
</li>
29+
</ul>
30+
""")
31+
32+
33+
@bp.app_errorhandler(404)
34+
def not_found(error):
35+
return redirect(url_for("main.index"))

examples/fomanticui-test/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
3+
4+
class MyConfig:
5+
DEBUG = True
6+
SECRET_KEY = os.environ.get("SECRET_KEY", os.urandom(16))
7+
DATABASE_URL = os.environ.get("DATABASE_URL", "sqlite:///sqlite3-fomanticui.sqlite")
8+
SQLALCHEMY_DATABASE_URI = DATABASE_URL
9+
SQLALCHEMY_TRACK_MODIFICATIONS = False

examples/fomanticui-test/fake.py

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
from random import choice
2+
from random import randint
3+
4+
from faker import Faker
5+
from models import Address
6+
from models import Category
7+
from models import Comment
8+
from models import db
9+
from models import Department
10+
from models import Employee
11+
from models import Order
12+
from models import OrderItem
13+
from models import OrderStatus
14+
from models import Payment
15+
from models import PaymentMethod
16+
from models import Post
17+
from models import Product
18+
from models import Profile
19+
from models import Project
20+
from models import Role
21+
from models import Tag
22+
from models import User
23+
24+
fake = Faker()
25+
26+
27+
def generate_fake_data(app):
28+
"""Populate the database with a rich set of example data covering all models."""
29+
with app.app_context():
30+
db.create_all()
31+
db.create_all()
32+
# ----- Roles -----
33+
if Role.query.count() == 0:
34+
for name in ("admin", "editor", "user"):
35+
db.session.add(Role(name=name))
36+
db.session.commit()
37+
38+
roles = Role.query.all()
39+
40+
# ----- Categories -----
41+
while Category.query.count() < 5:
42+
db.session.add(
43+
Category(name=fake.unique.word(), description=fake.sentence())
44+
)
45+
db.session.commit()
46+
categories = Category.query.all()
47+
48+
# ----- Tags -----
49+
while Tag.query.count() < 10:
50+
db.session.add(Tag(name=fake.unique.word()))
51+
db.session.commit()
52+
tags = Tag.query.all()
53+
54+
# ----- Products -----
55+
while Product.query.count() < 20:
56+
db.session.add(
57+
Product(
58+
name=fake.unique.word().title(),
59+
description=fake.text(max_nb_chars=200),
60+
price=round(
61+
fake.pyfloat(
62+
left_digits=3,
63+
right_digits=2,
64+
positive=True,
65+
min_value=5,
66+
max_value=200,
67+
),
68+
2,
69+
),
70+
stock=randint(0, 100),
71+
data={
72+
"color": fake.color_name(),
73+
"size": choice(["S", "M", "L", "XL"]),
74+
},
75+
)
76+
)
77+
db.session.commit()
78+
products = Product.query.all()
79+
80+
# ----- Users -----
81+
while User.query.count() < 50:
82+
user = User(
83+
email=fake.unique.email(),
84+
name=fake.name(),
85+
age=randint(18, 65),
86+
active=choice([True, False]),
87+
preferences={"newsletter": choice([True, False])},
88+
balance=round(
89+
fake.pyfloat(left_digits=3, right_digits=2, positive=True), 2
90+
),
91+
last_login=fake.date_time_this_year(),
92+
)
93+
# assign 1-3 random roles
94+
user.roles.extend(
95+
fake.random_elements(elements=roles, length=randint(1, 3), unique=True)
96+
)
97+
db.session.add(user)
98+
db.session.commit()
99+
100+
users = User.query.all()
101+
102+
# ----- Addresses & Profiles -----
103+
for user in users:
104+
if not user.addresses:
105+
addr = Address(
106+
street=fake.street_address(),
107+
city=fake.city(),
108+
state=fake.state(),
109+
postal_code=fake.postcode(),
110+
country=fake.country(),
111+
is_primary=True,
112+
user_id=user.id,
113+
)
114+
db.session.add(addr)
115+
if not user.profile:
116+
profile = Profile(bio=fake.sentence(), avatar=None, user_id=user.id)
117+
db.session.add(profile)
118+
db.session.commit()
119+
120+
# ----- Posts -----
121+
while Post.query.count() < 100:
122+
author = choice(users)
123+
post = Post(
124+
author_id=author.id,
125+
category=choice(categories),
126+
title=fake.sentence(),
127+
body=fake.text(max_nb_chars=800),
128+
)
129+
post.tags = fake.random_elements(
130+
elements=tags, length=randint(1, 4), unique=True
131+
)
132+
db.session.add(post)
133+
db.session.commit()
134+
135+
posts = Post.query.all()
136+
137+
# ----- Comments -----
138+
while Comment.query.count() < 300:
139+
db.session.add(
140+
Comment(
141+
post_id=choice(posts).id,
142+
user_id=choice(users).id,
143+
body=fake.sentence(),
144+
)
145+
)
146+
db.session.commit()
147+
148+
# ----- Orders, Items, Payments -----
149+
while Order.query.count() < 200:
150+
user = choice(users)
151+
order = Order(
152+
user_id=user.id,
153+
status=choice(list(OrderStatus)),
154+
total_price=0, # will update after adding items
155+
)
156+
db.session.add(order)
157+
db.session.commit()
158+
159+
orders = Order.query.all()
160+
161+
for order in orders:
162+
if not order.items:
163+
chosen_products = fake.random_elements(
164+
elements=products, length=randint(1, 5), unique=True
165+
)
166+
total = 0
167+
for prod in chosen_products:
168+
qty = randint(1, 3)
169+
total += prod.price * qty
170+
db.session.add(
171+
OrderItem(
172+
order_id=order.id,
173+
product_id=prod.id,
174+
quantity=qty,
175+
price=prod.price,
176+
)
177+
)
178+
order.total_price = round(total, 2)
179+
db.session.add(
180+
Payment(
181+
order_id=order.id,
182+
amount=order.total_price,
183+
paid_at=fake.date_time_this_year(),
184+
method=choice(list(PaymentMethod)),
185+
)
186+
)
187+
db.session.commit()
188+
189+
# ----- Departments -----
190+
while Department.query.count() < 5:
191+
db.session.add(
192+
Department(
193+
name=fake.unique.company(),
194+
budget=round(
195+
fake.pyfloat(
196+
left_digits=7,
197+
right_digits=2,
198+
positive=True,
199+
min_value=10000,
200+
max_value=1000000,
201+
),
202+
2,
203+
),
204+
)
205+
)
206+
db.session.commit()
207+
departments = Department.query.all()
208+
209+
# ----- Projects -----
210+
while Project.query.count() < 10:
211+
db.session.add(
212+
Project(
213+
name=fake.unique.catch_phrase(),
214+
deadline=fake.date_this_year(after_today=True),
215+
description=fake.text(max_nb_chars=200),
216+
budget=round(
217+
fake.pyfloat(
218+
left_digits=6,
219+
right_digits=2,
220+
positive=True,
221+
min_value=5000,
222+
max_value=500000,
223+
),
224+
2,
225+
),
226+
)
227+
)
228+
db.session.commit()
229+
projects = Project.query.all()
230+
231+
# ----- Employees -----
232+
while Employee.query.count() < 100:
233+
dept = choice(departments)
234+
emp = Employee(
235+
department_id=dept.id,
236+
first_name=fake.first_name(),
237+
last_name=fake.last_name(),
238+
salary=round(
239+
fake.pyfloat(
240+
left_digits=6,
241+
right_digits=2,
242+
positive=True,
243+
min_value=30000,
244+
max_value=150000,
245+
),
246+
2,
247+
),
248+
hire_date=fake.date_between(start_date="-10y", end_date="today"),
249+
shift_start=fake.time_object(),
250+
is_full_time=choice([True, False]),
251+
rating=round(
252+
fake.pyfloat(
253+
left_digits=1,
254+
right_digits=2,
255+
positive=True,
256+
min_value=1,
257+
max_value=5,
258+
),
259+
2,
260+
),
261+
)
262+
db.session.add(emp)
263+
db.session.commit()
264+
265+
employees = Employee.query.all()
266+
267+
for emp in employees:
268+
if not emp.projects:
269+
emp.projects = fake.random_elements(
270+
elements=projects, length=randint(1, 4), unique=True
271+
)
272+
db.session.commit()

0 commit comments

Comments
 (0)