예제 #1
0
 def _make_verbose(self, iterator, total):
     return verbose_iterator(
         iterator,
         total,
         lambda entry: entry[0].id,
         lambda entry: re.sub(
             r'\s+', ' ', strip_control_chars(getattr(
                 entry[0], 'title', ''))),
         print_total_time=True)
예제 #2
0
 def _sendEventRequest(self, key, eventType, avatar, conference):
     try:
         logger = self.getLogger()
         plugin = PluginsHolder().getPluginType('calendaring').getPlugin('outlook')
         if not isUserPluginEnabled(avatar.getId()):
             logger.info("Outlook plugin disabled for user: {}".format(avatar.getId()))
             return {'status_code': 200}
         if eventType in ['added', 'updated']:
             logger.debug("Performing '{}' for: {}".format(eventType, avatar.getId()))
             url = urlHandlers.UHConferenceDisplay.getURL(conference)
             location = strip_control_chars(conference.getRoom().getName()) if conference.getRoom() else ''
             description = strip_control_chars(conference.getDescription())
             self.payload = {'userEmail': avatar.getEmail(),
                             'uniqueID': plugin.getOption('prefix').getValue() + key,
                             'subject': strip_control_chars(conference.getTitle()),
                             'location': location,
                             'body': '<a href="{}">{}</a><br><br>{}'.format(url, url, description),
                             'status': plugin.getOption('status').getValue(),
                             'startDate': format_datetime(conference.getStartDate(),
                                                          format=plugin.getOption('datetimeFormat').getValue(),
                                                          timezone=pytz.utc),
                             'endDate': format_datetime(conference.getEndDate(),
                                                        format=plugin.getOption('datetimeFormat').getValue(),
                                                        timezone=pytz.utc),
                             'isThereReminder': plugin.getOption('reminder').getValue(),
                             'reminderTimeInMinutes': plugin.getOption('reminder_minutes').getValue()}
             operation = plugin.getOption('addToCalendarOperationName').getValue() if eventType == 'added' else plugin.getOption('updateCalendarOperationName').getValue()
         elif eventType == 'removed':
             logger.debug("Removing calendar entry for: {}".format(avatar.getId()))
             self.payload = {'userEmail': avatar.getEmail(),
                             'uniqueID': plugin.getOption('prefix').getValue() + key}
             operation = plugin.getOption('removeFromCalendarOperationName').getValue()
         else:
             return None
         headers = {'content-type': 'application/x-www-form-urlencoded'}
         return requests.post(urlpath.tslash(plugin.getOption('url').getValue()) + operation,
                              auth=(plugin.getOption('login').getValue(), plugin.getOption('password').getValue()),
                              data=self.payload, headers=headers, timeout=plugin.getOption('timeout').getValue())
     except requests.exceptions.Timeout:
         logger.exception('Timeout')
     except requests.exceptions.RequestException:
         logger.exception('RequestException: Connection problem')
     except Exception, e:
         logger.exception('Outlook EventException: {}'.format(e))
예제 #3
0
    def run_export_files(self,
                         batch=1000,
                         force=False,
                         max_size=None,
                         verbose=True,
                         initial=False):
        from indico_citadel.plugin import CitadelPlugin

        if max_size is None:
            max_size = CitadelPlugin.settings.get('max_file_size')

        attachments = (CitadelIdMap.query.join(Attachment).join(
            AttachmentFile, Attachment.file_id == AttachmentFile.id
        ).filter(Attachment.type == AttachmentType.file).filter(
            AttachmentFile.size > 0,
            AttachmentFile.size <= max_size * 1024 * 1024).filter(
                db.func.lower(AttachmentFile.extension).in_([
                    s.lower()
                    for s in CitadelPlugin.settings.get('file_extensions')
                ])).options(
                    contains_eager(CitadelIdMap.attachment).contains_eager(
                        Attachment.file)))
        if not force:
            attachments = attachments.filter(
                db.or_(CitadelIdMap.attachment_file_id.is_(None),
                       CitadelIdMap.attachment_file_id != Attachment.file_id))
        uploader = self.uploader(self)
        attachments = attachments.yield_per(batch)
        total = attachments.count()
        if verbose:
            attachments = verbose_iterator(
                attachments,
                total,
                attrgetter('id'),
                lambda obj: re.sub(r'\s+', ' ',
                                   strip_control_chars(obj.attachment.title)),
                print_total_time=True)
        else:
            self.plugin.logger.info(f'{total} files need to be uploaded')
        total, errors, aborted = uploader.upload_files(attachments,
                                                       initial=initial)
        return total, errors, aborted
예제 #4
0
def _update_calendar_entry(entry, settings):
    """Executes a single calendar update

    :param entry: a :class:`OutlookQueueEntry`
    :param settings: the plugin settings
    """
    from indico_outlook.plugin import OutlookPlugin
    logger = OutlookPlugin.logger

    logger.info('Processing %s', entry)
    url = posixpath.join(settings['service_url'], operation_map[entry.action])
    user = entry.user
    if user is None:
        logger.debug('Ignoring %s for deleted user %s', entry.action.name,
                     entry.user_id)
        return True
    elif not OutlookPlugin.user_settings.get(user, 'enabled'):
        logger.debug('User %s has disabled calendar entries', user)
        return True

    unique_id = '{}{}_{}'.format(settings['id_prefix'], user.id,
                                 entry.event_id)
    if entry.action in {OutlookAction.add, OutlookAction.update}:
        event = entry.event
        if event.is_deleted:
            logger.debug('Ignoring %s for deleted event %s', entry.action.name,
                         entry.event_id)
            return True
        location = strip_control_chars(event.room_name)
        description = strip_control_chars(event.description)
        event_url = event.external_url
        data = {
            'userEmail':
            user.email,
            'uniqueID':
            unique_id,
            'subject':
            strip_control_chars(event.title),
            'location':
            location,
            'body':
            '<a href="{}">{}</a><br><br>{}'.format(event_url, event_url,
                                                   description),
            'status':
            OutlookPlugin.user_settings.get(user, 'status',
                                            settings['status']),
            'startDate':
            format_datetime(event.start_dt,
                            format='MM-dd-yyyy HH:mm',
                            timezone=pytz.utc),
            'endDate':
            format_datetime(event.end_dt,
                            format='MM-dd-yyyy HH:mm',
                            timezone=pytz.utc),
            'isThereReminder':
            settings['reminder'],
            'reminderTimeInMinutes':
            settings['reminder_minutes']
        }
    elif entry.action == OutlookAction.remove:
        data = {'userEmail': user.email, 'uniqueID': unique_id}
    else:
        raise ValueError('Unexpected action: {}'.format(entry.action))

    if settings['debug']:
        logger.debug('Calendar update request:\nURL: %s\nData: %s', url,
                     pformat(data))
        return True

    try:
        res = requests.post(
            url,
            data,
            auth=(settings['username'], settings['password']),
            timeout=settings['timeout'],
            headers={'Content-Type': 'application/x-www-form-urlencoded'})
    except Timeout:
        logger.warning('Request timed out')
        return False
    except RequestException:
        logger.exception('Request failed:\nURL: %s\nData: %s', url,
                         pformat(data))
        return False
    else:
        if res.status_code == 200:
            return True
        logger.error(
            'Request unsuccessful:\nURL: %s\nData: %s\nCode: %s\nResponse: %s',
            url, pformat(data), res.status_code, res.text)
        return False
예제 #5
0
 def _sendEventRequest(self, key, eventType, avatar, conference):
     try:
         logger = self.getLogger()
         plugin = PluginsHolder().getPluginType('calendaring').getPlugin(
             'outlook')
         if not isUserPluginEnabled(avatar.getId()):
             logger.info("Outlook plugin disabled for user: {}".format(
                 avatar.getId()))
             return {'status_code': 200}
         if eventType in ['added', 'updated']:
             logger.debug("Performing '{}' for: {}".format(
                 eventType, avatar.getId()))
             url = urlHandlers.UHConferenceDisplay.getURL(conference)
             location = strip_control_chars(conference.getRoom().getName(
             )) if conference.getRoom() else ''
             description = strip_control_chars(conference.getDescription())
             self.payload = {
                 'userEmail':
                 avatar.getEmail(),
                 'uniqueID':
                 plugin.getOption('prefix').getValue() + key,
                 'subject':
                 strip_control_chars(conference.getTitle()),
                 'location':
                 location,
                 'body':
                 '<a href="{}">{}</a><br><br>{}'.format(
                     url, url, description),
                 'status':
                 plugin.getOption('status').getValue(),
                 'startDate':
                 format_datetime(
                     conference.getStartDate(),
                     format=plugin.getOption('datetimeFormat').getValue(),
                     timezone=pytz.utc),
                 'endDate':
                 format_datetime(
                     conference.getEndDate(),
                     format=plugin.getOption('datetimeFormat').getValue(),
                     timezone=pytz.utc),
                 'isThereReminder':
                 plugin.getOption('reminder').getValue(),
                 'reminderTimeInMinutes':
                 plugin.getOption('reminder_minutes').getValue()
             }
             operation = plugin.getOption(
                 'addToCalendarOperationName').getValue(
                 ) if eventType == 'added' else plugin.getOption(
                     'updateCalendarOperationName').getValue()
         elif eventType == 'removed':
             logger.debug("Removing calendar entry for: {}".format(
                 avatar.getId()))
             self.payload = {
                 'userEmail': avatar.getEmail(),
                 'uniqueID': plugin.getOption('prefix').getValue() + key
             }
             operation = plugin.getOption(
                 'removeFromCalendarOperationName').getValue()
         else:
             return None
         headers = {'content-type': 'application/x-www-form-urlencoded'}
         return requests.post(
             urlpath.tslash(plugin.getOption('url').getValue()) + operation,
             auth=(plugin.getOption('login').getValue(),
                   plugin.getOption('password').getValue()),
             data=self.payload,
             headers=headers,
             timeout=plugin.getOption('timeout').getValue())
     except requests.exceptions.Timeout:
         logger.exception('Timeout')
     except requests.exceptions.RequestException:
         logger.exception('RequestException: Connection problem')
     except Exception, e:
         logger.exception('Outlook EventException: {}'.format(e))