Пример #1
0
 def execute(self):
     session = DbConnexion.session()
     if 'id' in self.param:
         schedule = session.query(Schedule).get(int(self.param['id'])) or Schedule(id=int(self.param['id']))
     else:
         schedule = Schedule(modified=datetime.now())
     if 'begin' in self.param:
         schedule.begin = self.param['begin']
     if 'end' in self.param:
         schedule.end = self.param['end']
     if 'teacher' in self.param:
         schedule.teacher = session.query(Person).filter(Person.type == 'teacher')\
             .filter(Person.id == self.param['teacher']).first()
     if 'promotions' in self.param:
         promo_ids = eval(self.param['promotions'])
         schedule.promotions = session.query(Promotion).filter(Promotion.id.in_(promo_ids)).all()
     if 'classrooms' in self.param:
         classrooms_ids = eval(self.param['classrooms'])
         schedule.classrooms = session.query(ClassRoom).filter(ClassRoom.id.in_(classrooms_ids)).all()
     if 'terminals' in self.param:
         terminal_ids = eval(self.param['terminals'])
         schedule.terminals = session.query(Terminal).filter(Terminal.id.in_(terminal_ids)).all()
     session.add(schedule)
     session.commit()
     pass
Пример #2
0
    def execute(self):
        session = DbConnexion.session()
        session.execute("DELETE FROM promotion")
        session.execute("DELETE FROM person")
        session.execute("DELETE FROM terminal")
        session.execute("DELETE FROM terminal")
        session.execute("DELETE FROM presence")
        session.execute("DELETE FROM schedule")

        promos = []
        for i in range(2013, 2016):
            promos.append(Promotion(label='RIL', academic_year=i))
            promos.append(Promotion(label='RAR', academic_year=i))
        session.add_all(promos)
        session.commit()

        persons = []
        for i in range(1, 100):
            promo = promos[random.randint(0, len(promos) - 1)]
            persons.append(Person(firstname='firstname%s' % (i,),
                                  lastname='lastname%s' % (i,),
                                  type='student',
                                  promotion=promo))
        session.add_all(persons)
        session.commit()

        teachers = []
        for i in range(100, 115):
            teachers.append(Person(firstname='firstname%s' % (i,),
                                   lastname='lastname%s' % (i,),
                                   type='teacher'))
        session.add_all(teachers)
        session.commit()
        teachers.append(None)

        classrooms = []
        for i in range(1, 21):
            classrooms.append(ClassRoom(label='Class%s' % (i,), capacity=random.randint(15, 70)))
        session.add_all(classrooms)
        session.commit()

        terminals = []
        for i in range(1, 35):
            terminals.append(Terminal(label='Terminal%s' % (i,),
                                      classroom=classrooms[randint(0, len(classrooms) - 1)],
                                      ip_address='%s.%s.%s.%s' % (randint(0, 255), randint(0, 255), randint(0, 255), randint(0, 255))))
        for i in range(35, 41):
            terminals.append(Terminal(label='Terminal%s' % (i,),
                                      ip_address='%s.%s.%s.%s' % (randint(0, 255), randint(0, 255), randint(0, 255), randint(0, 255))))
        session.add_all(terminals)
        session.commit()

        schedules = []
        today = datetime.now()
        for i in range(1, 50):
            tclassrooms = []
            tclassrooms.append(classrooms[randint(0, len(classrooms) - 1)])
            tclass2 = classrooms[randint(0, len(classrooms) - 1)]
            if tclassrooms[0].id != tclass2.id:
                tclassrooms.append(tclass2)
            tpromos = []
            tpromos.append(promos[randint(0, len(promos) - 1)])
            tpromo2 = promos[randint(0, len(promos) - 1)]
            if tpromos[0].id != tpromo2.id:
                tpromos.append(tpromo2)
            tterminal = []
            while today.weekday() == 6 or today.weekday() == 5:
                today = today + timedelta(days=1)
            s = Schedule(teacher=teachers[random.randint(1, len(teachers) - 1)])
            s.begin = datetime(today.year, today.month, today.day, 8, 30)
            s.end = datetime(today.year, today.month, today.day, 12, 30)
            s.classrooms = tclassrooms
            s.promotions = tpromos
            schedules.append(s)

            s = Schedule(teacher=s.teacher)
            s.begin = datetime(today.year, today.month, today.day, 13, 30)
            s.end = datetime(today.year, today.month, today.day, 17, 30)
            s.classrooms = tclassrooms
            s.promotions = tpromos
            schedules.append(s)
            today = today + timedelta(days=1)

        session.add_all(schedules)
        session.commit()

        pass