class Validation(db.Model): __tablename__ = 'validation' id = db.Column(db.Integer,primary_key=True) diagnosis = db.Column(db.String(100)) remarks = db.Column(db.String(1000)) case_id = db.Column(db.Integer, db.ForeignKey('case.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) final = db.Column(db.Boolean) date = db.Column(db.DateTime()) def __init__(self, user='', case='', diagnosis='', remarks='', final=False):
class Patch(db.Model): id = db.Column(db.Integer, primary_key=True) image_id = db.Column(db.Integer, db.ForeignKey('image.id')) xcoord = db.Column(db.Integer) ycoord = db.Column(db.Integer) radius = db.Column(db.Integer) partype_id = db.Column(db.Integer, db.ForeignKey('partype.id')) def __init__(self, image_id="", xcoord="", ycoord="", radius=""): self.xcoord = xcoord self.ycoord = ycoord self.radius = radius def __repr__(self): return '<Patch %r>' % self.id
class Chunklist(db.Model): __tablename__ = 'chunklist' id = db.Column(db.Integer, primary_key=True) filename = db.Column(db.String(100)) date = db.Column(db.DateTime()) end_time = db.Column(db.DateTime()) chunks = db.relationship('Chunk', backref='chunklist', lazy='dynamic') user_id = db.Column(db.Integer, db.ForeignKey('user.id')) case = db.relationship('Case', backref='chunklist', uselist=False) def __init__(self, filename='', date='', user_id=''): self.filename = filename self.date = date.replace(microsecond=0) self.user_id = user_id def set_done(self, case): self.case = case self.end_time = datetime.datetime.now().replace(microsecond=0) @property def validation_filename(self): return '_'.join(self.filename.split('_')[:3]) @property def duration(self): if self.end_time: return str(self.end_time - self.date) else: return "Not done" def __repr__(self): return self.filename
class Municipality(db.Model): __tablename__ = 'municipality' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) cases = db.relationship('Case', backref='municipality', lazy='dynamic') province_id = db.Column(db.Integer, db.ForeignKey('province.id')) def __init__(self, name=''): self.name = name def __repr__(self): return self.name @property def serialize(self): return {'id': self.id, 'name': self.name}
class Chunk(db.Model): __tablename__ = 'chunk' id = db.Column(db.Integer, primary_key=True) filename = db.Column(db.String(100)) checksum = db.Column(db.String(50)) done = db.Column(db.Boolean()) chunklist_id = db.Column(db.Integer, db.ForeignKey('chunklist.id')) def __init__(self, filename='', checksum='', user=''): self.filename = filename self.checksum = checksum self.user = user self.done = False self.data = None def __repr__(self): return self.filename
class Image(db.Model): __tablename__ = 'image' id = db.Column(db.Integer, primary_key=True) case_id = db.Column(db.Integer, db.ForeignKey('case.id')) im = db.Column(db.BLOB) number = db.Column(db.Integer) patches = db.relationship('Patch', backref='image', lazy='dynamic') # images are labeled/tagged by several labelers # is_tagged = db.Column(db.Boolean, default=False) ''' def __init__(self, path="", case=""): with open(path, 'r') as f: self.im = f.read() self.case = case ''' def create_image(self, path="", case=""): with open(path, 'rb') as f: self.im = f.read() self.case = case
class ParType(db.Model): __tablename__ = 'partype' id = db.Column(db.Integer, primary_key=True) infection_id = db.Column(db.Integer, db.ForeignKey('infection.id')) name = db.Column(db.String(80)) negative = db.Column(db.Boolean()) patches = db.relationship('Patch', backref='partype', lazy='dynamic') def __init__(self, name="", infection="", negative=0): self.name = name self.infection = infection self.negative = negative @property def type(self): return self.name def __repr__(self): return self.name def __str__(self): return self.name
class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) password = db.Column(db.String(120)) firstname = db.Column(db.String(100)) lastname = db.Column(db.String(100)) usertype_id = db.Column(db.Integer, db.ForeignKey('usertype.id')) case = db.relationship('Case', backref='user', lazy='dynamic') contact = db.Column(db.String(80)) email = db.Column(db.String(80), unique=True) chunklists = db.relationship('Chunklist', backref='user', lazy='dynamic') validations = db.relationship('Validation', backref='user', lazy='dynamic') test = db.Column(db.Boolean) """ @models_committed.connect_via(app) def on_models_committed(sender, changes): for obj, method in changes: if method == 'insert' and type(obj) == User: body = '''Greetings, %s %s! You have successfully been registered to the system. Your temporary password is <b>%s</b>.<br><br>Below are your account details:<br><br>Username: %s<br>User type: %s<br>Contact number: %s<br>Email: %s<br><br>Please login and change your password. Thank you!''' % (obj.firstname, obj.lastname, '123', obj.username, obj.usertype, obj.contact, obj.email) msg = Message('Outbreak Monitoring', sender="*****@*****.**", recipients=['*****@*****.**',obj.email]) msg.html = body mail.send(msg) """ @validates('password') def update_password(self, key, value): Database.query.first().modified = datetime.datetime.now() return value @staticmethod def hash_password(password): return hashlib.sha1(password).hexdigest() def is_administrator(self): return self.usertype == UserType.get_administrator() def is_microscopist(self): return self.usertype == UserType.get_microscopist() def is_doctor(self): return self.usertype == UserType.get_doctor() def is_labeler(self): return self.usertype == UserType.get_labeler() def is_localadmin(self): return self.usertype == UserType.get_localadmin() def __init__(self, firstname="", lastname="", username="", password="", contact="", email=""): self.firstname = firstname self.lastname = lastname self.username = username self.password = password self.contact = contact self.email = email def __repr__(self): return '<User %r %r (%r)>' % (self.firstname, self.lastname, self.username) def __str__(self): return self.username
class Case(db.Model): __tablename__ = 'case' id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime()) # parasite = db.Column(db.String(100)) description = db.Column(db.String(1000)) comment = db.Column(db.String(1000)) lat = db.Column(db.Float) lng = db.Column(db.Float) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) images = db.relationship('Image', backref='case', lazy='dynamic') test = db.Column(db.Boolean) # partype_id = db.Column(db.Integer, db.ForeignKey('partype.id')) infection_id = db.Column(db.Integer, db.ForeignKey('infection.id')) region_id = db.Column(db.Integer, db.ForeignKey('region.id')) province_id = db.Column(db.Integer, db.ForeignKey('province.id')) municipality_id = db.Column(db.Integer, db.ForeignKey('municipality.id')) chunklist_id = db.Column(db.Integer, db.ForeignKey('chunklist.id')) parasite_validator = db.Column(db.String(100)) description_validator = db.Column(db.String(1000)) patient_id = db.Column(db.String(100)) validations = db.relationship('Validation', backref='case', lazy='dynamic') # def __init__(self, date="", partype="", description="", lat="", lng="", test="", region="", province="", municipality="", patient_id=""): def __init__(self, date="", description="", lat="", lng="", test="", region="", province="", municipality="", infection=""): self.date = date self.description = description self.lat = lat self.lng = lng self.test = test if region == '': self.region = None else: self.region = region if province == '': self.province = None else: self.province = province if municipality == '': self.municipality = None else: self.municipality = municipality if infection == '': self.infection = None else: self.infection = infection # if partype == '': # self.partype = None # else: # self.partype = partype # if patient_id == '': # self.patient_id = None # else: # self.patient_id = patient_id def __repr__(self): return '<Case %r>' % self.id def __str__(self): return str(self.id) @property def code(self): return "%s-%s-%s" % (self.region.code, self.province.code, self.id) @property def duration(self): if self.chunklist: return self.chunklist.duration else: return 'Not recorded' @property def final_validation(self): return Validation.query.filter(Validation.final==True, Validation.case==self).first() @property def finalized(self): return self.final_validation != None @property def finalized_text(self): return "Done" if self.finalized else "Pending" @property def validator(self): validation = self.final_validation if validation: return validation.user return None @property def validator_contacts(self): validator = self.validator if validator: return "%s / %s" % (validator.contact, validator.email) return None