Exemplo n.º 1
0
class Debt(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    tipo = db.Column(db.String(100), unique=False, nullable=True)
    valor = db.Column(db.Integer, unique=False, nullable=True)
    person_id = db.Column(db.Integer,
                          db.ForeignKey('person.cpf'),
                          nullable=False)
Exemplo n.º 2
0
class VocabularyTerm(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    english = db.Column(db.VARCHAR(255), nullable=False)
    french = db.Column(db.ARRAY(db.VARCHAR(255),
                                dimensions=1,
                                zero_indexes=True),
                       nullable=False,
                       default=[])
    antonym = db.Column(db.ARRAY(db.VARCHAR(255),
                                 dimensions=1,
                                 zero_indexes=True),
                        nullable=False,
                        default=True)
    category_id = db.Column(db.Integer,
                            db.ForeignKey('vocabulary_category.id'),
                            nullable=False)
    category = db.relationship('VocabularyCategory')

    def __init__(self, english, french, antonym, category):
        self.english = english
        self.french = french
        self.antonym = antonym
        self.category = category

    def __repr__(self):
        return f'<VocabTerm {self.english}>'
Exemplo n.º 3
0
class RelacionamentoModel(db.Model):

    __tablename__ = 'relacionamentotopicos'

    id = db.Column(db.Integer, primary_key=True)
    nome = db.Column(db.String(150))
    topico1_id = db.Column(db.Integer, db.ForeignKey('topicos.id'))
    topico2_id = db.Column(db.Integer, db.ForeignKey('topicos.id'))

    topico1 = db.relationship(
        "TopicoModel",
        primaryjoin=TopicoModel.id == topico1_id,
    )
    topico2 = db.relationship(
        "TopicoModel",
        primaryjoin=TopicoModel.id == topico2_id,
    )

    def __init__(self, nome, topico1_id, topico2_id):
        self.nome = nome
        self.topico1_id = topico1_id
        self.topico2_id = topico2_id

    def json(self):
        return {
            'id': self.id,
            'nome': self.nome,
            'id_topico1': self.topico1_id,
            'id_topico2': self.topico2_id
        }

    @classmethod
    def buscar_por_nome(cls, nome):
        return cls.query.filter_by(nome=nome).first()

    @classmethod
    def buscar_por_id(cls, id):
        return cls.query.filter_by(id=id).first()

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

    def remover_no_banco(self):
        db.session.delete(self)
        db.session.commit()
Exemplo n.º 4
0
class Address(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    rua = db.Column(db.String(100), unique=True, nullable=True)
    numero = db.Column(db.Integer, unique=False, nullable=True)
    cidade = db.Column(db.String(100), unique=False, nullable=True)
    estado = db.Column(db.String(100), unique=False, nullable=True)
    person_id = db.Column(db.Integer,
                          db.ForeignKey('person.cpf'),
                          nullable=False)
class Address(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    addr = db.Column(db.String(255), nullable=False)
    employee_id = db.Column(db.Integer,
                            db.ForeignKey('employee.id'),
                            unique=True,
                            nullable=False)

    def __init__(self, addr):
        self.addr = addr
Exemplo n.º 6
0
class TopicoModel(db.Model):

    __tablename__ = 'topicos'

    id = db.Column(db.Integer, primary_key=True)
    nome = db.Column(db.String(80))
    id_disciplinas = db.Column(db.Integer, db.ForeignKey('disciplinas.id'))

    disciplina = db.relationship('DisciplinaModel')

    def __init__(self, nome, id_disciplinas):
        self.nome = nome
        self.id_disciplinas = id_disciplinas

    def json(self):
        return {
            'id': self.id,
            'nome': self.nome,
            'id_disciplina': self.id_disciplinas
        }

    @classmethod
    def buscar_por_nome(cls, nome):
        return cls.query.filter_by(nome=nome).first()

    @classmethod
    def buscar_por_id(cls, id):
        return cls.query.filter_by(id=id).first()

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

    def remover_no_banco(self):
        db.session.delete(self)
        db.session.commit()
Exemplo n.º 7
0
class Item(db.Model):
    """
    Creates new item objects so create new items for use
    throughout the API
    """

    __tablename__ = 'items'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    price = db.Column(db.Float(precision=2))
    store_id = db.Column(db.Integer, db.ForeignKey('stores.id'))
    store = db.relationship('Store')

    def __init__(self, name: str, price: float, store_id: int) -> None:
        self.name = name
        self.price = price
        self.store_id = store_id

    @classmethod
    def get_by_name(cls, name: str) -> Dict:
        return cls.query.filter_by(name=name).first()

    def as_json(self) -> Dict:
        return {'name': self.name, 'price': self.price}

    def get_all(self) -> Dict:
        return {'items': [self.as_json() for item in Item.query.all()]}

    def save(self) -> None:
        db.session.add(self)
        db.session.commit()

    def delete(self) -> None:
        db.session.delete(self)
        db.session.commit()
Exemplo n.º 8
0
class Message(db.Model):
    __tablename__ = "message"

    id = db.Column(db.Integer, primary_key=True)
    sender_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    receiver_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    message = db.Column(db.String(300))
    subject = db.Column(db.String(30))
    creation_date = db.Column(db.DateTime, default=datetime.now)
    read = db.Column(db.Boolean, default=False)

    def __init__(self, sender_id, receiver_id, message, subject):
        self.sender_id = sender_id
        self.receiver_id = receiver_id
        self.message = message
        self.subject = subject
        self.creation_date = datetime.now()
        self.read = False

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

    @classmethod
    def get_message_by_id(cls, id):
        return cls.query.filter_by(receiver_id=id).all()

    @classmethod
    def get_unread_message_by_id(cls, id):
        return cls.query.filter(and_(cls.receiver_id == id,
                                     cls.read == False)).all()

    @classmethod
    def read_message(cls, id):
        first_message = Message.query.filter(
            and_(cls.receiver_id == id, cls.read == False)).first()
        first_message.read = True
        db.session.commit()
        return first_message

    @classmethod
    def delete_message(cls, id, message_id):
        message = cls.query.filter_by(id=message_id).first()
        try:
            if id == message.receiver_id or id == message.sender_id:
                db.session.delete(message)
                db.session.commit()
                return True
        except Exception as e:
            return False
        return False

    def json(self):
        return {
            "sender_id": self.sender_id,
            "receiver_id": self.receiver_id,
            "subject": self.subject,
            "message": self.message,
            "creation_date": self.creation_date.isoformat(),
            "read": self.read
        }
Exemplo n.º 9
0
class ItemModel(db.Model):
    __tablename__ = 'items'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    item_name = db.Column(db.String(20))
    price = db.Column(db.Float(precision=2))
    created_timestamp = db.Column(db.Float)
    modify_timestamp = db.Column(db.Float)
    store_id = db.Column(db.Integer, db.ForeignKey('stores.id'))
    store = db.relationship('StoreModel')
    user_id = db.Column(db.Integer)

    def __init__(self, item_name: str, price: float, modify_timestamp: float):
        self.item_name = item_name
        self.price = price
        self.modify_timestamp = modify_timestamp

    def json(self) -> ItemJSON:
        return {
            self.id: [{
                'item_name': self.item_name,
                'price': self.price,
                'store': self.store_id,
                'last_modified': convert_timestamp(self.modify_timestamp)
            }]
        }

    def all_items_json(self) -> ALLItemJSON:
        return {
            self.id: [{
                'item_name': self.item_name,
                'price': self.price,
                'store': self.store_id,
                'hold_by_user': self.user_id,
                'last_modified': convert_timestamp(self.modify_timestamp)
            }]
        }

    @classmethod
    def find_by_name(cls, item_name: str) -> "ItemModel":
        return cls.query.filter_by(item_name=item_name).first()

    @classmethod
    def find_by_id(cls, _id: int) -> "ItemModel":
        return cls.query.filter_by(id=_id).first()

    @classmethod
    def find_cart(cls, _id: int) -> "ItemModel":
        return cls.query.filter_by(user_id=_id).all()

    @classmethod
    def find_stock(cls) -> "ItemModel":
        return cls.query.filter(cls.store_id != None,
                                cls.user_id == None).all()

    @classmethod
    def update_user_id(cls, _id: int) -> "ItemModel":
        return cls.query.filter_by(user_id=_id).update(dict(user_id=None))

        return item.all()

    @classmethod
    def sum_cart_by_user_id(cls, _id: int) -> "ItemModel":
        tot = cls.query.with_entities(func.sum(ItemModel.price)). \
            filter_by(user_id=_id).all()
        print(tot[0][0])
        if tot[0][0] is None:
            return 0
        return tot[0][0]

    @classmethod
    def count_cart_by_user_id(cls, _id: int) -> "ItemModel":
        tot = cls.query.with_entities(func.count(ItemModel.price)). \
            filter_by(user_id=_id).all()
        if tot[0][0] is None:
            return 0
        return tot[0][0]

    def save_to_db(self) -> None:
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self) -> None:
        db.session.delete(self)
        db.session.commit()
Exemplo n.º 10
0
from db.db import db
from db.db import DB_STAFF
from db.db import DB_COURSE

# db.create_all()将自动生成表student_teacher_relationship, student, teacher
student_teacher_relationship = db.Table('student_teacher_relationship',
    db.Column('student_id', db.Integer, db.ForeignKey('student.id')),
    db.Column('teacher_id', db.Integer, db.ForeignKey('teacher.id')),
    info={'bind_key': DB_STAFF}
)

class Student(db.Model):
    __bind_key__ = DB_STAFF
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(10))
    teachers = db.relationship('Teacher', secondary = student_teacher_relationship, back_populates = 'students')

class Teacher(db.Model):
    __bind_key__ = DB_STAFF
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(10))
    students = db.relationship('Student', secondary = student_teacher_relationship, back_populates = 'teachers')