class Solution(db.Model): # Класс решения id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(1000), unique=False, nullable=False) status = db.Column(db.String(50), unique=False, nullable=False) student_id = db.Column(db.Integer, db.ForeignKey('student.id'), nullable=False) student = db.relationship('Student', backref=db.backref('Solutions', lazy=True)) task_id = db.Column(db.Integer, db.ForeignKey('task.id'), nullable=False) task = db.relationship('Task', backref=db.backref('Solutions', lazy=True))
class OrganizationUser(db.Model): __tablename__ = 'organization_users' id = db.Column(db.Integer, primary_key=True) organization_id = db.Column(db.ForeignKey('resources.id'), nullable=False) organization = db.relationship('Resource') user_id = db.Column(db.ForeignKey('users.id'), nullable=False) user = db.relationship('User') is_admin = db.Column(db.Boolean, default=False, nullable=False) def as_dict(self): return { 'organization_id': self.organization_id, 'user_id': self.user_id, 'is_admin': self.is_admin, }
class Comment(db.Model): # Класс комментария id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(100), unique=False, nullable=False) content = db.Column(db.String(1000), unique=False, nullable=False) article_id = db.Column(db.Integer, db.ForeignKey('article.id'), nullable=False) article = db.relationship('Article', backref=db.backref('Comments', lazy=True))
class Club(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) league = db.Column(db.String(100), default='Uganda Premier League') logo_name = db.Column(db.String(200), default='avatar.png') home_rating = db.Column(db.Float, default=0.0) away_rating = db.Column(db.Float, default=0.0) ave_rating = db.Column(db.Float, default=0.0) manager = db.relationship('Manager', secondary=club_manager, backref=db.backref('clubs', lazy='dynamic')) players = db.relationship( 'Player', secondary=club_players, backref=db.backref('clubs', lazy='dynamic'), ) def __init__(self, *args, **kwargs): super(Club, self).__init__(*args, **kwargs) def __repr__(self): return '<Club: %s >' % self.name
class Photo(db.Model): id = db.Column(db.Integer, primary_key=True) identifier = db.Column(db.String(100)) photo_name = db.Column(db.String(100)) created_time = db.Column(db.DateTime, default=datetime.datetime.now) faces = db.relationship('Face', secondary=faces, backref=db.backref('photos', lazy='dynamic') ) def __init__(self, *args, **kwargs): super(Photo, self).__init__(*args, **kwargs) def __repr__(self): return '<Photo: %s>' % self.identifier
class Player(db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) role = db.Column(db.String(100), nullable=False) curr_perf = db.Column(db.Float, default=0.00) photo_name = db.Column(db.String(400), unique=True, nullable=False) date_of_birth = db.Column(db.DateTime, nullable=False) week = db.Column(db.Integer, default=0) num_matches = db.Column(db.Integer, default=0) stats = db.relationship('Stats', secondary=player_stats, backref=db.backref('players', lazy='dynamic')) performance = db.relationship( 'Performance', secondary=player_performance, backref=db.backref('players', lazy='dynamic'), ) def __init__(self, *args, **kwargs): super(Player, self).__init__(*args, **kwargs) def __repr__(self): return '<Player: %s >' % self.first_name
class Manager(db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) email = db.Column(db.String(100), unique=True, nullable=False) password = db.Column(db.String(100), nullable=False) image_name = db.Column(db.String(400), default='avatar.png') messages = db.relationship('Message', secondary=manager_messages, backref=db.backref('managers', lazy='dynamic')) def __init__(self, *args, **kwargs): super(Manager, self).__init__(*args, **kwargs) def __repr__(self): return '<Manager; First Name: %s >' % self.first_name
class Account(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) email = db.Column(db.String(100)) phone = db.Column(db.String(20)) password = db.Column(db.String(100)) photo_name = db.Column(db.String(400), default='avatar.png') created_time = db.Column(db.DateTime) id_photos = db.relationship('Photo', secondary=id_photos, backref=db.backref('users', lazy='dynamic') ) def __init__(self, *args, **kwargs): super(Account, self).__init__(*args, **kwargs) def __repr__(self): return '<Account: %s>' % self.name
class AccountRequest(db.Model): __tablename__ = 'account_requests' id = db.Column(db.Integer, primary_key=True) organization_name = db.Column(db.String(100)) # used for new organization organization_id = db.Column( db.ForeignKey('resources.id')) # used to join existing organization organization = db.relationship( 'Resource') # used to join existing organization inviter_id = db.Column( db.ForeignKey('users.id')) # used to join existing organization creation_timestamp = db.Column(db.DateTime, nullable=False) redeemed_timestamp = db.Column(db.DateTime) access_code = db.Column(db.String(40), nullable=False) email_address = db.Column(db.String, nullable=False) email_sent = db.Column(db.Boolean, nullable=False) # True if sent successfully email_failed = db.Column(db.Boolean, nullable=False) # True if given up on sending attributes = db.Column( db.String, nullable=False) # JSON field containing extra attributes
class Resource(db.Model): __tablename__ = 'resources' id = db.Column(db.Integer, primary_key = True) last_revision_id = db.Column(db.Integer) # can be null for folder resources or empty files/sequences organization_id = db.Column(db.ForeignKey('resources.id')) # would like this to be non-null, but how else do we create first organization? creation_timestamp = db.Column(db.DateTime) modification_timestamp = db.Column(db.DateTime) # fix(later): remove this and use revision timestamp? what about folders? should we track modification timestamps for this? # meta-data that could eventually have change tracking (probably best to move into a separate table e.g. resource_meta_revisions) parent_id = db.Column(db.ForeignKey('resources.id'), index = True) parent = db.relationship('Resource', remote_side = [id], foreign_keys = [parent_id]) name = db.Column(db.String, nullable = False) type = db.Column(db.Integer, nullable = False) # fix(later): add index for this? permissions = db.Column(db.String) # JSON; NULL -> inherit from parent system_attributes = db.Column(db.String, nullable = False, default = '{}') # JSON dictionary; additional attributes of the resource (system-defined) user_attributes = db.Column(db.String) # JSON dictionary; additional attributes of the resource (user-defined) deleted = db.Column(db.Boolean, nullable = False, default = False) # fix(soon): remove after migrate file_type = db.Column(db.String(20)) hash = db.Column(db.String(50)) size = db.Column(db.BigInteger) # resource types BASIC_FOLDER = 10 ORGANIZATION_FOLDER = 11 CONTROLLER_FOLDER = 12 REMOTE_FOLDER = 13 FILE = 20 SEQUENCE = 21 APP = 22 # sequence data types NUMERIC_SEQUENCE = 1 TEXT_SEQUENCE = 2 IMAGE_SEQUENCE = 3 # ======== methods ======== # get information about the resource in a json-ready dictionary def as_dict(self, extended = False): d = { 'id': self.id, 'name': self.name, 'type': self.type, } if extended: d['parent_id'] = self.parent_id if self.creation_timestamp: # fix(clean): remove this after make sure everything has timestamp d['creationTimestamp'] = self.creation_timestamp.isoformat() + 'Z' # fix(soon): remove d['creation_timestamp'] = self.creation_timestamp.isoformat() + 'Z' if self.modification_timestamp: # fix(clean): remove this after make sure everything has timestamp d['modificationTimestamp'] = self.modification_timestamp.isoformat() + 'Z' # fix(soon): remove d['modification_timestamp'] = self.modification_timestamp.isoformat() + 'Z' d['permissions'] = json.loads(self.permissions) if self.permissions else [] d['settings'] = json.loads(self.system_attributes) if self.system_attributes else {} # fix(soon): remove d['system_attributes'] = json.loads(self.system_attributes) if self.system_attributes else {} d['user_attributes'] = json.loads(self.user_attributes) if self.user_attributes else {} d['lastRevisionId'] = self.last_revision_id # fix(soon): remove d['last_revision_id'] = self.last_revision_id if self.last_revision_id: d['storage_path'] = self.storage_path(self.last_revision_id) if self.type == self.FILE: # fix(later): remove this case after migrate DB and update client sync code (and browser display code) to use direct system_attributes d['hash'] = d['system_attributes'].get('hash') or self.hash d['size'] = d['system_attributes'].get('size') or self.size return d # get the path of the resource (including it's own name) # includes leading slash def path(self): if self.parent: path = self.parent.path() + '/' + self.name else: path = '/' + self.name return path # returns True if this resource is a child/grandchild/etc. of the specified resource def is_descendent_of(self, resource_id): if not self.parent_id: return False elif self.parent_id == resource_id: return True else: return self.parent.is_descendent_of(resource_id) # get a list of folders contained within this folder (recursively) # fix(clean): maybe this is too specialized; move elsewhere? def descendent_folder_ids(self): ids = [] children = Resource.query.filter(Resource.parent_id == self.id, Resource.deleted == False) for child in children: if child.type >= 10 and child.type < 20: ids.append(child.id) ids += child.descendent_folder_ids() return ids # the root of a hierachy of resources; this will generally be an organization (or system folder such as 'doc' or 'system') def root(self): if self.parent: return self.parent.root() else: return self # get a list of permission applied to this resource (including inherited from parents) def query_permissions(self): permissions = json.loads(self.permissions) if self.permissions else None if self.parent: parent_permissions = self.parent.query_permissions() # if we have permissions at this level, we need to merge in the parent permissions if self.permissions: permission_dict = {(type, id): level for (type, id, level) in self.permissions} for permission_record in parent_permissions: (type, id, level) = permission_record if not (type, id) in permission_dict: permissions.append((type, id, level)) # otherwise, just use the parent permissions (the common case) else: permissions = parent_permissions return permissions # get the path of the resource in the bulk storage system def storage_path(self, revision_id): org_id = self.organization_id if not org_id: # fix(clean): remove this org_id = self.root().id id_str = '%09d' % self.id return '%d/%s/%s/%s/%d_%d' % (org_id, id_str[-9:-6], id_str[-6:-3], id_str[-3:], self.id, revision_id)