diff --git a/app/models.py b/app/models.py index 5b37c2d0..280436fc 100644 --- a/app/models.py +++ b/app/models.py @@ -985,8 +985,13 @@ class Client(Base, ModelMixin): approved = sa.Column(sa.Boolean, nullable=False, default=False, server_default="0") description = sa.Column(sa.Text, nullable=True) + # a referral can be attached to a client + # so all users who sign up via the authorize screen are counted towards this referral + referral_id = sa.Column(sa.ForeignKey("referral.id"), nullable=True) + icon = orm.relationship(File) user = orm.relationship(User) + referral = orm.relationship("Referral") def nb_user(self): return ClientUser.filter_by(client_id=self.id).count() @@ -2251,7 +2256,7 @@ class Referral(Base, ModelMixin): code = sa.Column(sa.String(128), unique=True, nullable=False) - user = orm.relationship(User, foreign_keys=[user_id]) + user = orm.relationship(User, foreign_keys=[user_id], backref="referrals") @property def nb_user(self) -> int: diff --git a/migrations/versions/2021_102611_d67eab226ecd_.py b/migrations/versions/2021_102611_d67eab226ecd_.py new file mode 100644 index 00000000..f9ec852c --- /dev/null +++ b/migrations/versions/2021_102611_d67eab226ecd_.py @@ -0,0 +1,31 @@ +"""empty message + +Revision ID: d67eab226ecd +Revises: a06066e3fbeb +Create Date: 2021-10-26 11:35:13.448796 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'd67eab226ecd' +down_revision = 'a06066e3fbeb' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('client', sa.Column('referral_id', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'client', 'referral', ['referral_id'], ['id']) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'client', type_='foreignkey') + op.drop_column('client', 'referral_id') + # ### end Alembic commands ###