From 09569c59ef73ea0653dddf2b43356db803f7be80 Mon Sep 17 00:00:00 2001 From: Robert McWilliam Date: Tue, 14 May 2024 02:03:27 +0100 Subject: [PATCH 1/2] Easier way to add volunteer role admins --- apps/volunteer/admin/volunteer.py | 1 + .../4dcd2eba5f2c_volunteerroleadminsetting.py | 36 +++++++++++++++++++ models/volunteer/role.py | 1 + models/volunteer/volunteer.py | 13 +++++-- 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 migrations/versions/4dcd2eba5f2c_volunteerroleadminsetting.py diff --git a/apps/volunteer/admin/volunteer.py b/apps/volunteer/admin/volunteer.py index 398a0eba7..ec2b18201 100644 --- a/apps/volunteer/admin/volunteer.py +++ b/apps/volunteer/admin/volunteer.py @@ -39,6 +39,7 @@ class VolunteerUserModelView(VolunteerModelView): "volunteer_phone", "interested_roles", "trained_roles", + "admined_roles", "over_18", "allow_comms_during_event", "banned", diff --git a/migrations/versions/4dcd2eba5f2c_volunteerroleadminsetting.py b/migrations/versions/4dcd2eba5f2c_volunteerroleadminsetting.py new file mode 100644 index 000000000..875e2b497 --- /dev/null +++ b/migrations/versions/4dcd2eba5f2c_volunteerroleadminsetting.py @@ -0,0 +1,36 @@ +"""VolunteerRoleAdminSetting + +Revision ID: 4dcd2eba5f2c +Revises: e9529c62ca57 +Create Date: 2024-05-14 01:00:14.114990 + +""" + +# revision identifiers, used by Alembic. +revision = '4dcd2eba5f2c' +down_revision = 'e9529c62ca57' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('volunteer_role_admin_version', + sa.Column('user_id', sa.Integer(), autoincrement=False, nullable=False), + sa.Column('role_id', sa.Integer(), autoincrement=False, nullable=False), + sa.Column('transaction_id', sa.BigInteger(), autoincrement=False, nullable=False), + sa.Column('operation_type', sa.SmallInteger(), nullable=False), + sa.PrimaryKeyConstraint('user_id', 'role_id', 'transaction_id', name=op.f('pk_volunteer_role_admin_version')) + ) + op.create_index(op.f('ix_volunteer_role_admin_version_operation_type'), 'volunteer_role_admin_version', ['operation_type'], unique=False) + op.create_index(op.f('ix_volunteer_role_admin_version_transaction_id'), 'volunteer_role_admin_version', ['transaction_id'], unique=False) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_volunteer_role_admin_version_transaction_id'), table_name='volunteer_role_admin_version') + op.drop_index(op.f('ix_volunteer_role_admin_version_operation_type'), table_name='volunteer_role_admin_version') + op.drop_table('volunteer_role_admin_version') + # ### end Alembic commands ### diff --git a/models/volunteer/role.py b/models/volunteer/role.py index c9b8c3372..2b902aaf0 100644 --- a/models/volunteer/role.py +++ b/models/volunteer/role.py @@ -61,6 +61,7 @@ class RolePermission(BaseModel): class RoleAdmin(BaseModel): __tablename__ = "volunteer_role_admin" + __versioned__: dict = {} user_id = db.Column( db.Integer, db.ForeignKey("user.id"), nullable=False, primary_key=True ) diff --git a/models/volunteer/volunteer.py b/models/volunteer/volunteer.py index 07d94fbd0..47666bee1 100644 --- a/models/volunteer/volunteer.py +++ b/models/volunteer/volunteer.py @@ -6,7 +6,7 @@ from .. import BaseModel from . import ShiftEntry - +from . import RoleAdmin # This effectively records the roles that a volunteer is interested in VolunteerRoleInterest = db.Table( @@ -25,7 +25,6 @@ db.Column("role_id", db.Integer, db.ForeignKey("volunteer_role.id"), primary_key=True), ) - class Volunteer(BaseModel, UserMixin): __table_name__ = "volunteer" __versioned__: dict = {} @@ -53,7 +52,15 @@ class Volunteer(BaseModel, UserMixin): secondary=VolunteerRoleTraining, lazy="dynamic", ) - + admined_roles = db.relationship( + "Role", + backref="role_admins", + secondary=RoleAdmin.__table__, + primaryjoin="RoleAdmin.user_id==Volunteer.user_id", + secondaryjoin="RoleAdmin.role_id==Role.id", + lazy="dynamic" + ) + def __repr__(self): return f"" From 698229e601792791803e48f9226f0fc8734e8f75 Mon Sep 17 00:00:00 2001 From: Jon Wood Date: Wed, 15 May 2024 21:18:55 +0000 Subject: [PATCH 2/2] Regenerate migration --- ... => 1267f408bc10_volunter_role_admin_versions.py} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename migrations/versions/{4dcd2eba5f2c_volunteerroleadminsetting.py => 1267f408bc10_volunter_role_admin_versions.py} (88%) diff --git a/migrations/versions/4dcd2eba5f2c_volunteerroleadminsetting.py b/migrations/versions/1267f408bc10_volunter_role_admin_versions.py similarity index 88% rename from migrations/versions/4dcd2eba5f2c_volunteerroleadminsetting.py rename to migrations/versions/1267f408bc10_volunter_role_admin_versions.py index 875e2b497..ce8087bb5 100644 --- a/migrations/versions/4dcd2eba5f2c_volunteerroleadminsetting.py +++ b/migrations/versions/1267f408bc10_volunter_role_admin_versions.py @@ -1,14 +1,14 @@ -"""VolunteerRoleAdminSetting +"""empty message -Revision ID: 4dcd2eba5f2c -Revises: e9529c62ca57 -Create Date: 2024-05-14 01:00:14.114990 +Revision ID: 1267f408bc10 +Revises: b9db98f438aa +Create Date: 2024-05-15 21:17:04.246271 """ # revision identifiers, used by Alembic. -revision = '4dcd2eba5f2c' -down_revision = 'e9529c62ca57' +revision = '1267f408bc10' +down_revision = 'b9db98f438aa' from alembic import op import sqlalchemy as sa