class Integrator(db.Model): """Model for the facility table""" __tablename__ = 'Integrator' org_id = db.Column(db.Integer(), primary_key=True) range = db.Column(db.ARRAY(db.DateTime())) facility = db.Column(db.ARRAY(db.String())) hours = db.Column(db.ARRAY(db.Integer())) def __repr__(self): return "<Integrator(id=%s)>" % (self.org_id)
class UpdateInfo(db.Model): ''' 只有一条记录 content 是所有已经爬取过的页面的 URL 列表 ''' __tablename__ = 'fish_update_info' update_id = db.Column( db.Integer, primary_key=True, ) content = db.Column( db.ARRAY(db.String), ) last_update_time = db.Column( db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, ) def __repr__(self): return "<UpdateInfo(last_update_time='%s')>" % ( self.last_update_time.strftime('%Y-%m-%d %H:%M:%S') )
class TAMU(db.Model): """Model for the TAMU table""" __tablename__ = 'TAMU' id = db.Column(db.Integer(), primary_key=True) bad_dates = db.Column(db.ARRAY(db.Date())) request_id = db.Column(db.Integer()) def create_request(self): db.session.add(self) db.session.commit()
class Record(CreateTimeMixin, db.Model): ''' 所有钓鱼帖的记录 ''' __tablename__ = 'fish_record' record_id = db.Column( db.Integer, primary_key=True, ) title = db.Column( db.String(100), ) content = db.Column( db.ARRAY(db.String), ) image_num = db.Column( db.Integer, ) source = db.Column( db.String(1000), ) views = db.Column( db.Integer, default=0, ) update_time = db.Column( # 不能自动更新,因为每次访问 views 都会更新,会导致更新时间也变化 db.DateTime, default=datetime.datetime.now, ) def __repr__(self): return "<Record(id='%d')>" % self.record_id @property def json(self): return { 'record_id': self.record_id, 'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'), 'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S'), 'title': self.title, 'content': self.content, 'image_num': self.image_num, 'source': self.source, 'views': self.views if self.views else 0, }
class NSRL(db.Model): """Model for the NSRL table""" __tablename__ = 'NSRL' id = db.Column(db.Integer(), primary_key=True) request_id = db.Column(db.Integer()) endDate = db.Column(db.Date()) experimentType = db.Column(db.String(50)) isNasa = db.Column(db.Boolean()) let = db.Column(db.String(100)) beamSize = db.Column(db.String(100)) maxDose = db.Column(db.String(100)) energies = db.Column(db.ARRAY(db.Integer())) def create_request(self): db.session.add(self) db.session.commit()
class requests(db.Model): """Model for the Requests table""" __tablename__ = 'requests' name = db.Column(db.String(50)) email = db.Column(db.String(128)) cell = db.Column(db.String(15)) company = db.Column(db.String(30)) integrator = db.Column(db.String(30)) funding_contact = db.Column(db.String(50)) address = db.Column(db.String(128)) city = db.Column(db.String(50)) state = db.Column(db.String(30)) zipcode = db.Column(db.Integer()) approved_integrator = db.Column(db.Boolean()) approved_facility = db.Column(db.Boolean()) facility = db.Column(db.String(30)) ion = db.Column(db.String(30)) energy = db.Column(db.Float()) id = db.Column(db.Integer(), primary_key=True) funding_cell = db.Column(db.String(15)) funding_email = db.Column(db.String(128)) start = db.Column(db.DateTime, nullable=False) ions = db.Column(db.ARRAY(db.Integer())) comments = db.Column(db.String(200)) po_number = db.Column(db.Integer()) username = db.Column(db.String(200)) beam_time = db.Column(db.Integer()) scheduled_start = db.Column(db.DateTime) integrator_comment = db.Column(db.String(200)) modified = db.Column(db.Boolean()) date_of_request = db.Column(db.DateTime) status = db.Column(db.String(40)) rejected = db.Column(db.Boolean()) order = db.Column(db.Integer()) request_range = db.Column(db.Integer()) priority = db.Column(db.Boolean()) ion_hours = db.Column(db.ARRAY(db.Integer())) shifts = db.Column(db.ARRAY(db.Integer())) hoursOn = db.Column(db.ARRAY(db.Integer())) hoursOff = db.Column(db.ARRAY(db.Integer())) totalHours = db.Column(db.ARRAY(db.Integer())) personnel = db.Column(db.String(200)) title = db.Column(db.String(200)) def create_request(self): db.session.add(self) db.session.commit() def __repr__(self): return "<Beam(id=%s, name=%s, facility=%s)>" % (self.id, self.name, self.facility)
class Beams(db.Model): """Model for the Beams table""" __tablename__ = 'Beams' id = db.Column(db.Integer(), primary_key=True) org_id = db.Column(db.Integer()) ion = db.Column(db.String(30)) amev = db.Column(db.Float()) max_energy = db.Column(db.Float()) let = db.Column(db.ARRAY(db.Float())) beam_range = db.Column(db.Float()) max_flux = db.Column(db.Float()) air = db.Column(db.Boolean()) device = db.Column(db.String(30)) let_peak = db.Column(db.Float()) def __repr__(self): return "<Beam(org_id=%s, ion=%s)>" % (self.org_id, self.ion)
class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), unique=True, nullable=False) first_name = db.Column(db.String(120), nullable=False) last_name = db.Column(db.String(120), nullable=False) phone = db.Column(db.String(120), nullable=False) gender = db.Column(db.String(15), nullable=False) dob = db.Column(db.String(15), nullable=False) dod = db.Column(db.String(15), nullable=False) password = db.Column(db.String(120), nullable=False) medications = db.Column(MutableList.as_mutable(db.ARRAY(db.String(120))), nullable=False) heatmap = db.Column(MutableList.as_mutable(db.ARRAY(db.String(120))), nullable=False) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_email(cls, email): return cls.query.filter_by(email=email).first() def to_json(x): return { 'email': x.email, 'first_name': x.first_name, 'last_name': x.last_name, 'phone': x.phone, 'gender': x.gender, 'dob': x.dob, 'dod': x.dod, 'password': x.password, 'medications': x.medications, 'heatmap': x.heatmap } @classmethod def return_all(cls): return { 'users': list(map(lambda x: to_json(x), UserModel.query.all())) } @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {'message': '{} row(s) deleted'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash)