class Preference(db.Model): id = db.Column(db.Integer, primary_key=True) group_id = db.Column(db.Integer, db.ForeignKey('group.id'), nullable=False) first_preference = db.Column(db.String(500), index=True) second_preference = db.Column(db.String(500), index=True) third_preference = db.Column(db.String(500), index=True) match_id = db.Column(db.Integer, db.ForeignKey('user.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) last_updated_time = db.Column(db.DateTime, index=True, default=datetime.utcnow) #this is the many side, so uselist=False match = db.relationship("User", backref=db.backref("match_preferences", lazy='dynamic'), foreign_keys=[match_id], uselist=False) user = db.relationship("User", backref=db.backref("user_preferences", lazy='dynamic'), foreign_keys=[user_id], uselist=False) def __repr__(self): return '<Preference {}, {}>'.format(self.group_id, self.user_id)
class Tweet(db.Model): id = db.Column(db.Integer, primary_key = True) date_created = db.Column(db.DateTime) date_updated = db.Column(db.DateTime) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) username = db.Column(db.String(32), db.ForeignKey('user.username')) body = db.Column(db.String(140)) def __repr__(self): return '<Tweet %r>' % (self.body)
class Broadcaster2Follower(db.Model): id = db.Column(db.Integer, primary_key=True) date_created = db.Column(db.DateTime) date_updated = db.Column(db.DateTime) broadcaster_id = db.Column(db.Integer, db.ForeignKey('user.id')) follower_id = db.Column(db.Integer, db.ForeignKey('user.id')) active = db.Column(db.Boolean, default=True) def __repr__(self): return '<Broadcaster2Follower %r, %r>' % (self.broadcaster_id, self.follower_id)
class GroupMember(db.Model): id = db.Column(db.Integer, primary_key=True) group_id = db.Column(db.Integer, db.ForeignKey('group.id'), nullable=False) member_email = db.Column(db.String(120), index=True, nullable=False) def __repr__(self): return '<GroupMember {}, {}>'.format(self.group_id, self.member_email) @staticmethod def signup_status(member_email): user = User.query.filter_by(email=member_email).first() if user: return True else: return False @staticmethod def group_signup_status(group_id, member_email): user = User.query.filter_by(email=member_email).first() if user: preferences = user.user_preferences.filter_by( group_id=group_id).first() if preferences: return True return False
class ActivityLog(db.Model): TYPES = [ ('create', 'Create'), ('update', 'Update'), ('delete', 'Delete'), ] id = db.Column(db.Text(length=36), default=lambda: str(uuid.uuid4()), primary_key=True) action = db.Column(ChoiceType(TYPES)) user_id = db.Column(db.String(255), db.ForeignKey('user.id')) attributes = db.Column(JSONType) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow) __tablename__ = 'ActivityLog' def __repr__(self): return 'User id {}'.format(self.user_id) def to_dict(self): data = { "id": self.id, "user_id": self.user_id, "action": self.action.code, "attributes": self.attributes, 'created_at': self.created_at.strftime('%Y-%m-%dT%H:%M:%S.%fZ'), 'updated_at': self.updated_at.strftime('%Y-%m-%dT%H:%M:%S.%fZ') } return data
class Books(db.Model): id = db.Column(db.Integer, primary_key=True) subject_id = db.Column(db.Integer, db.ForeignKey('subject.id'), nullable=False) name = db.Column(db.String, unique=True, nullable=False) file = db.Column(db.Binary, nullable=False)
class Group(db.Model): id = db.Column(db.Integer, primary_key=True) groupname = db.Column(db.String(120), index=True, unique=True, nullable=False) rsvp_close_date = db.Column(db.Date, index=True, nullable=False) reveal_date = db.Column(db.Date, index=True, nullable=False) budget = db.Column(db.Float, index=True) host_join = db.Column(db.Boolean, index=True) last_updated_time = db.Column(db.DateTime, index=True, default=datetime.utcnow) host_id = db.Column(db.Integer, db.ForeignKey('user.id')) preferences = db.relationship('Preference', backref='group', lazy='dynamic') members = db.relationship('GroupMember', backref='group', lazy='dynamic') def __repr__(self): return '<Group {}>'.format(self.groupname) def get_all_member_emails(self): current_members = self.members.all() current_emails = [] for member in current_members: current_emails.append(member.member_email) if self.host_join: current_members.append(self.host.email) return current_emails def get_all_signup_member_emails(self): current_members = self.members.all() current_emails = [] for member in current_members: if GroupMember.group_signup_status(self.id, member.member_email): current_emails.append(member.member_email) if self.host_join and GroupMember.group_signup_status( self.id, self.host.email): current_members.append(self.host.email) return current_emails def if_match_set(self): current_preferences = self.preferences.all() match_set = True for current_preference in current_preferences: if not current_preference.match: match_set = False break return match_set @staticmethod def check_group_member(group_name, email): current_group = Group.query.filter_by(groupname=group_name).first() current_emails = current_group.get_all_member_emails() if email in current_emails: return True else: return False
class Schedule(db.Model): id = db.Column(db.Integer, primary_key=True) subject_id = db.Column(db.Integer, db.ForeignKey('subject.id'), nullable=False) starts = db.Column(db.DateTime, nullable=False) ends = db.Column(db.DateTime, nullable=False) duration = db.Column(db.Integer, nullable=False)
class Message(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(250), nullable=False) sender_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) is_palindrome = db.Column(db.Boolean, nullable=False) def to_dict(self): d = { 'id': self.id, 'body': self.body, 'sender_id': self.sender_id, 'recipient_id': self.recipient_id, 'is_palindrome': self.is_palindrome } return d
class Mapping(db.Model): id = db.Column(db.String(120), unique=True, nullable=False, primary_key=True) app_name = db.Column(db.String(120), db.ForeignKey('application.name'), nullable=False) app = db.relationship('Application', foreign_keys=app_name) image_url = db.Column(db.String(500), nullable=True) fields = db.Column(db.String(500), nullable=True) def __repr__(self): return '<Mapping %r>' % self.id
class Warranty(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) store_id = db.Column(db.Integer, db.ForeignKey("store.id"), nullable=False) store = relationship("Store", backref="warranty") item_id = db.Column(db.Integer, db.ForeignKey("item.id"), nullable=False) item = relationship("Item", backref="warranty") warranty_price = db.Column(db.Float, nullable=False) warranty_duration_months = db.Column(db.Integer, nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) def __init__(self, store_id, item_id, warranty_price, warranty_duration_months): self.store_id = store_id self.item_id = item_id self.warranty_price = warranty_price self.warranty_duration_months = warranty_duration_months def __repr__(self): return '<Warranty {}. item: {}>'.format(self.id, self.item_id)
class TenantHistory(db.Model): __tablename__ = 'tenants_history' id = db.Column(db.Integer, primary_key=True) tenant_id = db.Column(db.String(50), db.ForeignKey('tenants.tenant_id'), nullable=False) update_time = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) updated_by = db.Column(db.String(120), unique=False, nullable=False) updates_as_json = db.Column(db.String(10000), unique=False, nullable=False) @property def serialize(self): d = { 'tenant_id': self.tenant_id, 'update_time': str(self.update_time), 'updated_by': self.updated_by, 'updates': json.loads(self.updates_as_json) } return d
class Homework(db.Model): id = db.Column(db.Integer, primary_key=True) subject_id = db.Column(db.Integer, db.ForeignKey('subject.id'), nullable=False) exercise = db.Column(db.String)
class Tenant(db.Model): __tablename__ = 'tenants' id = db.Column(db.Integer, primary_key=True) tenant_id = db.Column(db.String(50), unique=True, nullable=False) base_url = db.Column(db.String(2000), unique=True, nullable=False) site_id = db.Column(db.String(50), primary_key=False, nullable=False) status = db.Column(db.Enum(TenantStatusTypes), unique=False, nullable=False) token_service = db.Column(db.String(2000), unique=False, nullable=False) security_kernel = db.Column(db.String(2000), unique=False, nullable=False) authenticator = db.Column(db.String(2000), unique=False, nullable=False) owner = db.Column(db.String(120), db.ForeignKey('tenantOwners.email'), nullable=False) admin_user = db.Column(db.String(120), unique=False, nullable=False) token_gen_services = db.Column(ARRAY(db.String), unique=False, nullable=False) create_time = db.Column(db.DateTime, nullable=False) last_update_time = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) created_by = db.Column(db.String(120), unique=False, nullable=False) last_updated_by = db.Column(db.String(120), unique=False, nullable=False) public_key = db.Column(db.String(10000), unique=False, nullable=True) # NOTE: ldap connections are not required if the tenant will use an alternative mechanism for authenticating # accounts: service_ldap_connection_id = db.Column(db.String(50), db.ForeignKey('ldap_connections.ldap_id'), nullable=True) user_ldap_connection_id = db.Column(db.String(50), db.ForeignKey('ldap_connections.ldap_id'), nullable=True) description = db.Column(db.String(1000), unique=False, nullable=True) def __repr__(self): return f'{self.tenant_id}: {self.description}' fields_metadata = { 'tenant_id': {'selectable': True, 'serializer': 'self.tenant_id'}, 'base_url': {'selectable': True, 'serializer': 'self.base_url'}, 'site_id': {'selectable': True, 'serializer': 'self.site_id'}, 'status': {'selectable': True, 'serializer': 'self.status.serialize'}, 'token_service': {'selectable': True, 'serializer': 'self.token_service'}, 'security_kernel': {'selectable': True, 'serializer': 'self.security_kernel'}, 'authenticator': {'selectable': True, 'serializer': 'self.authenticator'}, 'owner': {'selectable': True, 'serializer': 'self.owner'}, 'admin_user': {'selectable': True, 'serializer': 'self.admin_user'}, 'token_gen_services': {'selectable': True, 'serializer': 'self.token_gen_services'}, 'create_time': {'selectable': True, 'serializer': 'str(self.create_time)'}, 'created_by': {'selectable': True, 'serializer': 'self.created_by'}, 'last_update_time': {'selectable': True, 'serializer': 'str(self.last_update_time)'}, 'last_updated_by': {'selectable': True, 'serializer': 'self.last_updated_by'}, 'public_key': {'selectable': True, 'serializer': 'self.public_key'}, 'service_ldap_connection_id': {'selectable': True, 'serializer': 'self.service_ldap_connection_id'}, 'user_ldap_connection_id': {'selectable': True, 'serializer': 'self.user_ldap_connection_id'}, 'description': {'selectable': True, 'serializer': 'self.description'}, } selectable_fields = [attr for attr, meta in fields_metadata.items() if meta['selectable']] @property def serialize(self): d = {} for attr, meta in Tenant.fields_metadata.items(): if hasattr(self, attr) and getattr(self, attr): d[attr] = eval(meta['serializer']) return d def get_public_key(self): """ Return the public key associated with this tenant. :return: (str) The public key, as a string. """ # todo - This ultimately needs to be changed to look up the public key from the SK. return conf.dev_jwt_public_key