コード例 #1
0
ファイル: students.py プロジェクト: ahosha/YogaFlaskServerSQL
class StudentModel(db.Model):
    __tablename__ = 'students'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(80))
    firstname = db.Column(db.String(80))
    lastname = db.Column(db.String(80))

    abonementtypeid = db.Column(db.Integer, db.ForeignKey('abonements.id'))
    abonements = db.relationship('AbonementModel')

    #abons = db.relationship('AbonementModel', lazy='select', backref=db.backref('abonements', lazy='joined'))

    #lessonattendances = db.relationship('LesssonAttendanceModel', backref = 'students', cascade = 'all, delete-orphan', lazy = 'dynamic')
    lessonattendances = db.relationship('LesssonAttendanceModel', back_populates="lessonattendance")

    abonementstartdate = db.Column(db.String(80))
    active = db.Column(db.Integer)

    def __init__(self, username, password, firstname, lastname, abonementtypeid, abonementstartdate, active):
        self.username = username
        self.password = password
        self.firstname = firstname
        self.lastname = lastname      
        self.abonementtypeid = abonementtypeid
        self.abonementstartdate = abonementstartdate      
        self.active = active

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

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


    def json(self):
        return {'username': self.username, 'password': self.password, 
                'firstname': self.firstname, 'lastname': self.lastname, 
                'countlessonattendances': len([la for la in self.lessonattendances]),
                'lessonattendances': [la.json() for la in self.lessonattendances],
                'abonementstartdate': self.abonementstartdate, 'active': self.active }

    def jsonAbonements(self):
        return {'abonements':  self.query(StudentModel).join(StudentModel.abonementstype).all()[0].name}

    @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()
コード例 #2
0
class LesssonModel(db.Model):
    __tablename__ = 'lessons'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    teacherid = db.Column(db.Integer)
    date = db.Column(db.String(80))
    time = db.Column(db.String(80))
    lessontype = db.Column(db.String(80))
    lessonattendances = db.relationship('LesssonAttendanceModel', backref = 'lessons', cascade = 'all, delete-orphan', lazy = 'dynamic')  

    def __init__(self, name, teacherid, date, time, lessontype):
        self.name = name
        self.teacherid = teacherid
        self.date = date
        self.time = time      
        self.lessontype = lessontype

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

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

    def json(self):
        return {'name': self.name,'teacherid': self.teacherid,
                'date': self.date, 'time': self.time, 
                'countlessonattendances': len([la for la in self.lessonattendances]),
                'lessonattendances': [la.json() for la in self.lessonattendances],
                'lessontype': self.lessontype}

    @classmethod
    def find_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

    @classmethod
    def find_by_date(cls, date):
        return cls.query.filter_by(date=date).first()

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

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

    @classmethod
    def find_by_lessontype(cls, lessontype):
        return cls.query.filter_by(id=lessontype).first()
コード例 #3
0
ファイル: item.py プロジェクト: ahosha/YogaFlaskServerSQL
class ItemModel(db.Model):
    __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('StoreModel')

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

    def json(self):
        return {'name': self.name, 'price': self.price}

    @classmethod
    def find_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

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

    def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()
コード例 #4
0
ファイル: store.py プロジェクト: ahosha/YogaFlaskServerSQL
class StoreModel(db.Model):
    __tablename__ = 'stores'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))

    items = db.relationship('ItemModel', lazy='dynamic')

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

    def json(self):
        return {
            'name': self.name,
            'items': [item.json() for item in self.items.all()]
        }

    @classmethod
    def find_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

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

    def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()
コード例 #5
0
ファイル: teachers.py プロジェクト: ahosha/YogaFlaskServerSQL
class TeacherModel(db.Model):
    __tablename__ = 'teachers'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(80))
    firstname = db.Column(db.String(80))
    lastname = db.Column(db.String(80))
    location = db.Column(db.String(80))
    active = db.Column(db.Integer)

    def __init__(self, username, password, firstname, lastname, location,
                 active):
        self.username = username
        self.password = password
        self.firstname = firstname
        self.lastname = lastname
        self.location = location
        self.active = active

    def json(self):
        return {
            'username': self.username,
            'password': self.password,
            'firstname': self.firstname,
            'lastname': self.lastname,
            'location': self.location,
            'active': self.active
        }

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

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

    @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()
コード例 #6
0
ファイル: user.py プロジェクト: ahosha/YogaFlaskServerSQL
class UserModel(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80))
    password = db.Column(db.String(80))

    def __init__(self, username, password):
        self.username = username
        self.password = password

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

    @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()
コード例 #7
0
class AbonementModel(db.Model):
    __tablename__ = 'abonements'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    duration = db.Column(db.Integer)
    price = db.Column(db.Integer)
    students = db.relationship('StudentModel', lazy='dynamic')

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

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

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

    def json(self):
        return {
            'name': self.name,
            'duration': self.duration,
            'price': self.price,
            'students': [student.json() for student in self.students.all()]
        }

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

    @classmethod
    def find_by_name(cls, name):
        return cls.query.filter_by(name=name).first()