Example #1
0
    def fetch_rooms(self, connection, room_name=None):
        self._logger.debug("Fetching room information...")

        counter = Counter()
        foundation_rooms = []

        coordinates = self.fetch_buildings_coordinates(connection)
        cursor = connection.cursor()

        if room_name:
            cursor.execute(
                'SELECT * FROM foundation_pub.meeting_rooms WHERE ID = :room_name',
                room_name=room_name)
        else:
            cursor.execute(
                'SELECT * FROM foundation_pub.meeting_rooms ORDER BY ID')

        for row in cursor:
            counter['found'] += 1
            data = self._prepare_row(row, cursor)
            room_id = data['ID']

            try:
                room_data, email_warning = self._parse_room_data(
                    data, coordinates, room_id)
                manager_group = data.get('EMAIL_LIST')
                self._logger.debug("Fetched data for room with id='%s'",
                                   room_id)
            except SkipRoom as e:
                counter['skipped'] += 1
                self._logger.info("Skipped room %s: %s", room_id, e)
                continue

            room = Room.query.filter_by(building=room_data['building'],
                                        floor=room_data['floor'],
                                        number=room_data['number'],
                                        location=self._location).first()

            if room_data['owner'] is None:
                del room_data['owner']
                if room is None:
                    counter['skipped'] += 1
                    self._logger.info("Skipped room %s: %s", room_id,
                                      email_warning[0] % email_warning[1:])
                    continue
                elif not room.is_deleted:
                    self._logger.warning(*email_warning)

            # Insert new room
            if room is None:
                room = Room()
                self._location.rooms.append(room)
                counter['inserted'] += 1
                self._logger.info("Created new room '%s'", room_id)
            else:
                counter['updated'] += 1

            # Update room data
            self._update_room(room, room_data)
            new_managers = set()
            if manager_group is not None:
                group = GroupProxy(manager_group.strip(), provider='cern-ldap')
                if group.group is None:
                    self._logger.warning("Group '%s' does not exist in LDAP",
                                         manager_group)
                new_managers.add(group)
            current_managers = room.get_manager_list()
            for principal in current_managers - new_managers:
                room.update_principal(principal, full_access=False)
            for principal in new_managers - current_managers:
                room.update_principal(principal, full_access=True)

            self._logger.info("Updated room '%s' information", room_id)
            foundation_rooms.append(room)

        # Deactivate rooms not found in Foundation
        indico_rooms = Room.find(
            Room.name == room_name) if room_name else Room.find(
                location=self._location)
        rooms_to_deactivate = (
            room for room in indico_rooms
            if room not in foundation_rooms and not room.is_deleted)
        for room in rooms_to_deactivate:
            self._logger.info("Deactivated room '%s'", room.full_name)
            room.is_deleted = True
            counter['deactivated'] += 1
        self._logger.info("Deactivated %d rooms not found in Foundation",
                          counter['deactivated'])

        db.session.commit()
        self._logger.info(
            "Rooms summary: %d in Foundation - %d skipped - %d inserted - %d updated - %d deactivated",
            counter['found'], counter['skipped'], counter['inserted'],
            counter['updated'], counter['deactivated'])