|
3 | 3 | from fastapi import FastAPI |
4 | 4 | from fastapi.middleware.cors import CORSMiddleware |
5 | 5 | from models import BaseEvent, Event, Tournament, User, db |
6 | | -from pony.orm import db_session |
| 6 | +from pony.orm import commit, db_session |
7 | 7 |
|
8 | | -from fastadmin import PonyORMInlineModelAdmin, PonyORMModelAdmin, action, display |
| 8 | +from fastadmin import PonyORMInlineModelAdmin, PonyORMModelAdmin, WidgetType, action, display |
9 | 9 | from fastadmin import fastapi_app as admin_app |
10 | 10 | from fastadmin import register |
11 | 11 |
|
12 | 12 |
|
13 | 13 | @register(User) |
14 | 14 | class UserModelAdmin(PonyORMModelAdmin): |
15 | | - exclude = ("password",) |
16 | 15 | list_display = ("id", "username", "is_superuser") |
17 | 16 | list_display_links = ("id", "username") |
18 | 17 | list_filter = ("id", "username", "is_superuser") |
19 | 18 | search_fields = ("username",) |
20 | | - |
21 | | - async def authenticate(self, username, password): |
22 | | - with db_session: |
23 | | - obj = next((f for f in User.select(username=username, password=password, is_superuser=True)), None) # fmt: skip |
24 | | - if not obj: |
25 | | - return None |
26 | | - return obj.id |
27 | | - |
28 | | - async def change_password(self, user_id, password): |
29 | | - user = await self.model_cls.filter(id=user_id).first() |
30 | | - if not user: |
| 19 | + formfield_overrides = { # noqa: RUF012 |
| 20 | + "username": (WidgetType.SlugInput, {"required": True}), |
| 21 | + "password": (WidgetType.PasswordInput, {"passwordModalForm": True}), |
| 22 | + } |
| 23 | + |
| 24 | + @db_session |
| 25 | + def authenticate(self, username, password): |
| 26 | + obj = next((f for f in User.select(username=username, password=password, is_superuser=True)), None) # fmt: skip |
| 27 | + if not obj: |
| 28 | + return None |
| 29 | + return obj.id |
| 30 | + |
| 31 | + @db_session |
| 32 | + def change_password(self, user_id, password): |
| 33 | + obj = next((f for f in self.model_cls.select(id=user_id)), None) |
| 34 | + if not obj: |
31 | 35 | return |
32 | 36 | # direct saving password is only for tests - use hash |
33 | | - user.password = password |
34 | | - await user.save() |
| 37 | + obj.password = password |
| 38 | + commit() |
35 | 39 |
|
36 | 40 |
|
37 | 41 | class EventInlineModelAdmin(PonyORMInlineModelAdmin): |
@@ -61,20 +65,32 @@ class EventModelAdmin(PonyORMModelAdmin): |
61 | 65 | "started", |
62 | 66 | ) |
63 | 67 |
|
64 | | - @action(description="Make user active") |
65 | | - async def make_is_active(self, ids): |
66 | | - await self.model_cls.filter(id__in=ids).update(is_active=True) |
| 68 | + @action(description="Make event active") |
| 69 | + @db_session |
| 70 | + def make_is_active(self, ids): |
| 71 | + # update(o.set(is_active=True) for o in self.model_cls if o.id in ids) |
| 72 | + objs = self.model_cls.select(lambda o: o.id in ids) |
| 73 | + for obj in objs: |
| 74 | + obj.is_active = True |
| 75 | + commit() |
67 | 76 |
|
68 | 77 | @action |
69 | | - async def make_is_not_active(self, ids): |
70 | | - await self.model_cls.filter(id__in=ids).update(is_active=False) |
| 78 | + @db_session |
| 79 | + def make_is_not_active(self, ids): |
| 80 | + # update(o.set(is_active=False) for o in self.model_cls if o.id in ids) |
| 81 | + objs = self.model_cls.select(lambda o: o.id in ids) |
| 82 | + for obj in objs: |
| 83 | + obj.is_active = False |
| 84 | + commit() |
71 | 85 |
|
72 | 86 | @display |
73 | | - async def started(self, obj): |
| 87 | + @db_session |
| 88 | + def started(self, obj): |
74 | 89 | return bool(obj.start_time) |
75 | 90 |
|
76 | 91 | @display() |
77 | | - async def name_with_price(self, obj): |
| 92 | + @db_session |
| 93 | + def name_with_price(self, obj): |
78 | 94 | return f"{obj.name} - {obj.price}" |
79 | 95 |
|
80 | 96 |
|
|
0 commit comments