class Microregion(datasets.Entity): """Entity class for microregions.""" _name = 'microregion' __table__ = schema.Table( 'microregions', datasets.Entity.metadata, schema.Column('id', types.Integer, nullable=False, primary_key=True), schema.Column('mesoregion_id', types.SmallInteger, schema.ForeignKey('mesoregions.id', use_alter=True), nullable=False, index=True), schema.Column('state_id', types.SmallInteger, schema.ForeignKey('states.id', use_alter=True), nullable=False, index=True), schema.Column('name', types.String(64), nullable=False, index=True)) # Relationships state = orm.relationship('State', back_populates='microregions') mesoregion = orm.relationship('Mesoregion', back_populates='microregions') municipalities =\ orm.relationship('Municipality', back_populates='microregion') districts = orm.relationship('District', back_populates='microregion') subdistricts =\ orm.relationship('Subdistrict', back_populates='microregion')
class Vote(ModelBase): """ The Vote object represents the *current* ballot for a given voter. """ __versioned__ = {} __tablename__ = 'vote' # TODO: Find out what other constraints we'd need? One vote per election? voter_id = schema.Column( schema.ForeignKey('pollbook_voters.id'), primary_key=True, ) ballot_id = schema.Column( schema.ForeignKey('vote_log.ballot_id'), index=True, nullable=False, ) voter = relationship( "Voter", back_populates="votes", ) record = relationship( "VoteRecord", back_populates="vote", )
class PersonIdentifierPrincipal(Principal): """ Security principal based on an external ID representing a person/user entity. """ __tablename__ = 'person_identifier_principal' __versioned__ = {} id = schema.Column(UuidType, schema.ForeignKey('principal.id'), default=uuid.uuid4, primary_key=True) id_type = schema.Column(types.UnicodeText, nullable=False) id_value = schema.Column(types.UnicodeText, nullable=False) @validates('id_type') def validate_id_type(self, key, id_type): return PersonIdType(id_type).value __table_args__ = (schema.UniqueConstraint('id_type', 'id_value'), ) __mapper_args__ = { 'polymorphic_identity': 'person-identifier-principal', 'inherit_condition': id == Principal.id, }
class GroupPrincipal(Principal): """ Security principal based on membership in a group. """ __tablename__ = 'group_principal' __versioned__ = {} id = schema.Column(UuidType, schema.ForeignKey('principal.id'), default=uuid.uuid4, primary_key=True) group_id = schema.Column(UuidType, schema.ForeignKey('group.id'), nullable=False) group = relationship('Group', back_populates='principals') __mapper_args__ = { 'polymorphic_identity': 'group-principal', 'inherit_condition': id == Principal.id, }
class PersonPrincipal(Principal): """ Security principal based on a person/user entity. """ __tablename__ = 'person_principal' __versioned__ = {} id = schema.Column(UuidType, schema.ForeignKey('principal.id'), default=uuid.uuid4, primary_key=True) person_id = schema.Column(UuidType, schema.ForeignKey('person.id'), nullable=False, unique=True) person = relationship('Person', back_populates='principal') __mapper_args__ = { 'polymorphic_identity': 'person-principal', 'inherit_condition': id == Principal.id, }
class ElectionGroupRole(Role): """ Roles granted on election. """ __tablename__ = 'election_group_role' __versioned__ = {} grant_id = schema.Column(UuidType, schema.ForeignKey('role.grant_id'), default=uuid.uuid4, primary_key=True) group_id = schema.Column(UuidType, schema.ForeignKey('election_group.id')) group = relationship('ElectionGroup', backref='roles', lazy='joined') global_role = schema.Column(types.Boolean) __table_args__ = (schema.CheckConstraint(or_( and_(global_role == true(), group_id == null()), or_(global_role == null(), global_role == false())), name='no_eg_when_global'), ) # principal_id = schema.Column( # UuidType, # schema.ForeignKey('principal.id'), # nullable=False) # principal = relationship( # 'Principal', # back_populates='roles') # __table_args__ = ( # schema.UniqueConstraint('role', 'election_id', 'principal_id'), # ) __mapper_args__ = { 'polymorphic_identity': 'election-group-role', }
class VoteRecord(ModelBase): """ The VoteRecord represents *all* votes left by a given voter. """ __versioned__ = {} __tablename__ = 'vote_log' # ballot_id is a reference to a ballot. # the ballot *may* not reside in this database -- how would be set up this? ballot_id = schema.Column( evalg.database.types.UuidType, doc='reference to the ballot', primary_key=True, ) voter_id = schema.Column( schema.ForeignKey('pollbook_voters.id'), doc='reference to the voter who cast the ballot', index=True, nullable=False, ) logged_at = schema.Column( evalg.database.types.UtcDateTime, default=utcnow, doc='timestamp of the change', index=True, nullable=False, ) ip_addr = schema.Column( evalg.database.types.IpAddressType, doc='client ip that stored this vote', nullable=True, ) user = schema.Column( sqltypes.UnicodeText, doc='authenticated user that stored this vote', nullable=True, ) vote = relationship( 'Vote', doc='link to the vote if this record is a current vote', back_populates="record", )
class Role(ModelBase): """ Roles granted to a principal. """ __tablename__ = 'role' __versioned__ = {} grant_id = schema.Column(UuidType, default=uuid.uuid4, primary_key=True) name = schema.Column(types.String, nullable=False) target_type = schema.Column(types.String(50), nullable=False) principal_id = schema.Column(UuidType, schema.ForeignKey('principal.id'), nullable=False) principal = relationship('Principal', back_populates='roles') __mapper_args__ = { 'polymorphic_identity': 'role', 'polymorphic_on': target_type }