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
 def execute(self):
     session = DbConnexion.session()
     if self.terminal is None:
         self.terminal = session.query(Terminal).filter_by(mac_address=self.mac_address).first()
         if self.terminal is None:
             exist = False
             self.terminal = Terminal(label='New Terminal', mac_address=self.mac_address, ip_address=self.ip_address)
         else:
             exist = True
             self.terminal.ip_address = self.ip_address
     else:
         old_terminal = session.query(Terminal).filter_by(mac_address=self.terminal.mac_address).first()
         if old_terminal is None:
             exist = False
         else:
             exist = True
             self.terminal.id = old_terminal.id
     # Attention s'il a une classroom il faut qu'elle exist
     if self.label is not None:
         self.terminal.label = self.label
     if not exist:
         session.add(self.terminal)
     session.commit()
     return self.terminal
     pass
 def execute(self):
     session = DbConnexion.session()
     stm = session.query(Promotion)
     if 'id' in self.param:
         return stm.get(self.param['id'])
     if 'label' in self.param:
         stm = stm.filter(Promotion.label.like(self.param['label']))
     if 'academic_year' in self.param:
         stm = stm.filter(Promotion.academic_year == self.param['academic_year'])
     return stm.all()
     pass
    def execute(self):
        session = DbConnexion.session()
        if self.date_t is None:
            self.date_t = datetime.now()
        # recherche de la personne par son uuid
        print "recherche la personne dont le QR code est", self.person_uuid
        person = session.query(Person).filter(Person.uuid == self.person_uuid).first()
        if person is None:
            return None
        # recherche le terminal par sa mac_address
        mac_address = hex(get_mac())[2:-1].upper().rjust(12, '0')
        terminal = session.query(Terminal).filter(Terminal.mac_address == mac_address).first()

        # recherche les schedules par la person et la date (sans l'heure)
        if person.type == 'student':
            schedule = session.query(Schedule).join(Schedule.promotions)\
                .filter(Schedule.begin <= self.date_t)\
                .filter(Schedule.end > self.date_t)\
                .filter(Promotion.id == person.promotion_id)\
                .first()
        elif person.type == 'teacher':
            schedule = session.query(Schedule)\
                .filter(Schedule.begin <= self.date_t)\
                .filter(Schedule.end > self.date_t)\
                .filter(Schedule.teacher_id == person.id)\
                .first()
        else:
            schedule = None
        if schedule is None:
            # la person n'a aucun schedule enregistre à la date défini
            print('Pas de planing à cette instant')
            return None

        # vérifier si le terminal courant fait bien partie des terminaux du schedule
        if terminal is not None:
            if not (terminal in schedule.terminals or terminal.classroom in schedule.classrooms):
                # la person s'est trompé de salle
                print('Trompé de salle ??')
                # TODO enregistrer la presence à ce terminal ??
                # -> oui pourai être utils en cas de modif de salle tardif
                # return 2

        presence = Presence(date_t=self.date_t, person=person, terminal=terminal, schedule=schedule)
        # insert if not exist selon PK (schedule+person)
        old_schedule = session.query(Presence)\
            .filter(Presence.person_id == person.id)\
            .filter(Presence.schedule_id == schedule.id)\
            .first()
        if old_schedule is None:
            session.add(presence)
            session.commit()
        return presence
        pass
Beispiel #5
0
    def put(self):
        print('PUT Person')
        args = json.loads(request.data)

        if args['firstname'] is not None:
            session = DbConnexion.session()
            persone = Person(firstname=args['firstname'], lastname=args['lastname'], phone_number=args['phone_number'],
                             email=args['email'], type=args['type'], promotion_id=args['promotion_id'])
            session.add(persone)
            session.commit()
        return "Succes", 200
        pass
 def execute(self):
     session = DbConnexion.session()
     stm = session.query(ClassRoom)
     if 'id' in self.param:
         return stm.get(self.param['id'])
     if 'label' in self.param:
         stm = stm.filter(ClassRoom.label.like(self.param['label']))
     if 'capacity_gt' in self.param:
         stm = stm.filter(ClassRoom.capacity >= self.param['capacity_gt'])
     if 'capacity_lt' in self.param:
         stm = stm.filter(ClassRoom.capacity <= self.param['capacity_lt'])
     return stm.all()
     pass
 def execute(self):
     session = DbConnexion.session()
     stm = session.query(Presence)
     if "date" in self.param:
         date = self.param["date"]
         stm = stm.filter(Presence.date_t.startswith(date))
     elif "begin" in self.param and "end" in self.param:
         begin = self.param["begin"]
         end = self.param["end"]
         stm = stm.filter(Presence.date_t.between(begin, end))
     if "person" in self.param:
         stm = stm.filter(Presence.person_id == int(self.param["person"]))
     if "schedule" in self.param:
         stm = stm.filter(Presence.schedule_id == int(self.param["schedule"]))
     if "promotion" in self.param:
         stm = stm.join(Person).filter_by(promotion_id=int(self.param["promotion"]))
     return stm.all()
     pass
    def execute(self):
        session = DbConnexion.session()
        stm = session.query(Person)
        if 'id' in self.param:
            return stm.get(self.param['id'])
        if 'uuid' in self.param:
            return stm.filter(Person.uuid == self.param['uuid']).first()
        if 'firstname' in self.param:
            stm = stm.filter(Person.firstname.like(self.param['firstname']))
        if 'lastname' in self.param:
            stm.filter(Person.lastname.like(self.param['lastname']))
        # if self.phone_number is not None:
        #     stm.filter(Person.phone_number.like(str(self.phone_number)))
        # if self.email is not None:
        #     stm.filter(Person.email.like(str(self.email)))
        # if self.type is not None:
        #     stm.filter(Person.type == str(self.type))
        # if self.promotion_id is not None:
        #     stm.filter(Person.promotion_id == int(self.promotion_id))

        return stm.all()
        pass
    def execute(self):
        session = DbConnexion.session()
        stm = session.query(Schedule)
        if 'id' in self.param:
            return stm.get(self.param['id'])
        if 'date' in self.param:
            date = self.param['date']
            print "filtrer par date", type(date), date
            stm = stm.filter(Schedule.begin.startswith(date)).filter(Schedule.end.startswith(date))
        elif 'datetime' in self.param:
            date = self.param['datetime']
            print "filtrer par datetime", date
            stm = stm.filter(Schedule.begin < date).filter(Schedule.end > date)
        elif 'begin' in self.param and 'end' in self.param:
            begin = self.param['begin']
            end = self.param['end']
            stm = stm.filter(or_(and_(Schedule.begin >= begin,
                                      Schedule.end < begin),
                                 and_(Schedule.begin >= end,
                                      Schedule.end < end)))
        if 'teacher' in self.param:
            print "filtrer par teacher", self.param['teacher']
            stm = stm.filter(Schedule.teacher_id.__eq__(self.param['teacher']))
        if 'promotion' in self.param:
            print "filtrer par promotion", self.param['promotion']
            stm = stm.join(Schedule.promotions).filter_by(id=self.param['promotion'])
        if 'terminal' in self.param:
            # FIXME : if terminal has classroom attribut filter by its classroom
            print "filtrer par terminal", self.param['terminal']
            stm = stm.join(Schedule.terminals).filter_by(id=self.param['terminal'])
        if 'classroom' in self.param:
            print "filtrer par classroom", self.param['classroom']
            stm = stm.join(Schedule.classrooms).filter_by(id=self.param['classroom'])

        schedule_list = stm.all()
        return schedule_list
        pass
    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