piccolo-orm / piccolo_admin

A powerful web admin for your database.
https://piccolo-orm.com/ecosystem/
MIT License
322 stars 39 forks source link

Make admin more flexible for subclassed `BaseUser` #262

Closed dantownsend closed 1 year ago

dantownsend commented 1 year ago

In some situations the user may need to modify BaseUser and SessionsBase to use UUIDs instead.

We can do this with a very minor change to Piccolo Admin.

class CustomSessions(SessionsBase):
    id = UUID(primary_key=True)
    user_id = UUID(null=False)

class CustomUser(BaseUser):
    id = UUID(primary_key=True)

create_admin(
    tables=[TableA, TableB],
    auth_table=CustomUser,
    session_table=CustomSessions
)

This pretty much works out of the box, except for one minor thing here:

https://github.com/piccolo-orm/piccolo_admin/blob/f2fbcea0ef5dcff029f67a023b1d1965e7a07fec/piccolo_admin/endpoints.py#L776-L780

We just need to cast user_id to a string, in case it's a UUID.

sinisaos commented 1 year ago

@dantownsend You're right. The only change we need to make is to cast the user_id to user_id=str(request.user.user_id) and everything will work for both Serial or UUID primary key.

dantownsend commented 1 year ago

@sinisaos Yeah, I think it's worth adding as it's such a small change.