示例#1
0
class SiteModel(db.Model):
    __tablename__ = 'sites'

    site_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(80))
    url = db.Column(db.String(180))
    hoteis = db.relationship('HotelModel')

    def __init__(self, name, url):
        self.name = name
        self.url = url

    def json(self):
        return {
            "site_id": self.site_id,
            "name": self.name,
            "url": self.url,
            "hoteis": [hotel.json() for hotel in self.hoteis]
        }

    @classmethod
    def find_site(cls, url):
        site = cls.query.filter_by(url=url).first()
        if site:
            return site

        return None

    def save_site(self):
        db.session.add(self)
        db.session.commit()

    def delete_site(self):
        db.session.delete(self)
        db.session.commit()
示例#2
0
class CloudModel(db.Model):
    __tablename__ = "cloud"

    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    cloudProviderEnum = db.Column(db.String(30))
    region = db.Column(db.String(255))
    zone = db.Column(db.String(255))
    amount = db.Column(db.Float(precision=2))
    services = db.relationship("ServiceModel", cascade="all")

    def __init__(self, cloudProviderEnum, region, zone, amount,
                 services: List[ServiceModel]):
        self.cloudProviderEnum = cloudProviderEnum
        self.region = region
        self.zone = zone
        self.amount = amount
        self.services = services

    def json(self):
        return {
            "id": self.id,
            "cloudProviderEnum": self.cloudProviderEnum,
            "region": self.region,
            "zone": self.zone,
            "amount": self.amount,
            "services": [service.json() for service in self.services]
        }
示例#3
0
class MicroServiceModel(db.Model):
    __tablename__ = "micro_service"

    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    name = db.Column(db.String(255))
    description = db.Column(db.String(255))
    cloud_id = db.Column(db.Integer, db.ForeignKey("cloud.id"))
    cloud = db.relationship(
        "CloudModel",
        backref="micro_service",
        primaryjoin="CloudModel.id==MicroServiceModel.cloud_id",
        uselist=False,
        cascade="all")

    budgeting_id = db.Column(db.Integer,
                             db.ForeignKey('budgeting.id'),
                             nullable=False)

    def __init__(self, name, description, cloud: CloudModel):
        self.name = name
        self.description = description
        self.cloud = cloud

    def json(self):
        return {
            "id": self.id,
            "name": self.name,
            "description": self.description,
            "cloud": self.cloud.json()
        }
示例#4
0
class MenuModel(db.Model):
    __tablename__ = 'menu'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    price = db.Column(db.String(15), nullable=False)
    description = db.Column(db.String(256), nullable=False)
    menu = db.relationship(RecipeModel, backref=db.backref('menu', lazy=True))


    def __init__(self, name, price, description):
        self.name = name
        self.price = price
        self.description = description


    def json(self):

        return {
            'id': self.id,
            'name': self.name,
            'price': str(self.price),
            'description': self.description,
            'menu': [self.format_recipe(recipe.id, recipe.name, recipe.quantity) for recipe in self.menu]

        }

    def format_recipe(self, id, name, quantity):
        return {
            "id": id,
            "name": name,
            "quantity": quantity
        }
示例#5
0
class MonitoringModel(db.Model):
    __tablename__ = 'monitoring'

    id = db.Column(db.Integer, primary_key=True)
    identifier = db.Column(db.String(60), unique=True)
    date_created=db.Column(db.DateTime(6),default=db.func.current_timestamp(),nullable=False)
    status = db.Column(db.Boolean)
    device_id = db.Column(db.String(30))
    tests = db.relationship('TestModel') 

    def __init__(self, identifier, status, device_id):
        self.identifier = identifier
        self.device_id = device_id
        self.date_created=date.today()
        self.status = status
    
    def json(self):
        return {
            
            'identifier': self.identifier,
            'status':self.status,
            #'tests':[test.json() for test in self.tests]
        }
    
    @classmethod
    def find_by_identifier(cls, identifier):
        print(identifier)
        monitoring =  cls.query.filter_by(identifier=identifier).first()
        if monitoring:
            return monitoring
        return None
    
    @classmethod
    def find_by_id(cls, identifier):
        monitoring =  cls.query.filter_by(identifier=identifier).first()
       
        if monitoring:
            return monitoring
        return None
    
    @classmethod
    def find__running(cls, status=True):
        monitoring = cls.query.order_by(desc(cls.id)).filter(and_(cls.status==1,cls.date_created==str(date.today()))).limit(250).all()
        return monitoring
        
    def save(self):
        db.session.add(self)
        db.session.commit()

    def update(self, status=False):
        self.status = status
        
    def delete(self):
        [test.delete() for test in self.tests]
        db.session.delete(self)
        db.session.commit()
示例#6
0
class FornecedorModel(db.Model):
    __tablename__ = 'fornecedor'

    cod_fornecedor = db.Column(db.Integer, primary_key=True)
    cnpj_cpf = db.Column(db.String(18), unique=True, nullable=False)
    nome_fantasia = db.Column(db.String(150), nullable=False)
    razao_social = db.Column(db.String(150), nullable=False)
    ativo = db.Column(db.String(3), nullable=False)
    contato = db.relationship("ContatoModel")

    def __init__(self, cnpj_cpf, nome_fantasia, razao_social, ativo):
        self.cnpj_cpf = cnpj_cpf
        self.nome_fantasia = nome_fantasia
        self.razao_social = razao_social
        self.ativo = ativo

    def json(self):
        return {
            'cod_fornecedor': self.cod_fornecedor,
            'cnpj_cpf': self.cnpj_cpf,
            'nome_fantasia': self.nome_fantasia,
            'razao_social': self.razao_social,
            'ativo': self.ativo,
            'contatos': [contato.json() for contato in self.contato]
        }

    @classmethod
    def find_fornecedor(cls, cod_fornecedor):
        fornecedor = cls.query.filter_by(cod_fornecedor=cod_fornecedor).first()
        if fornecedor:
            return fornecedor
        return None

    @classmethod
    def find_fornecedor_cnpj_cpf(cls, cnpj_cpf):
        fornecedor = cls.query.filter_by(cnpj_cpf=cnpj_cpf).first()
        if fornecedor:
            return fornecedor
        return None

    def save_fornecedor(self):
        db.session.add(self)
        db.session.commit()

    def update_fornecedor(self, cod_fornecedor, cnpj_cpf, nome_fantasia,
                          razao_social, ativo):
        self.cod_fornecedor = cod_fornecedor
        self.cnpj_cpf = cnpj_cpf
        self.nome_fantasia = nome_fantasia
        self.razao_social = razao_social
        self.ativo = ativo

    def delete_fornecedor(self):
        [contato.delete_contato() for contato in self.contato]
        db.session.delete(self)
        db.session.commit()
class BrandFoodChemicalModel(db.Model):

    __tablename__ = 'brand_food_chemical'

    id_brand = db.Column(db.Integer, db.ForeignKey('brand.id'), primary_key=True)
    id_food = db.Column(db.Integer, db.ForeignKey('food.id'), primary_key=True)
    id_chemical = db.Column(db.Integer, db.ForeignKey('chemical.id'), primary_key=True)

    chemicals = db.relationship('ChemicalModel', lazy='joined')

    def __init__(self, id_brand, id_food, id_chemical):
        self.id_brand = id_brand
        self.id_food = id_food
        self.id_chemical = id_chemical

    def json(self):
        return {
            'brandId': self.id_brand,
            'foodId': self.id_food,
            'chemicalId': self.id_chemical
        }

    def chemical_name(self):
        return self.chemicals.json()['name']

    @classmethod
    def find_by_brand_food(cls, brand_id, food_id):
        return cls.query.filter_by(id_brand=brand_id, id_food=food_id).order_by(unaccent(text("name")))

    @classmethod
    def find_by_id(cls, brand_id, food_id, chemical_id):
        return cls.query.enable_eagerloads(False).filter_by(id_brand=brand_id, id_food=food_id, id_chemical=chemical_id).first()

    @classmethod
    def find_all(cls):
        return cls.query.enable_eagerloads(False).all()

    def save(self):
        db.session.add(self)

    def delete(self):
        db.session.delete(self)
        db.session.commit()
示例#8
0
class BudgetingModel(db.Model):
    __tablename__ = "budgeting"

    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    name = db.Column(db.String(255))
    totalCost = db.Column(db.Float(precision=2))
    periodEnum = db.Column(db.String(10))
    currencyEnum = db.Column(db.String(6))
    effectiveFrom = db.Column(db.Date())
    effectiveTo = db.Column(db.Date())
    description = db.Column(db.String(255))
    paymentTypeEnum = db.Column(db.String(10))
    microServices = db.relationship("MicroServiceModel", cascade="all")
    running = db.Column(db.String(3), default="NO")
    creation = db.Column(db.Date(), server_default=sqlalchemy.sql.func.now())

    def __init__(self, name, totalCost, periodEnum, currencyEnum,
                 effectiveFrom, effectiveTo, description, paymentTypeEnum,
                 microServices: List[MicroServiceModel]):
        self.name = name
        self.totalCost = totalCost
        self.periodEnum = periodEnum
        self.currencyEnum = currencyEnum
        self.effectiveFrom = effectiveFrom
        self.effectiveTo = effectiveTo
        self.description = description
        self.paymentTypeEnum = paymentTypeEnum
        self.microServices = microServices

    def json(self):
        return {
            "id":
            self.id,
            "name":
            self.name,
            "totalCost":
            self.totalCost,
            "periodEnum":
            self.periodEnum,
            "currencyEnum":
            self.currencyEnum,
            "effectiveFrom":
            str(self.effectiveFrom),
            "effectiveTo":
            str(self.effectiveTo),
            "description":
            self.description,
            "paymentTypeEnum":
            self.paymentTypeEnum,
            "running":
            self.running,
            "creation":
            str(self.creation),
            "microServices":
            [microService.json() for microService in self.microServices]
        }

    @classmethod
    def find_budgeting(cls, id):
        budgeting = cls.query.filter_by(
            id=id).first()  # select * from budgeting where id = $id
        if budgeting:
            return budgeting
        return None

    @classmethod
    def find_by_running_scenario(cls):
        budgeting = cls.query.filter_by(running="YES").first(
        )  # select * from budgeting where running = YES
        if budgeting:
            return budgeting
        return None

    def save_budgeting(self):
        db.session.add(self)
        db.session.commit()

    def update_scenario_budgeting(self, running):
        self.running = running

    def delete_budgeting(self):
        db.session.delete(self)
        db.session.commit()
示例#9
0
class BrandFoodModel(db.Model):

    __tablename__ = 'brand_food'

    id_brand = db.Column(db.Integer,
                         db.ForeignKey('brand.id'),
                         primary_key=True)
    id_food = db.Column(db.Integer, db.ForeignKey('food.id'), primary_key=True)

    bar_code = db.Column(db.String(13),
                         nullable=False,
                         unique=True,
                         index=True)

    brands = db.relationship('BrandModel', lazy='joined')
    foods = db.relationship('FoodModel', lazy='joined')

    def __init__(self, id_brand, id_food, bar_code):
        self.id_brand = id_brand
        self.id_food = id_food
        self.bar_code = bar_code

    def json(self):
        return {
            'brandId': self.id_brand,
            'foodId': self.id_food,
            'barCode': self.bar_code
        }

    def brand(self):
        brand = self.brands.json()
        return {
            'barCode': self.bar_code,
            'id': brand['id'],
            'name': brand['name']
        }

    def food(self):
        food = self.foods.json()
        return {
            'barCode': self.bar_code,
            'id': food['id'],
            'name': food['name']
        }

    @classmethod
    def find_by_food(cls, food_id):
        return cls.query.filter_by(id_food=food_id).order_by(
            unaccent(text("food_1.name")))

    @classmethod
    def find_by_brand(cls, brand_id):
        return cls.query.filter_by(id_brand=brand_id).order_by(
            unaccent(text("brand_1.name")))

    @classmethod
    def find_by_id(cls, brand_id, food_id):
        return cls.query.enable_eagerloads(False).filter_by(
            id_brand=brand_id, id_food=food_id).first()

    @classmethod
    def find_by_bar_code(cls, bar_code):
        return cls.query.enable_eagerloads(False).filter_by(
            bar_code=bar_code).first()

    @classmethod
    def find_all(cls):
        return cls.query.enable_eagerloads(False).all()

    def save(self):
        db.session.add(self)

    def delete(self):
        db.session.delete(self)
        db.session.commit()