class Domain(db.Model): """ Define a mapping to the database for a domain. """ id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255), nullable=False) role_id = db.Column(db.Integer, db.ForeignKey('role.id'), nullable=False) policies = db.relationship('Policy', backref='domain', cascade='all, delete-orphan') def __init__(self, title, role_id): """ Initialize a domain. """ self.title = title self.role_id = role_id def __repr__(self): """ Return a readable representation of a domain. """ return '<Domain %r>' % self.id @property def serialize(self): """ Return a JSON-encoded representation of a domain. """ return {'id': self.id, 'title': self.title, 'role_id': self.role_id}
class Invitation(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(36), unique=True, nullable=False) email = db.Column(db.String(100), nullable=False) status = db.Column(db.Enum(InvitationStatus), nullable=False) organization_id = db.Column(db.Integer, db.ForeignKey('organization.id'), nullable=False) def __init__(self, email, organization_id): self.code = str(uuid.uuid4()) self.email = email self.status = InvitationStatus.PENDING self.organization_id = organization_id def __repr__(self): return '<Invitation %r>' % self.id @property def serialize(self): return { 'id': self.id, 'code': self.code, 'email': self.email, 'status': self.status.value, 'organization_id': self.organization_id }
class Accountability(db.Model): """ Define a mapping to the database for an accountability. """ id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255), nullable=False) role_id = db.Column(db.Integer, db.ForeignKey('role.id'), nullable=False) def __init__(self, title, role_id): """ Initialize an accountability. """ self.title = title self.role_id = role_id def __repr__(self): """ Return a readable representation of an accountability. """ return '<Accountability %r>' % self.id @property def serialize(self): """ Return a JSON-encoded representation of an accountability. """ return {'id': self.id, 'title': self.title, 'role_id': self.role_id}
class Circle(db.Model): """ Define a mapping to the database for a circle. """ id = db.Column(db.Integer, db.ForeignKey('role.id'), primary_key=True) strategy = db.Column(db.String(255), nullable=True) roles = db.relationship('Role', backref='parent_circle', foreign_keys='Role.parent_circle_id', cascade='all, delete-orphan') # implement inheritance, circle extends role super = db.relationship('Role', back_populates='derived_circle', foreign_keys='Circle.id', single_parent=True) def __init__(self, id, strategy): """ Initialize a circle. """ self.id = id self.strategy = strategy def __repr__(self): """ Return a readable representation of a circle. """ return '<Circle %r>' % self.id @property def serialize(self): """ Return a JSON-encoded representation of a circle. """ return { 'id': self.id, 'strategy': self.strategy }
class Invitation(db.Model): """ Define a mapping to the database for an invitation. """ id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(36), unique=True, nullable=False) email = db.Column(db.String(100), nullable=False) status = db.Column(db.Enum(InvitationStatus), nullable=False) organization_id = db.Column(db.Integer, db.ForeignKey('organization.id'), nullable=False) def __init__(self, email, organization_id): """ Initialize an invitation. """ self.code = str(uuid.uuid4()) self.email = email self.status = InvitationStatus.pending self.organization_id = organization_id def __repr__(self): """ Return a readable representation of an invitation. """ return '<Invitation %r>' % self.id @property def serialize(self): """ Return a JSON-encoded representation of an invitation. """ return { 'id': self.id, 'code': self.code, 'email': self.email, 'status': self.status.value, 'organization_id': self.organization_id }
class Policy(db.Model): """ Define a mapping to the database for a policy. """ id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255), nullable=False) description = db.Column(db.String(255), nullable=True) domain_id = db.Column(db.Integer, db.ForeignKey('domain.id'), nullable=False) def __init__(self, title, description, domain_id): """ Initialize a policy. """ self.title = title self.description = description self.domain_id = domain_id def __repr__(self): """ Return a readable representation of a policy. """ return '<Policy %r>' % self.id @property def serialize(self): """ Return a JSON-encoded representation of a policy. """ return { 'id': self.id, 'title': self.title, 'description': self.description, 'domain': self.domain_id }
class Partner(db.Model): """ Define a mapping to the database for a partner. """ id = db.Column(db.Integer, primary_key=True) type = db.Column(db.Enum(PartnerType), nullable=False) firstname = db.Column(db.String(45), nullable=False) lastname = db.Column(db.String(45), nullable=False) email = db.Column(db.String(100), nullable=False) is_active = db.Column(db.Boolean(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) organization_id = db.Column(db.Integer, db.ForeignKey('organization.id'), nullable=False) invitation_id = db.Column(db.Integer, db.ForeignKey('invitation.id'), nullable=True) memberships = db.relationship('Role', secondary=role_member, back_populates='members') __table_args__ = (db.UniqueConstraint( 'user_id', 'organization_id', name='UNIQUE_organization_id_user_id'), ) def __init__(self, type, firstname, lastname, email, user, organization, invitation_id=None): """ Initialize a partner. """ self.type = type self.firstname = firstname self.lastname = lastname self.email = email self.is_active = True self.user = user self.organization = organization self.invitation_id = invitation_id def __repr__(self): """ Return a readable representation of a partner. """ return '<Partner %r>' % self.id @property def serialize(self): """ Return a JSON-encoded representation of a partner. """ return { 'id': self.id, 'type': self.type.value, 'firstname': self.firstname, 'lastname': self.lastname, 'email': self.email, 'is_active': self.is_active, 'user_id': self.user.id, 'organization_id': self.organization.id, 'invitation_id': self.invitation_id }
class Role(db.Model): """ Define a mapping to the database for a role. """ id = db.Column(db.Integer, primary_key=True) type = db.Column(db.Enum(RoleType), nullable=False) name = db.Column(db.String(100), nullable=False) purpose = db.Column(db.String(255), nullable=False) parent_circle_id = db.Column(db.Integer, db.ForeignKey('circle.id'), nullable=True) organization_id = db.Column(db.Integer, db.ForeignKey('organization.id'), nullable=False) members = db.relationship('Partner', secondary=role_member, back_populates='memberships') # cascade='all, delete-orphan') domains = db.relationship('Domain', backref='role', cascade='all, delete-orphan') accountabilities = db.relationship('Accountability', backref='role', cascade='all, delete-orphan') # implement inheritance, circle extends role derived_circle = db.relationship('Circle', back_populates='super', foreign_keys='Circle.id', uselist=False, cascade='all, delete-orphan') def __init__(self, type, name, purpose, parent_circle_id, organization_id): """ Initialize a role. """ self.type = type self.name = name self.purpose = purpose self.parent_circle_id = parent_circle_id self.organization_id = organization_id def __repr__(self): """ Return a readable representation of a role. """ return '<Role %r>' % self.id @property def serialize(self): """ Return a JSON-encoded representation of a role. """ return { 'id': self.id, 'type': self.type.value, 'name': self.name, 'purpose': self.purpose, 'parent_circle_id': self.parent_circle_id, 'organization_id': self.organization_id }
""" Define classes for a circle member. """ from swarm_intelligence_app.models import db role_member = db.Table( 'role_member', db.Column('partner_id', db.Integer, db.ForeignKey('partner.id')), db.Column('role_id', db.Integer, db.ForeignKey('role.id')))