forked from piccolo-orm/piccolo_admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
391 lines (323 loc) · 10.5 KB
/
example.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""
An example of how to configure and run the admin.
Can be run from the command line using `python -m piccolo_admin.example`,
or `admin_demo`.
"""
import asyncio
import datetime
import decimal
import enum
import os
import random
import smtplib
import typing as t
import targ
from hypercorn.asyncio import serve
from hypercorn.config import Config
from piccolo.apps.user.tables import BaseUser
from piccolo.columns import (
JSON,
Array,
BigInt,
Boolean,
ForeignKey,
Integer,
Interval,
Numeric,
Real,
SmallInt,
Text,
Timestamp,
Varchar,
)
from piccolo.columns.readable import Readable
from piccolo.engine.postgres import PostgresEngine
from piccolo.engine.sqlite import SQLiteEngine
from piccolo.table import Table
from piccolo_api.session_auth.tables import SessionsBase
from pydantic import BaseModel, validator
from piccolo_admin.endpoints import FormConfig, TableConfig, create_admin
from piccolo_admin.example_data import DIRECTORS, MOVIE_WORDS, MOVIES, STUDIOS
class Sessions(SessionsBase):
pass
class User(BaseUser, tablename="piccolo_user"):
pass
class Director(Table, help_text="The main director for a movie."):
class Gender(enum.Enum):
male = "m"
female = "f"
non_binary = "n"
name = Varchar(length=300, null=False)
years_nominated = Array(
base_column=Integer(),
help_text=(
"Which years this director was nominated for a best director "
"Oscar."
),
)
gender = Varchar(length=1, choices=Gender)
@classmethod
def get_readable(cls):
return Readable(template="%s", columns=[cls.name])
class Movie(Table):
class Genre(int, enum.Enum):
fantasy = 1
sci_fi = 2
documentary = 3
horror = 4
action = 5
comedy = 6
romance = 7
musical = 8
name = Varchar(length=300)
rating = Real(help_text="The rating on IMDB.")
duration = Interval()
director = ForeignKey(references=Director)
oscar_nominations = Integer()
won_oscar = Boolean()
description = Text()
release_date = Timestamp(null=True)
box_office = Numeric(digits=(5, 1), help_text="In millions of US dollars.")
tags = Array(base_column=Varchar())
barcode = BigInt(default=0)
genre = SmallInt(choices=Genre, null=True)
class Studio(Table, help_text="A movie studio."):
name = Varchar()
facilities = JSON()
class BusinessEmailModel(BaseModel):
email: str
title: str
content: str
@validator("email")
def validate_email(cls, v):
if "@" not in v:
raise ValueError("not valid email")
return v
class BookingModel(BaseModel):
email: str
name: str
notes: str
@validator("email")
def validate_email(cls, v):
if "@" not in v:
raise ValueError("not valid email")
return v
def business_email_endpoint(request, data):
sender = "info@example.com"
receivers = [data.email]
message = f"""From: Piccolo Admin <info@example.com>
To: Colleague <{data.email}>
Subject: {data.title}
{data.content}
"""
try:
smtpObj = smtplib.SMTP("localhost:1025")
smtpObj.sendmail(sender, receivers, message)
print("Successfully sent email")
except (smtplib.SMTPException, ConnectionRefusedError):
print("Error: unable to send email")
return "Email sent"
async def booking_endpoint(request, data):
"""
Testing that async functions works.
"""
sender = "info@example.com"
receivers = [data.email]
message = f"""From: Bookings <info@example.com>
To: To Friend <{data.email}>
Subject: {data.name} booking
{data.notes}
"""
try:
smtpObj = smtplib.SMTP("localhost:1025")
smtpObj.sendmail(sender, receivers, message)
print("Successfully sent email")
except (smtplib.SMTPException, ConnectionRefusedError):
print("Error: unable to send email")
return "Booking complete"
TABLE_CLASSES: t.Tuple[t.Type[Table], ...] = (
Director,
Movie,
Studio,
User,
Sessions,
)
movie_config = TableConfig(
table_class=Movie,
visible_columns=[
Movie._meta.primary_key,
Movie.name,
Movie.rating,
Movie.director,
],
filter_columns=[
Movie.name,
Movie.rating,
Movie.director,
],
)
director_config = TableConfig(
table_class=Director,
visible_columns=[
Director._meta.primary_key,
Director.name,
Director.gender,
],
)
APP = create_admin(
[movie_config, director_config, Studio],
forms=[
FormConfig(
name="Business email form",
pydantic_model=BusinessEmailModel,
endpoint=business_email_endpoint,
description="Send an email to a business associate.",
),
FormConfig(
name="Booking form",
pydantic_model=BookingModel,
endpoint=booking_endpoint,
description="Make a booking for a customer.",
),
],
auth_table=User,
session_table=Sessions,
)
def set_engine(engine: str = "sqlite"):
if engine == "postgres":
db = PostgresEngine(config={"database": "piccolo_admin"})
else:
sqlite_path = os.path.join(os.path.dirname(__file__), "example.sqlite")
db = SQLiteEngine(path=sqlite_path) # type: ignore
for table_class in TABLE_CLASSES:
table_class._meta._db = db
def create_schema(persist: bool = False):
if not persist:
for table_class in reversed(TABLE_CLASSES):
table_class.alter().drop_table(if_exists=True).run_sync()
for table_class in TABLE_CLASSES:
table_class.create_table(if_not_exists=True).run_sync()
def populate_data(inflate: int = 0, engine: str = "sqlite"):
"""
Populate the database with some example data.
:param inflate:
If set, this number of extra rows are inserted containing dummy data.
This is useful for testing.
"""
# Add some rows
Director.insert(*[Director(**d) for d in DIRECTORS]).run_sync() # type: ignore # noqa: E501
Movie.insert(*[Movie(**m) for m in MOVIES]).run_sync() # type: ignore
Studio.insert(*[Studio(**s) for s in STUDIOS]).run_sync() # type: ignore
if engine == "postgres":
# We need to update the sequence, as we explicitly set the IDs for the
# directors we just inserted
Director.raw(
"SELECT setval('director_id_seq', max(id)) FROM director"
).run_sync()
# Create a user for testing login
user = User(
username="piccolo",
password="piccolo123",
admin=True,
email="admin@test.com",
active=True,
)
user.save().run_sync()
if inflate:
try:
import faker
except ImportError:
print(
"Install faker to use this feature: "
"`pip install piccolo_admin[faker]`"
)
else:
fake = faker.Faker()
remaining = inflate
chunk_size = 100
while remaining > 0:
if remaining < chunk_size:
chunk_size = remaining
remaining = 0
else:
remaining = remaining - chunk_size
directors = []
genders = ["m", "f", "n"]
for _ in range(chunk_size):
gender = random.choice(genders)
if gender == "m":
name = fake.name_male()
elif gender == "f":
name = fake.name_female()
else:
name = fake.name_nonbinary()
directors.append(Director(name=name, gender=gender))
Director.insert(*directors).run_sync()
director_ids = (
Director.select(Director._meta.primary_key)
.order_by(Director._meta.primary_key, ascending=False)
.limit(chunk_size)
.output(as_list=True)
.run_sync()
)
movies = []
genres = [i.value for i in Movie.Genre]
for _ in range(chunk_size):
oscar_nominations = random.sample(
[0, 0, 0, 0, 0, 1, 1, 3, 5], 1
)[0]
won_oscar = oscar_nominations > 0
rating = (
random.randint(80, 100)
if won_oscar
else random.randint(1, 100)
) / 10
movie = Movie(
name="{} {}".format(
fake.word().title(),
fake.word(ext_word_list=MOVIE_WORDS),
),
rating=rating,
duration=datetime.timedelta(
minutes=random.randint(60, 210)
),
director=random.sample(director_ids, 1)[0],
oscar_nominations=oscar_nominations,
won_oscar=won_oscar,
description=fake.sentence(30),
release_date=fake.date_time(),
box_office=decimal.Decimal(
str(random.randint(10, 1500) / 10)
),
barcode=random.randint(1_000_000_000, 9_999_999_999),
genre=random.choice(genres),
)
movies.append(movie)
Movie.insert(*movies).run_sync()
def run(persist: bool = False, engine: str = "sqlite", inflate: int = 0):
"""
Start the Piccolo admin.
:param persist:
If True, we don't rebuild all of the data each time.
:param engine:
Options are sqlite and postgres. By default sqlite is used.
:param inflate:
If set, this number of extra rows are inserted containing dummy data.
This is useful when you need to test with lots of data. Example
`--inflate=10000`.
"""
set_engine(engine)
create_schema(persist=persist)
if not persist:
populate_data(inflate=inflate, engine=engine)
# Server
class CustomConfig(Config):
use_reloader = True
accesslog = "-"
asyncio.run(serve(APP, CustomConfig()))
def main():
cli = targ.CLI(description="Piccolo Admin")
cli.register(run)
cli.run(solo=True)
if __name__ == "__main__":
main()