class UserModel(alchemy.Model): __tablename__ = 'user' id = alchemy.Column(alchemy.Integer, primary_key=True) name = alchemy.Column(alchemy.String(80)) password = alchemy.Column(alchemy.String(80)) def __init__(self, name, password): self.name = name self.password = password def json(self): return {'name': self.name, 'password': self.password} @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() def save_to_db(self): alchemy.session.add(self) alchemy.session.commit() def delete_from_db(self): alchemy.session.delete(self) alchemy.session.commit()
class UserModel(alchemy.Model): __tablename__ = 'user' id = alchemy.Column(alchemy.Integer, primary_key=True) username = alchemy.Column(alchemy.String(100), unique=True) password = alchemy.Column(alchemy.String(94)) def __init__(self, username, password): self.username = username self.password = password def json(self): return {'username': self.username, 'password': self.password} @classmethod def find_by_username(cls, username): return cls.query.filter.by(username=username).first() @classmethod def find_by_id(cls, id): return cls.query.filter.by(id=id).first() def save_to_db(self): alchemy.session.add(self) alchemy.session.commit() def delete_from_db(self): alchemy.session.delete(self) alchemy.session.commit()
class EpisodeModel(alchemy.Model): __tablename__ = 'episode' id = alchemy.Column(alchemy.Integer, primary_key=True) name = alchemy.Column(alchemy.String(80)) season = alchemy.Column(alchemy.Float(precision=2)) show_id = alchemy.Column(alchemy.Integer, alchemy.ForeignKey('shows.id')) #episode = alchemy.relationship('show.ShowModel') def __init__(self, name, season, show_id): self.name = name self.season = season self.show_id = show_id def json(self): return {'name': self.name, 'season': self.season} @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() def save_to_db(self): alchemy.session.add(self) alchemy.session.commit() def delete_from_db(self): alchemy.session.delete(self) alchemy.session.commit()
class EpisodeModel(alchemy.Model): __tablename__ = "episode" id = alchemy.Column(alchemy.Integer, primary_key=True) name = alchemy.Column(alchemy.String(80)) season = alchemy.Column(alchemy.Integer) show_id = alchemy.Column(alchemy.Integer, alchemy.ForeignKey('shows.id')) def __init__(self, name, season, show_id): self.name = name self.season = season self.show_id = show_id def json(self): return {"name": self.name, "season": self.season} def save_to_db(self): alchemy.session.add(self) alchemy.session.commit() def delete_from_db(self): alchemy.session.delete(self) alchemy.session.commit() @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first()
class ShowModel(alchemy.Model): __tablename__ = 'show' id = alchemy.Column(alchemy.Integer, primary_key=True) name = alchemy.Column(alchemy.String(80)) episodes = alchemy.relationship(episode.EpisodeModel, lazy='dynamic') def __init__(self, name): self.name = name def json(self): return {'id': self.id, 'name': self.name, 'episode':[episode.json() for episode in self.episodes.all()]} def save_to_db(self): alchemy.session.add(self) alchemy.session.commit() @classmethod def find_by_name(cls,name): return cls.query.filter_by(name=name).first() @classmethod def find_by_id(cls, id): return cls.query.filter_by(id=id).first() def delete_from_db(self): alchemy.session.delete(self) alchemy.session.commit() def update_show(self): alchemy.session.update(self) alchemy.session.commit()
class Employee(alchemy.Model): __tablename__ = 'employee' id = alchemy.Column(alchemy.Integer, primary_key=True) name = alchemy.Column(alchemy.String(80)) email = alchemy.Column(alchemy.String(80)) department = alchemy.Column(alchemy.String(80)) salary = alchemy.Column(alchemy.Float(15, 2)) birth_date = alchemy.Column(alchemy.Date) def __init__(self, name, email, department, salary, birth_date): self.name = name self.email = email self.department = department self.salary = float(salary) self.birth_date = datetime.strptime(birth_date, '%m-%d-%Y') def json(self): return { 'id': self.id, 'name': self.name, 'email': self.email, 'department': self.department, 'salary': str(round(self.salary, 2)), 'birth_date': self.birth_date.strftime('%m-%d-%Y') } def save_to_db(self): alchemy.session.add(self) alchemy.session.commit() @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() @classmethod def find_by_id(cls, id): return cls.query.filter_by(id=id).first() def delete_from_db(self): alchemy.session.delete(self) alchemy.session.commit() def update(self): alchemy.session.commit()
class ShowModel(alchemy.Model): __tablename__ = "shows" id = alchemy.Column(alchemy.Integer, primary_key=True) name = alchemy.Column(alchemy.String(80)) #episodios - fazer algo para carregar automaticamente os episodios episodes = alchemy.relationship(episode.EpisodeModel, lazy="dynamic") def __init__(self, name): self.name = name def json(self): return { "id": self.id, "name": self.name, "episodes": [episode.json() for episode in self.episodes.all()] } def save_to_db(self): alchemy.session.add(self) alchemy.session.commit() def delete_from_db(self): alchemy.session.delete(self) alchemy.session.commit() @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() @classmethod def find_by_id(cls, id): return cls.query.filter_by(id=id).first() @classmethod def find_all(cls): return cls.query.all()