class Gender(db.Model): __tablename__ = 'genero' id = db.Column(db.Integer, primary_key=True) name = db.Column('nombre', db.String(255), nullable=False) students = db.relationship('Student', backref='gender', lazy=True) teachers = db.relationship('Teacher', backref='gender', lazy=True) responsables = db.relationship('Responsable', backref='gender', lazy=True)
class Nucleus(db.Model): __tablename__ = 'nucleo' id = db.Column(db.Integer, primary_key=True) name = db.Column('nombre', db.String(255), nullable=False) address = db.Column('direccion', db.String(255), nullable=True) telephone = db.Column('telefono', db.String(255), nullable=True) assistances = db.relationship('AssistanceStudentWorkshop', backref='nucleus', lazy=True) courses = db.relationship('TeacherNucleus', backref='nucleus', lazy=True)
class Role(db.Model): __tablename__ = 'role' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), unique=True, nullable=False) permissions = db.relationship('Permission', secondary=role_permission, lazy='subquery', backref=db.backref('roles', lazy=True)) def __repr__(self): return '<Role %r>' % self.name @classmethod def get_by_name(cls, rolname): return cls.query.filter_by(name=rolname).first() @classmethod def get_list_by_name(cls, rolnames): roles = map(lambda rolname: Role.get_by_name(rolname), rolnames) return list(roles) def has_permission(self, permission_name): permissions = map(lambda p: p.name == permission_name, self.permissions) return any(permissions)
class Workshop(db.Model): __tablename__ = 'taller' id = db.Column( db.Integer, primary_key=True ) name = db.Column( 'nombre', db.String(255), nullable=False ) short_name = db.Column( 'nombre_corto', db.String(255), nullable=False ) assistances = db.relationship( 'AssistanceStudentWorkshop', backref='workshop', lazy=True )
class Neighborhood(db.Model): __tablename__ = 'barrio' id = db.Column(db.Integer, primary_key=True) name = db.Column('nombre', db.String(255), nullable=False) students = db.relationship('Student', backref='neighborhood', lazy=True)
class Level(db.Model): __tablename__ = 'nivel' id = db.Column(db.Integer, primary_key=True) name = db.Column('nombre', db.String(255), nullable=False) students = db.relationship('Student', backref='level', lazy=True)
class InstrumentType(db.Model): __tablename__ = 'tipo_instrumento' id = db.Column(db.Integer, primary_key=True) name = db.Column('nombre', db.String(255), nullable=False) instruments = db.relationship('Instrument', backref='category', lazy=True) @classmethod def any(cls, category_id): return any(cls.query.filter(cls.id == category_id))
class School(db.Model): __tablename__ = 'escuela' id = db.Column(db.Integer, primary_key=True) name = db.Column('nombre', db.String(255), nullable=False) address = db.Column('direccion', db.String(255), nullable=True) telephone = db.Column('telefono', db.String(255), nullable=True) students = db.relationship('Student', backref='school', lazy=True)
class Day(db.Model): __tablename__ = 'dia' id = db.Column(db.Integer, primary_key=True) name = db.Column('nombre', db.String(15), unique=True, nullable=False) number = db.Column('numero', db.Integer, unique=True, nullable=False) teacher_nucleus = db.relationship('TeacherNucleus', backref='day', lazy=True)
class Teacher(db.Model): __tablename__ = 'docente' id = db.Column(db.Integer, primary_key=True) last_name = db.Column('apellido', db.String(255), nullable=False) first_name = db.Column('nombre', db.String(255), nullable=False) birth_date = db.Column('fecha_nac', db.DateTime, nullable=False) location_id = db.Column('localidad_id', db.Integer, nullable=False) residency = db.Column('domicilio', db.String(255), nullable=False) latitude = db.Column('latitud', db.String(255), nullable=False) longitude = db.Column('longitud', db.String(255), nullable=False) gender_id = db.Column('genero_id', db.Integer, db.ForeignKey('genero.id'), nullable=False) doc_type_id = db.Column('tipo_doc_id', db.Integer, nullable=False) doc_number = db.Column('numero', db.Integer, nullable=False) telephone = db.Column('tel', db.String(255), nullable=False) is_active = db.Column(db.Boolean, nullable=False, default=True) school_years = db.relationship('SchoolYear', secondary=school_year_workshop_teacher, lazy='subquery', backref=db.backref('teachers', lazy=True)) workshops = db.relationship('Workshop', secondary=school_year_workshop_teacher, lazy='subquery', backref=db.backref('teachers', lazy=True)) def __init__(self, data): self.__init_attributes(data) def __init_attributes(self, data): self.last_name = data['last_name'] self.first_name = data['first_name'] self.birth_date = data['birth_date'] self.location_id = data['location_id'] self.residency = data['residency'] self.gender_id = data['gender_id'] self.doc_type_id = data['doc_type_id'] self.doc_number = data['doc_number'] self.telephone = data['telephone'] self.latitude = data['latitude'] self.longitude = data['longitude'] def activate(self): self.is_active = True db.session.commit() return self def deactivate(self): self.is_active = False db.session.commit() return self @classmethod def create(cls, data): teacher = cls(data) db.session.add(teacher) db.session.commit() def update(self, values): self.__init_attributes(values) db.session.commit() def get_workshops_of_cicle(self, cicle_id): return Workshop.query.join(school_year_workshop_teacher).\ filter_by(docente_id=self.id, ciclo_lectivo_id=cicle_id) def get_workshops(self): return Workshop.query.join(school_year_workshop_teacher).\ filter_by(docente_id=self.id) def assign_to(self, form_workshops, form_cicle): for whp in form_workshops: statement = school_year_workshop_teacher.insert().values( docente_id=self.id, ciclo_lectivo_id=form_cicle, taller_id=whp) db.session.execute(statement) db.session.commit() def get_days_of_cicle_whp_nucleus(self, cicle_id, whp_id, nucleus_id): return Day.query.join(TeacherNucleus).\ filter_by(teacher_id=self.id, schoolyear_id=cicle_id, workshop_id=whp_id, nucleus_id=nucleus_id) # se supone que esto va a asignar los nucleos, algun dia .... def assign_to_nucleus(self, form_cicle, form_whp, form_nucleus, week_day): statement = TeacherNucleus.create(self.id, form_nucleus, form_cicle, form_whp, week_day)
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True, nullable=False) username = db.Column(db.String(255), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) first_name = db.Column(db.String(20), nullable=False) last_name = db.Column(db.String(30), nullable=False) is_authenticated = db.Column(db.Boolean, nullable=False, default=True) is_active = db.Column(db.Boolean, nullable=False, default=True) is_anonymous = db.Column(db.Boolean, nullable=False, default=False) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now()) created_at = db.Column(db.DateTime, nullable=False, default=datetime.now()) roles = db.relationship('Role', secondary=user_role, lazy='subquery', backref=db.backref('users', lazy=True)) def __init__(self, data): self.__init_attributes(data) self.__init_hashes(data) self.__init_relationships(data) def __init_attributes(self, data): self.username = data['username'] self.email = data['email'] self.first_name = data['first_name'] self.last_name = data['last_name'] def __init_hashes(self, data): self.password = bc.generate_password_hash(data['password']) def __init_relationships(self, data): self.roles = data['roles'] @classmethod def create(cls, data): user = cls(data) db.session.add(user) db.session.commit() @classmethod def exist_username(cls, username): user = cls.query.filter_by(username=username).first() return bool(user) @classmethod def exist_email(cls, email): user = cls.query.filter_by(email=email).first() return bool(user) def __repr__(self): return '<User %r>' % self.username def validate_pass(self, candidate_pass): return bc.check_password_hash(self.password, candidate_pass) def get_id(self): return str(self.id) def activate(self): self.is_active = True db.session.commit() return self def deactivate(self): self.is_active = False db.session.commit() return self def update(self, values): self.__init_attributes(values) self.__init_relationships(values) db.session.commit() def has_permission(self, permission_name): permissions = map(lambda rol: rol.has_permission(permission_name), self.roles) return any(permissions) def permissions(self): roles_permissions = map(lambda rol: rol.permissions, self.roles) flat_permissions = itertools.chain(*roles_permissions) return set(flat_permissions)
class Student(db.Model): __tablename__ = 'estudiante' id = db.Column(db.Integer, primary_key=True) last_name = db.Column('apellido', db.String(30), nullable=False) first_name = db.Column('nombre', db.String(20), nullable=False) birth_date = db.Column('fecha_nac', db.DateTime, nullable=False) birth_location = db.Column('lugar_nac', db.String(255), nullable=False) location_id = db.Column('localidad_id', db.Integer, nullable=False) residency = db.Column('domicilio', db.String(255), nullable=False) latitude = db.Column('latitud', db.String(255), nullable=False) longitude = db.Column('longitud', db.String(255), nullable=False) neighborhood_id = db.Column('barrio_id', db.Integer, db.ForeignKey('barrio.id'), nullable=False) gender_id = db.Column('genero_id', db.Integer, db.ForeignKey('genero.id'), nullable=False) doc_type_id = db.Column('tipo_doc_id', db.Integer, nullable=False) doc_number = db.Column('numero', db.Integer, nullable=False) telephone = db.Column('tel', db.String(255), nullable=False) school_id = db.Column('escuela_id', db.Integer, db.ForeignKey('escuela.id'), nullable=False) level_id = db.Column('nivel_id', db.Integer, db.ForeignKey('nivel.id'), nullable=False) is_active = db.Column('activo', db.Boolean, nullable=False, default=True) workshops_school_years = db.relationship('StudentWorkshop', lazy=True, backref=db.backref('students')) responsables = db.relationship('Responsable', secondary=responsable_student, lazy='subquery', backref=db.backref('students', lazy=True)) def __init__(self, data): self.__init_attributes(data) self.__init_relationships(data) def __init_attributes(self, data): self.last_name = data['last_name'] self.first_name = data['first_name'] self.birth_date = data['birth_date'] self.location_id = data['location_id'] self.residency = data['residency'] self.gender_id = data['gender_id'] self.doc_type_id = data['doc_type_id'] self.doc_number = data['doc_number'] self.telephone = data['telephone'] self.gender_id = data['gender_id'] self.school_id = data['school_id'] self.neighborhood_id = data['neighborhood_id'] self.birth_location = data['birth_location'] self.level_id = data['level_id'] self.latitude = data['latitude'] self.longitude = data['longitude'] def __init_relationships(self, data): self.responsables = self.__responsables_by_id(data['responsables_id']) def __responsables_by_id(self, responsables_id): return [Responsable.query.get(r_id) for r_id in responsables_id] def __repr__(self): return f'<Student {self.first_name}, {self.last_name}>' def activate(self): self.is_active = True db.session.commit() def deactivate(self): self.is_active = False db.session.commit() def get_workshops_of_cicle(self, cicle_id): return Workshop.query.join(StudentWorkshop).\ filter_by(student_id=self.id, schoolyear_id=cicle_id) @classmethod def create(cls, data): student = cls(data) db.session.add(student) db.session.commit() def update(self, values): self.__init_attributes(values) db.session.commit() def more_responsables_active_that(self, responsable): return any( map(lambda r: r.is_active, self.other_responsables(responsable))) def other_responsables(self, responsable): return filter(lambda r: r.id != responsable.id, self.responsables) def add_responsable(self, responsable): self.responsables.append(responsable) db.session.commit() def add_course(self, cicle_id, workshops_id): for workshop_id in workshops_id: StudentWorkshop.create(self.id, cicle_id, workshop_id) def has_responsable(self, responsable): return responsable in self.responsables
class SchoolYear(db.Model): __tablename__ = 'ciclo_lectivo' id = db.Column(db.Integer, primary_key=True) start_date = db.Column('fecha_inicio', db.Date, nullable=False) finish_date = db.Column('fecha_fin', db.Date, nullable=False) semester = db.Column('semestre', db.Integer, nullable=False) workshops = db.relationship('Workshop', secondary=school_year_workshop, lazy='subquery', backref=db.backref('school_years', lazy=True)) assistances = db.relationship('AssistanceStudentWorkshop', backref='schoolyear', lazy=True) courses = db.relationship('TeacherNucleus', backref='schoolyear', lazy=True) def __init__(self, data): self.__init_attributes(data) def __init_attributes(self, data): self.start_date = data['starting_date'] self.finish_date = data['ending_date'] self.semester = data['semester_id'] @classmethod def create(self, data): school_year = self(data) db.session.add(school_year) db.session.commit() def add_workshop(self, workshop): self.workshops.append(workshop) db.session.commit() def has_workshop(self, workshop): return workshop in self.workshops def assign_workshops(self, form_workshops): for whp in form_workshops: statement = school_year_workshop.insert().values( ciclo_lectivo_id=self.id, taller_id=whp) db.session.execute(statement) db.session.commit() @classmethod def get_current_schoolyear(cls): today = datetime.now() schoolyear = cls.query.filter(cls.finish_date >= today, cls.start_date <= today).first() return schoolyear def get_remaining_workshops(self): remaining_workshops = [] for workshop in self.workshops: # Chequeo si no se pasó asistencia todavía el día de hoy y además # si corresponde que hoy se pase lista en ese taller (se dan en días particulares) if not AssistanceStudentWorkshop.assistance_already_taken( self.id, workshop.id): remaining_workshops.append(workshop) return remaining_workshops def assistance_dates(self, workshop_id, nucleus_id): assistance_dates = list( filter( lambda date: date.weekday() in self.course_weekdays( workshop_id, nucleus_id), self.dates_without_assistance(workshop_id, nucleus_id))) return sorted(assistance_dates) def course_weekdays(self, workshop_id, nucleus_id): return list( map(lambda course: course.day.number, self.workshop_courses(workshop_id, nucleus_id))) def workshop_courses(self, workshop_id, nucleus_id): return filter(lambda course: course.workshop_id == int(workshop_id), self.nucleus_courses(nucleus_id)) def nucleus_courses(self, nucleus_id): return filter(lambda course: course.nucleus_id == int(nucleus_id), self.courses) def dates_without_assistance(self, workshop_id, nucleus_id): total_dates = set(self.range_dates()) assistance_dates = set( self.dates_workshop_assistances(workshop_id, nucleus_id)) return total_dates - assistance_dates def dates_workshop_assistances(self, workshop_id, nucleus_id): return map(lambda assistance: assistance.date, self.workshop_assistances(workshop_id, nucleus_id)) def workshop_assistances(self, workshop_id, nucleus_id): return filter( lambda assistance: assistance.workshop_id == int(workshop_id), self.nucleus_assistances(nucleus_id)) def nucleus_assistances(self, nucleus_id): return filter( lambda assistance: assistance.nucleus_id == int(nucleus_id), self.assistances) def range_dates(self): delta = self.finish_date - self.start_date total_days = delta.days + 1 return [self.start_date + timedelta(days=i) for i in range(total_days)] def is_valid_assistance_date(self, a_date, workshop_id, schoolyear_id): return a_date in self.assistance_dates(workshop_id, schoolyear_id)