class Colonne(db.Model): numCol = db.Column(db.Integer, primary_key=True) graphId = db.Column(db.Integer, db.ForeignKey('graphique.graphId')) graphique = db.relationship("Graphique", backref=db.backref("colonnes", lazy="dynamic")) colName = db.Column(db.String(255)) dateDebut = db.Column(db.Date()) dateFin = db.Column(db.Date()) agregatSimple = db.Column(db.String(255)) capteur_id = db.Column(db.Integer, db.ForeignKey('capteur.captId')) capteur = db.relationship("Capteur") def __repr__(self): return "<Colonne (%d) (%s) >" % (self.numCol, self.colName) @property def aggreger(self): return agregats.get(self.agregatSimple, lambda x: 0) @property def valeurs(self): valeurs = [m.valeur for m in self.capteur.mesures.all()] if not self.agregatSimple: return valeurs else: v = self.aggreger(valeurs) return [v for _ in range(len(self.capteur.mesures.all()))] @property def valeurs_datees(self): return list(zip(self.dates, self.valeurs)) @property def dates(self): return [m.date for m in self.capteur.mesures.all()]
class Reservation(BaseDataModel): __tablename__ = 'reservation' startdate = db.Column(db.Date()) enddate = db.Column(db.Date()) roomno = db.Column(db.Integer(), db.ForeignKey('room.roomno', ondelete='NO ACTION'), nullable=False)
class ClosingDays(ModelBase): __tablename__ = 'closing_days' id = db.Column(db.Integer(), primary_key=True, autoincrement=True) campus_id = db.Column(db.Integer(), db.ForeignKey('campus.id'), nullable=False) first_day = db.Column(db.Date(), nullable=False) last_day = db.Column(db.Date(), nullable=True, server_default=None) translatable_id = db.Column(db.Integer(), db.ForeignKey('translatable.id', onupdate='CASCADE', ondelete='RESTRICT'), nullable=False) def __init__(self, campus_id: int, first_day: datetime.date, last_day: datetime.date, translatable_id: int): if not isinstance(campus_id, int): raise expected('campus_id', campus_id, int) if not isinstance(first_day, datetime.date): raise expected('first_day', first_day, datetime.date) if last_day is not None and not isinstance(last_day, datetime.date): raise expected_or_none('last_day', last_day, datetime.date) if not isinstance(translatable_id, int): raise expected('translatable_id', translatable_id, int) self.campus_id = campus_id self.first_day = first_day self.last_day = last_day self.translatable_id = translatable_id @staticmethod def create(campus: Campus, first_day: datetime.date, last_day: Optional[datetime.date], reason: str, language: str, add_to_db=True) -> 'ClosingDays': translatable, translation = Translatable.get_or_create(reason, language) result = ClosingDays(campus.id, first_day, last_day, translatable.id) if add_to_db: db.session.add(result) return result @staticmethod def find_is_closed(campus: Campus, day: datetime.date) -> 'Optional[ClosingDays]': return ClosingDays.query.filter(db.and_(ClosingDays.campus_id == campus.id, ClosingDays.first_day <= day, db.or_( ClosingDays.last_day == None, ClosingDays.last_day >= day ) )).first() @staticmethod def find_closing_days_including(campus: Campus, start_date: datetime.date, end_date: datetime.date) -> 'List[ClosingDays]': return ClosingDays.query.filter(db.and_(ClosingDays.campus_id == campus.id, ClosingDays.first_day <= end_date, db.or_( ClosingDays.last_day == None, ClosingDays.last_day >= start_date ) )).all()
class TimeOff(db.Model): __tablename__ = 'time_offs' id = db.Column(db.Integer(), primary_key=True) startDate = db.Column(db.Date(), nullable=False, default=datetime.today().strftime('%Y-%m-%d')) endDate = db.Column(db.Date(), nullable=False, default=datetime.today().strftime('%Y-%m-%d')) idParkingSpot = db.Column(db.Integer(), db.ForeignKey('parking_spots.id', ondelete='CASCADE'))
class Reservation(db.Model): __tablename__ = 'reservation' id = db.Column(db.Integer(), primary_key=True) time = db.Column(db.Date(), nullable=False) user_id = db.Column(db.Integer()) space_id = db.Column(db.Integer(), nullable=False) created_at = db.Column(db.DateTime(), nullable=False, server_default=db.func.now()) @classmethod def get_all_reservations(cls): return cls.query.all() @classmethod def get_by_id(cls, reservation_id): return cls.query.filter_by(id=reservation_id).first() def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() @classmethod def get_all_by_user_id(cls, user_id): return cls.query.filter_by(user_id=user_id).all() @classmethod def get_all_by_space_id(cls, space_id): return cls.query.filter_by(space_id=space_id).all()
class Task(db.Model): __tablename__ = 'task' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), nullable=False) description = db.Column(db.String(256)) priority = db.Column(db.Integer, nullable=False) type = db.Column(db.String(32), nullable=False) status = db.Column(db.String(32), nullable=False) due_date = db.Column(db.Date()) start = db.Column(db.DateTime()) end = db.Column(db.DateTime()) user_id = db.Column(db.Integer(), db.ForeignKey("user.id")) @classmethod def get_all(cls, user_id, type): return cls.query.filter_by(user_id=user_id, type=type) @classmethod def get_by_id(cls, task_id): return cls.query.filter_by(id=task_id).first() @classmethod def delete(cls, task_id): cls.query.filter_by(id=task_id).delete() db.session.commit() def save(self): db.session.add(self) db.session.commit()
class Product(db.Model): id = db.Column(db.Integer(), primary_key=True) type = db.Column(db.String(10)) origin = db.Column(db.String(4)) destination = db.Column(db.String(4)) adults = db.Column(db.Integer()) childs = db.Column(db.Integer()) infants = db.Column(db.Integer()) flights = db.relationship('Flight') total_duration = db.Column(db.Integer()) prices = db.Column(CastingArray(JSON)) total_price = db.Column(db.Float()) price_currency = db.Column(db.String(3)) airlines = db.Column(ARRAY(db.String(2))) departure_date = db.Column(db.Date()) return_date = db.Column(db.Date()) flight_classes = db.Column(ARRAY(db.String(1))) search_id = db.Column(db.Integer(), db.ForeignKey('search.id')) def __repr__(self): return '{origin} - {destination}'.format(origin=self.origin, destination=self.destination)
class Employee(BaseDataModel): __tablename__ = 'employee' address = db.Column(db.String(256)) bankname = db.Column(db.String(64)) birthdate = db.Column(db.Date()) birthplace = db.Column(db.String(64)) bloodgrp = db.Column(db.String(3)) city = db.Column(db.String(64)) country = db.Column(db.String(64)) fullname = db.Column(db.String(64)) email = db.Column(db.String(64), unique=True) gender = db.Column(db.String(6)) iban = db.Column(db.String(26)) idno = db.Column(db.String(64)) maritalstatus = db.Column(db.Boolean()) paycurrid = db.Column(db.Integer(), db.ForeignKey('currency.id', ondelete='NO ACTION'), default=1) salaryamount = db.Column(db.Float(), nullable=False, default=0.0) salaryday = db.Column(db.Integer, nullable=False, default=1) enddate = db.Column(db.Date()) tel = db.Column(db.String(16)) userid = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='SET NULL'))
class History(db.Model): __tablename__ = 'history' id = db.Column(db.Integer, primary_key=True) task_id = db.Column(db.Integer, nullable=False) task_title = db.Column(db.String(128), nullable=False) type = db.Column(db.String(32), nullable=False) start = db.Column(db.DateTime(), nullable=False) end = db.Column(db.DateTime(), nullable=False) status = db.Column(db.String(16), nullable=False) txn_date = db.Column(db.Date()) utc_offset = db.Column(db.Integer) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) @classmethod def get_by_user_id(cls, user_id, txn_date): return cls.query.filter_by(user_id=user_id, txn_date=txn_date) def save(self): db.session.add(self) db.session.commit()
class LearningDatapoint(ModelBase): __tablename__ = 'learning_datapoint' id = db.Column(db.Integer(), primary_key=True, autoincrement=True) campus_id = db.Column(db.Integer(), db.ForeignKey('campus.id'), nullable=False) menu_day = db.Column(db.Date(), nullable=False) screenshot = db.Column(db.Text(), nullable=False) processed_data = db.Column(db.Text(), nullable=False) submissions = db.relationship('LearningDatapointSubmission', backref='datapoint', passive_deletes=True) def __init__(self, campus_id: int, menu_day: datetime.date, screenshot: str, processed_data: Any): if not isinstance(campus_id, int): raise expected('campus_id', campus_id, int) if not isinstance(menu_day, datetime.date): raise expected('menu_day', menu_day, datetime.date) if screenshot is None: raise ValueError('screenshot expected not None') if processed_data is None: raise ValueError('processed_data expected not None') self.campus_id = campus_id self.menu_day = menu_day self.screenshot = screenshot self.processed_data = json.dumps(processed_data) @staticmethod def create(campus: 'Campus', menu_day: datetime.date, screenshot: str, processed_data: Any) -> 'Optional[LearningDatapoint]': datapoint = LearningDatapoint(campus.id, menu_day, screenshot, processed_data) db.session.add(datapoint) return datapoint @staticmethod def find_by_id(datapoint_id: int) -> 'Optional[LearningDatapoint]': return LearningDatapoint.query.filter_by(id=datapoint_id).first() @staticmethod def get_all() -> 'List[LearningDatapoint]': return LearningDatapoint.query.all() @staticmethod def get_random(user: '******') -> 'Optional[LearningDatapoint]': return LearningDatapoint.query.order_by( expression.func.random()).filter( expression.not_( LearningDatapointSubmission.query.filter( LearningDatapoint.id == LearningDatapointSubmission.datapoint_id, LearningDatapointSubmission.user_id == user.id).exists())).first() def user_submit(self, user: '******', submission_data: Any): LearningDatapointSubmission.create(self, user, submission_data) def __hash__(self): return hash(self.id)
class Menu(ModelBase): __tablename__ = 'menu' id = db.Column(db.Integer(), primary_key=True, autoincrement=True) campus_id = db.Column(db.Integer(), db.ForeignKey('campus.id'), nullable=False) menu_day = db.Column(db.Date(), nullable=False) menu_items: 'Collection[MenuItem]' = db.relationship('MenuItem', backref='menu', passive_deletes=True, order_by='[MenuItem.course_type, MenuItem.course_sub_type]') def __init__(self, campus_id: int, day: datetime.date): if not isinstance(campus_id, int): raise expected('campus_id', campus_id, int) if not isinstance(day, datetime.date): raise expected('day', day, datetime.date) self.campus_id = campus_id self.menu_day = day def delete(self): db.session.delete(self) def add_menu_item(self, translatable: Translatable, course_type: CourseType, course_sub_type: CourseSubType, course_attributes: List[CourseAttributes], course_allergens: List[CourseAllergens], price_students: Decimal, price_staff: Optional[Decimal]) -> 'MenuItem': menu_item = MenuItem(self, translatable.id, course_type, course_sub_type, price_students, price_staff) menu_item.set_attributes(course_attributes) menu_item.set_allergens(course_allergens) # FIXME: Is this safe? self.menu_items.append(menu_item) return menu_item @staticmethod def create(campus: Campus, day: datetime.date, add_to_db=True) -> 'Menu': menu = Menu(campus.id, day) if add_to_db: db.session.add(menu) return menu @staticmethod def get_menu(campus: Campus, day: datetime.date) -> 'Optional[Menu]': return Menu.query.filter_by(campus_id=campus.id, menu_day=day).first() @staticmethod def remove_menus_on_closing_days(): rows = Menu.query.filter( ClosingDays.query.filter( Menu.campus_id == ClosingDays.campus_id, Menu.menu_day >= ClosingDays.first_day, Menu.menu_day <= ClosingDays.last_day ).exists() ).all() for row in rows: db.session.delete(row) def __hash__(self): return hash(self.id)