class UserRoles(db.Model): __tablename__ = 'user_roles' id = db.Column(db.Integer(), primary_key=True) user_id = db.Column(db.Integer(), db.ForeignKey('users.id', ondelete='CASCADE')) role_id = db.Column(db.Integer(), db.ForeignKey('roles.id', ondelete='CASCADE'))
class Attend(db.Model): has_attended = db.Column(db.Boolean, default=False) student_id = db.Column(db.Integer, db.ForeignKey('students.id'), primary_key=True) lesson_id = db.Column(db.Integer, db.ForeignKey('lesson.id'), primary_key=True) student = db.relationship('Students', backref='attend') lesson = db.relationship('Lesson', backref='attend') def attend(self): self.has_attended = True if not self.has_attended else False db.session.add(self) db.session.commit()
class Instrument(db.Model): __tablename__ = 'instruments' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60)) type = db.Column(db.String(60)) inventory_number = db.Column(db.String(60)) img_path = db.Column(db.String(120)) cant = 0 def __repr__(self): return '<Instrumento: %r>' % self.name @classmethod def set_inventory_number(cls, type): year = datetime.datetime.now().year month = datetime.datetime.now().month cls.cant += 1 c = cls.cant return type + str(year) + str(month) + str(c) @classmethod def create(cls, form, path): name = form.name.data type = form.type.data inventory_number = cls.set_inventory_number(type) img_path = path instance = cls(name=name, type=type, inventory_number=inventory_number, img_path=img_path) db.session.add(instance) try: db.session.commit() except: db.session.rollback() return instance @classmethod def delete(cls, instrument_id): instrument = cls.query.filter_by(id=instrument_id).first_or_404() os.remove('%s/flaskps' % BASE_DIR + instrument.img_path) db.session.delete(instrument) db.session.commit() def update(self, form, path): self.name = form.name.data self.type = form.type.data self.img_path = path db.session.commit()
class Rol(db.Model): """ Create a Role table """ __tablename__ = 'roles' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(60), unique=True, nullable=False) permisos = db.Column(db.String(60000), nullable=False) def __init__(self, name, permisos): super().__init__ self.name = name self.permisos = ','.join(permisos) def __repr__(self): return '<Rol: {}>'.format(self.name)
class Teachers(db.Model): id = db.Column(db.Integer, primary_key=True) surname = db.Column(db.String(60)) name = db.Column(db.String(60)) birth_date = db.Column(db.Date()) locality = db.Column(db.String(60)) address = db.Column(db.String(60)) document_type = db.Column(db.String(60)) document_number = db.Column(db.String(60)) phone = db.Column(db.String(60)) def __repr__(self): return '<Docente: %r>' % self.name @classmethod def delete(cls, teacher_id): teacher = Teachers.query.filter_by(id=teacher_id).first_or_404() db.session.delete(teacher) db.session.commit() @classmethod def create(cls, form): teacher = Teachers( name=form.name.data, surname=form.surname.data, birth_date=form.birth_date.data, locality=form.locality.data, address=form.address.data, document_type=form.document_type.data, document_number=form.document_number.data, phone=form.phone.data, ) db.session.add(teacher) try: db.session.commit() except: db.session.rollback() return teacher def update(self, form): self.name = form.name.data self.surname = form.surname.data self.birth_date = form.birth_date.data self.locality = form.locality.data self.address = form.address.data self.document_type = form.document_type.data self.document_number = form.document_number.data self.phone = form.phone.data db.session.commit()
class SchoolYear(db.Model): id = db.Column(db.Integer, primary_key=True) start_date = db.Column(db.Date, default=get_today(), unique=True) end_date = db.Column(db.Date, default=get_today()) semesters = db.Column(ChoiceType(SCHOOL_YEAR_CHOICES)) workshops = db.relationship( 'Workshop', backref='semester', lazy=True) active = db.Column(db.Boolean, default=True) @classmethod def create(cls, form): start_date = form.start_date.data end_date = form.end_date.data semesters = form.semester.data instance = cls( start_date=start_date, end_date=end_date, semesters=semesters ) db.session.add(instance) try: db.session.commit() except: db.session.rollback() return instance def add_workshop(self, workshops): for workshop in workshops: self.workshops.append(workshops) def delete(self): self.active = False db.session.commit() def update(self, form): self.start_date = form.start_date.data self.end_date = form.end_date.data self.semesters = form.semester.data db.session.commit()
class Configurations(db.Model): """ Crear modelo de configuariones """ __tablename__ = 'configurations' id = db.Column(db.Integer, primary_key=True) active = db.Column(db.Boolean, default=True) description = db.Column(db.String(200)) title = db.Column(db.String(60), index=True) email = db.Column(db.String(60), index=True) offset_paginator = db.Column(db.Integer, default=20) def update(self,data): offset = data.get('offset') active = data.get('active') title = data.get('title') description = data.get('description') email = data.get('email') if title: self.title = title if description: self.description = description if email: self.email = email if offset: self.offset_paginator = int(offset) self.active = True if active else False db.session.commit()
class Person(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) addresses = db.relationship('Address', backref='person', lazy=True)
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True)
class Page(db.Model): id = db.Column(db.Integer, primary_key=True) tags = db.relationship('Tag', secondary=tags, lazy='subquery', backref=db.backref('pages', lazy=True))
self.workshops.append(workshops) def delete(self): self.active = False db.session.commit() def update(self, form): self.start_date = form.start_date.data self.end_date = form.end_date.data self.semesters = form.semester.data db.session.commit() workshop_students = db.Table('workshop_students', db.Column( 'workshop_id', db.Integer, db.ForeignKey('workshop.id'), primary_key=True), db.Column( 'student_id', db.Integer, db.ForeignKey('students.id'), primary_key=True), ) class Workshop(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120)) short_name = db.Column(db.String(60)) semester_id = db.Column( db.Integer, db.ForeignKey('school_year.id'), nullable=False) teacher_id = db.Column( db.Integer, db.ForeignKey('teachers.id'), nullable=True) students = db.relationship( 'Students',
name = db.Column(db.String(50), nullable=False) addresses = db.relationship('Address', backref='person', lazy=True) class Address(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), nullable=False) person_id = db.Column(db.Integer, db.ForeignKey('person.id'), nullable=False) # Ejemplo many-to-many tags = db.Table( 'tags', db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True), db.Column('page_id', db.Integer, db.ForeignKey('page.id'), primary_key=True)) class Page(db.Model): id = db.Column(db.Integer, primary_key=True) tags = db.relationship('Tag', secondary=tags, lazy='subquery', backref=db.backref('pages', lazy=True)) class Tag(db.Model):
class TimeStampedModel(): created_at = db.Column(db.Date, default=get_today()) updated_at = db.Column(db.Date, onupdate=get_today())
class Workshop(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120)) short_name = db.Column(db.String(60)) semester_id = db.Column( db.Integer, db.ForeignKey('school_year.id'), nullable=False) teacher_id = db.Column( db.Integer, db.ForeignKey('teachers.id'), nullable=True) students = db.relationship( 'Students', secondary=workshop_students, lazy='subquery', backref=db.backref('workshops', lazy=True) ) cant_lessons = db.Column(db.Integer, default=0) lessons = db.relationship('Lesson', backref='workshop', lazy=True) nucleo = db.Column(db.String(120)) address = db.Column(db.String(214)) days = db.Column(db.String(520)) horario = db.Column(db.String(20)) @classmethod def create(cls, form, sy): name = form.name.data short_name = form.short_name.data teacher_id = form.teacher.data nucleo = form.nucleo.data address = form.address.data days = form.days.data horario = form.horario.data cant = form.clases.data instance = cls( name=name, short_name=short_name, teacher_id=teacher_id, semester_id=sy.id, cant_lessons=cant, nucleo=nucleo, address=address, days=days, horario=horario, ) n = 1 for lesson in range(cant+1): l = Lesson(number=n, workshop_id=instance.id) n += 1 instance.lessons.append(l) db.session.add(instance) try: db.session.commit() except: db.session.rollback() return instance def update(self, form): pass def add_student(self, student): self.students.append(student) for lesson in self.lessons: attend = Attend(lesson=lesson, student=student) db.session.add(attend) db.session.add(self) try: db.session.commit() except: db.session.rollback()
class Lesson(db.Model): id = db.Column(db.Integer, primary_key=True) number = db.Column(db.Integer) workshop_id = db.Column(db.Integer, db.ForeignKey('workshop.id'), nullable=False)
class Nucleos(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60)) longitud = db.Column(db.Float) latitude = db.Column(db.Float)
class User(db.Model, TimeStampedModel, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) is_admin = db.Column(db.Boolean, default=False) username = db.Column(db.String(60), unique=True, nullable=False) email = db.Column(db.String(60), unique=True, nullable=False) name = db.Column(db.String(60)) surname = db.Column(db.String(60)) active = db.Column(db.Boolean, default=True) password_hash = db.Column(db.String(128)) # Define the relationship to Role via UserRoles roles = db.relationship('Rol', secondary='user_roles') def __repr__(self): return '<Usuario: %r>' % self.username @classmethod def create(cls, form): username = form.username.data name = form.name.data surname = form.surname.data email = form.email.data password = form.password.data roles = form.roles.raw_data roles = Rol.query.filter(Rol.name.in_(roles)).all() instance = cls( name=name, surname=surname, username=username, email=email, password=password, roles=roles, ) db.session.add(instance) try: db.session.commit() except: db.session.rollback() return instance def update(self, form): self.name = form.name.data self.surname = form.surname.data self.username = form.username.data self.email = form.email.data self.active = form.active.data roles = form.roles.raw_data roles = Rol.query.filter(Rol.name.in_(roles)).all() self.roles = roles if not self.verify_password(form.password.data): self.password = form.password.data db.session.commit() @classmethod def delete(cls, user_id): user = User.query.filter_by(id=user_id).first_or_404() db.session.delete(user) db.session.commit() def have_permissions(self, permission): perm = [] for rol in self.roles: perm += rol.permisos.split(',') if permission in perm: return True return False @property def is_active(self): return self.active @property def password(self): """ Impide que se pueda leer la clave de usuario """ raise AttributeError('password: no es un atributo de lectura. :D') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password)
class Address(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), nullable=False) person_id = db.Column(db.Integer, db.ForeignKey('person.id'), nullable=False)