Skip to content

Commit

Permalink
Easier way to add volunteer role admins
Browse files Browse the repository at this point in the history
  • Loading branch information
ormiret authored and jellybob committed May 15, 2024
1 parent b140235 commit 09569c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/volunteer/admin/volunteer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class VolunteerUserModelView(VolunteerModelView):
"volunteer_phone",
"interested_roles",
"trained_roles",
"admined_roles",
"over_18",
"allow_comms_during_event",
"banned",
Expand Down
36 changes: 36 additions & 0 deletions migrations/versions/4dcd2eba5f2c_volunteerroleadminsetting.py
Original file line number Diff line number Diff line change
@@ -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 ###
1 change: 1 addition & 0 deletions models/volunteer/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
13 changes: 10 additions & 3 deletions models/volunteer/volunteer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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"<Volunteer {self.__str__()}>"

Expand Down

0 comments on commit 09569c5

Please sign in to comment.