class AnalysisFile(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) upload_time = db.Column(db.DateTime) path = db.Column(db.String(255)) description = db.Column(db.String(255)) is_encrypted = db.Column(db.Boolean) # Foreign keys analysis_id = db.Column(db.Integer, db.ForeignKey('analysis.id')) storage_location_id = db.Column(db.Integer, db.ForeignKey('storage_location.id')) # Relationships analysis = db.relationship('Analysis', backref=db.backref('analysis_files', lazy='dynamic', cascade='all, delete-orphan', ) ) storage_location = db.relationship('StorageLocation', backref=db.backref('analysis_files', lazy='dynamic')) def __init__(self, upload_time, path, description, analysis_id, storage_location_id, is_encrypted): self.upload_time = upload_time self.path = path self.description = description self.analysis_id = analysis_id self.storage_location_id = storage_location_id self.is_encrypted = is_encrypted def __repr__(self): return '<AnalysisFile: %r>' % self.id
class PermissionType(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) description = db.Column(db.String(255)) can_view_analysis_files = db.Column(db.Boolean) can_view_comments = db.Column(db.Boolean) can_view_measurements = db.Column(db.Boolean) can_edit_analysis_files = db.Column(db.Boolean) can_edit_comments = db.Column(db.Boolean) can_edit_measurements = db.Column(db.Boolean) def __init__(self, name='', description='', can_view_analysis_files=False, can_view_comments=False, can_view_measurements=False, can_edit_analysis_files=False, can_edit_comments=False, can_edit_measurements=False): self.name = name self.description = description self.can_view_analysis_files = can_view_analysis_files self.can_view_comments = can_view_comments self.can_view_measurements = can_view_measurements self.can_edit_analysis_files = can_edit_analysis_files self.can_edit_comments = can_edit_comments self.can_edit_measurements = can_edit_measurements def __repr__(self): return '<PermissionType: %r>' % self.name
class MeasurementSource(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), unique=True) description = db.Column(db.String(255)) def __init__(self, name='', description=''): self.name = name self.description = description def __repr__(self): return '<MeasurementSource: %r>' % (self.name)
class GroupMembershipType(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) description = db.Column(db.String(255)) def __init__(self, name='', description=''): self.name = name self.description = description def __repr__(self): return '<GroupMembershipType: %r>' % self.name
class StorageLocation(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), unique=True) description = db.Column(db.String(255)) website = db.Column(db.String(50)) def __init__(self, name='', description='', website=''): self.name = name self.description = description self.website = website def __repr__(self): return '<StorageLocation: %r>' % self.name
class MeasurementUnit(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), unique=True) symbol = db.Column(db.String(10)) suffix = db.Column(db.Boolean) def __init__(self, name='', symbol='', suffix=False): self.name = name self.symbol = symbol self.suffix = suffix def __repr__(self): return '<MeasurementUnit: %r>' % (self.name)
class Profile(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) last_name = db.Column(db.String(50)) first_name = db.Column(db.String(50)) birthday = db.Column(db.Date) is_health_professional = db.Column(db.Boolean) # Foreign keys gender_id = db.Column(db.Integer, db.ForeignKey('gender.id')) # Relationships gender = db.relationship('Gender', backref=db.backref('profiles', lazy='dynamic')) def __init__(self, last_name, first_name, birthday, gender_id, is_health_professional): self.last_name = last_name self.first_name = first_name self.birthday = birthday self.gender_id = gender_id self.is_health_professional = is_health_professional def __repr__(self): return '<Profile: %r %r>' % (self.first_name, self.last_name)
class Notification(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) created_datetime = db.Column(db.DateTime) read_datetime = db.Column(db.DateTime) type = db.Column(db.String(50)) # Foreign keys notification_author_id = db.Column(db.Integer, db.ForeignKey('profile.id')) notification_owner_id = db.Column(db.Integer, db.ForeignKey('profile.id')) # Relationships notification_author = db.relationship( 'Profile', foreign_keys=[notification_author_id]) notification_owner = db.relationship('Profile', backref=db.backref('notifications', lazy='dynamic'), foreign_keys=[notification_owner_id]) __mapper_args__ = { 'polymorphic_identity': 'notification', 'polymorphic_on': type } def __init__(self, notification_owner_id, notification_author_id): self.created_datetime = datetime.utcnow() self.read_datetime = None self.notification_author_id = notification_author_id self.notification_owner_id = notification_owner_id def set_as_read(self): self.read_datetime = datetime.utcnow() def get_title(self): raise NotImplementedError(u'Método no implementado.') def get_description(self): raise NotImplementedError(u'Método no implementado.') def get_detail_object_type(self): raise NotImplementedError(u'Método no implementado.') def get_detail_object_id(self): raise NotImplementedError(u'Método no implementado.') def get_notification_type(self): raise NotImplementedError(u'Método no implementado.') def __repr__(self): return '<Notification: %r>' % self.id
class StorageCredential(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) token = db.Column(db.String(255)) # Foreign keys owner_id = db.Column(db.Integer, db.ForeignKey('user.id')) storage_location_id = db.Column(db.Integer, db.ForeignKey('storage_location.id')) # Relationships owner = db.relationship('User', backref=db.backref('storage_credentials', lazy='dynamic')) storage_location = db.relationship('StorageLocation', backref=db.backref( 'storage_credentials', lazy='dynamic')) def __init__(self, token, owner_id, storage_location_id): self.token = token self.owner_id = owner_id self.storage_location_id = storage_location_id def __repr__(self): return '<StorageCredential: %r>' % self.id
class User(db.Model): # Attributes id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), unique=True, index=True) email = db.Column(db.String(64), unique=True) password_hash = db.Column(db.String(128)) rsa_private_key = db.Column(db.Text) rsa_public_key = db.Column(db.Text) is_admin = db.Column(db.Boolean) # Foreign keys profile_id = db.Column(db.Integer, db.ForeignKey('profile.id')) # Relationships profile = db.relationship('Profile', backref=db.backref('user', lazy='dynamic')) def __init__(self, username, email, password, profile_id, is_admin=False): self.username = username self.email = email self.hash_password(password) self.profile_id = profile_id self.is_admin = is_admin self.create_rsa_keys(2048) def __repr__(self): return '<User: %r>' % (self.username) def hash_password(self, password): self.password_hash = pwd_context.encrypt(password) def verify_password(self, password): return pwd_context.verify(password, self.password_hash) def generate_auth_token(self, expiration=600): s = Serializer(Config.SECRET_KEY, expires_in=expiration) return s.dumps({'id': self.id}) @staticmethod def verify_auth_token(token): s = Serializer(Config.SECRET_KEY) try: data = s.loads(token) except SignatureExpired: # Token válido, pero expirado. return None except BadSignature: # Token inválido. return None user = User.query.get(data['id']) return user def create_rsa_keys(self, key_size=2048): # Verifica que el usuario no tenga una clave privada asociada. if (self.rsa_private_key is None or self.rsa_private_key == ''): # Crea el par de claves RSA. private_key = RSA.generate(key_size) public_key = private_key.publickey() # Asocia las claves al usuario. self.rsa_private_key = private_key.exportKey() self.rsa_public_key = public_key.exportKey() # Flask-Login integration @property def is_authenticated(self): return self.is_admin @property def is_active(self): return True @property def is_anonymous(self): return False def get_id(self): return self.id