Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin_features #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/services/admin/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from typing import Any

from sqladmin._queries import Query
from starlette.requests import Request

from app.core.admin.models import ModelViewCore
from app.core.auth.functions import hash_password
from app.core.models.record import Record
from app.services.user.models import RevokedToken, User

Expand All @@ -14,6 +20,41 @@ class UserAdmin(ModelViewCore, model=User):
# Detail Page
pass

# Form page
async def insert_model(self, request: Request, data: dict) -> Any:
"""Overwrite insert method for hashing the password"""
if "hashed_password" in data:
data["hashed_password"] = hash_password(data["hashed_password"])

model = await Query(self).insert(data)

Record(
model_type=self.model.__name__,
model_id=model.id,
source="ADMIN",
action="CREATE",
owner=request.session.get("username"),
).save()

return model

async def update_model(self, request: Request, pk: str, data: dict) -> Any:
"""Overwrite update method for hashing the password"""
if "hashed_password" in data:
data["hashed_password"] = hash_password(data["hashed_password"])

model = await Query(self).update(pk, data)

Record(
model_type=self.model.__name__,
model_id=model.id,
source="ADMIN",
action="UPDATE",
owner=request.session.get("username"),
).save()

return model

# General options
column_labels = {
User.username: "email"
Expand Down
Loading