Skip to content

Commit 09569c5

Browse files
ormiretjellybob
authored andcommitted
Easier way to add volunteer role admins
1 parent b140235 commit 09569c5

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

apps/volunteer/admin/volunteer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class VolunteerUserModelView(VolunteerModelView):
3939
"volunteer_phone",
4040
"interested_roles",
4141
"trained_roles",
42+
"admined_roles",
4243
"over_18",
4344
"allow_comms_during_event",
4445
"banned",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""VolunteerRoleAdminSetting
2+
3+
Revision ID: 4dcd2eba5f2c
4+
Revises: e9529c62ca57
5+
Create Date: 2024-05-14 01:00:14.114990
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = '4dcd2eba5f2c'
11+
down_revision = 'e9529c62ca57'
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
16+
17+
def upgrade():
18+
# ### commands auto generated by Alembic - please adjust! ###
19+
op.create_table('volunteer_role_admin_version',
20+
sa.Column('user_id', sa.Integer(), autoincrement=False, nullable=False),
21+
sa.Column('role_id', sa.Integer(), autoincrement=False, nullable=False),
22+
sa.Column('transaction_id', sa.BigInteger(), autoincrement=False, nullable=False),
23+
sa.Column('operation_type', sa.SmallInteger(), nullable=False),
24+
sa.PrimaryKeyConstraint('user_id', 'role_id', 'transaction_id', name=op.f('pk_volunteer_role_admin_version'))
25+
)
26+
op.create_index(op.f('ix_volunteer_role_admin_version_operation_type'), 'volunteer_role_admin_version', ['operation_type'], unique=False)
27+
op.create_index(op.f('ix_volunteer_role_admin_version_transaction_id'), 'volunteer_role_admin_version', ['transaction_id'], unique=False)
28+
# ### end Alembic commands ###
29+
30+
31+
def downgrade():
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.drop_index(op.f('ix_volunteer_role_admin_version_transaction_id'), table_name='volunteer_role_admin_version')
34+
op.drop_index(op.f('ix_volunteer_role_admin_version_operation_type'), table_name='volunteer_role_admin_version')
35+
op.drop_table('volunteer_role_admin_version')
36+
# ### end Alembic commands ###

models/volunteer/role.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class RolePermission(BaseModel):
6161

6262
class RoleAdmin(BaseModel):
6363
__tablename__ = "volunteer_role_admin"
64+
__versioned__: dict = {}
6465
user_id = db.Column(
6566
db.Integer, db.ForeignKey("user.id"), nullable=False, primary_key=True
6667
)

models/volunteer/volunteer.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .. import BaseModel
77

88
from . import ShiftEntry
9-
9+
from . import RoleAdmin
1010

1111
# This effectively records the roles that a volunteer is interested in
1212
VolunteerRoleInterest = db.Table(
@@ -25,7 +25,6 @@
2525
db.Column("role_id", db.Integer, db.ForeignKey("volunteer_role.id"), primary_key=True),
2626
)
2727

28-
2928
class Volunteer(BaseModel, UserMixin):
3029
__table_name__ = "volunteer"
3130
__versioned__: dict = {}
@@ -53,7 +52,15 @@ class Volunteer(BaseModel, UserMixin):
5352
secondary=VolunteerRoleTraining,
5453
lazy="dynamic",
5554
)
56-
55+
admined_roles = db.relationship(
56+
"Role",
57+
backref="role_admins",
58+
secondary=RoleAdmin.__table__,
59+
primaryjoin="RoleAdmin.user_id==Volunteer.user_id",
60+
secondaryjoin="RoleAdmin.role_id==Role.id",
61+
lazy="dynamic"
62+
)
63+
5764
def __repr__(self):
5865
return f"<Volunteer {self.__str__()}>"
5966

0 commit comments

Comments
 (0)