Ejemplo n.º 1
0
 def set_status(self, status, set_foia=True, comms=None):
     """Sets status of comm and foia, with option for only setting comm stats"""
     # check that status is valid
     if status not in [status_set[0] for status_set in STATUS]:
         raise ValueError('Invalid status.')
     if comms is None:
         comms = [self.communication]
     for comm in comms:
         # save comm first
         comm.status = status
         comm.save()
         # save foia next, unless just updating comm status
         if set_foia:
             foia = comm.foia
             foia.status = status
             if status in ['rejected', 'no_docs', 'done', 'abandoned']:
                 foia.date_done = comm.date
             foia.update()
             foia.save(comment='response task status')
             logging.info('Request #%d status changed to "%s"', foia.id,
                          status)
             action = generate_status_action(foia)
             foia.notify(action)
             # Mark generic '<Agency> sent a communication to <FOIARequest> as read.'
             # https://github.com/MuckRock/muckrock/issues/1003
             generic_notifications = (
                 Notification.objects.for_object(foia).get_unread().filter(
                     action__verb='sent a communication'))
             for generic_notification in generic_notifications:
                 generic_notification.mark_read()
Ejemplo n.º 2
0
 def proxy_reject(self, comms=None):
     """Special handling for a proxy reject"""
     if comms is None:
         comms = [self.communication]
     for comm in comms:
         comm.status = 'rejected'
         comm.save()
         foia = comm.foia
         foia.status = 'rejected'
         foia.proxy_reject()
         foia.update()
         foia.save(comment='response task proxy reject')
         action = generate_status_action(foia)
         foia.notify(action)
Ejemplo n.º 3
0
    def process(log):
        """Process the files"""
        log.append('Start Time: %s' % datetime.now())
        conn = S3Connection(settings.AWS_ACCESS_KEY_ID,
                            settings.AWS_SECRET_ACCESS_KEY)
        bucket = conn.get_bucket(settings.AWS_AUTOIMPORT_BUCKET_NAME)
        storage_bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)
        for key in bucket.list(prefix='scans/', delimiter='/'):
            if key.name == 'scans/':
                continue
            # strip off 'scans/'
            file_name = key.name[6:]

            try:
                (foia_pks, file_date, code, title, status, body, arg, id_,
                 est_date) = parse_name(file_name)
            except ValueError as exc:
                s3_copy(bucket, key, 'review/%s' % file_name)
                s3_delete(bucket, key)
                log.append(unicode(exc))
                continue

            for foia_pk in foia_pks:
                try:
                    foia = FOIARequest.objects.get(pk=foia_pk)
                    source = foia.agency.name if foia.agency else ''

                    comm = FOIACommunication.objects.create(
                        foia=foia,
                        from_who=source,
                        to_who=foia.user.get_full_name(),
                        response=True,
                        date=file_date,
                        full_html=False,
                        delivered='mail',
                        communication=body,
                        status=status)

                    foia.status = status or foia.status
                    if foia.status in [
                            'partial', 'done', 'rejected', 'no_docs'
                    ]:
                        foia.date_done = file_date.date()
                    if code == 'FEE' and arg:
                        foia.price = Decimal(arg)
                    if id_:
                        foia.tracking_id = id_
                    if est_date:
                        foia.date_estimate = est_date
                    if code == 'REJ-P':
                        foia.proxy_reject()

                    if key.name.endswith('/'):
                        import_prefix(key, bucket, storage_bucket, comm, log)
                    else:
                        import_key(key, storage_bucket, comm, log, title=title)

                    foia.save(comment='updated from autoimport files')
                    action = generate_status_action(foia)
                    foia.notify(action)
                    foia.update(comm.anchor())

                except FOIARequest.DoesNotExist:
                    s3_copy(bucket, key, 'review/%s' % file_name)
                    log.append(
                        'ERROR: %s references FOIA Request %s, but it does not exist'
                        % (file_name, foia_pk))
                except SoftTimeLimitExceeded:
                    # if we reach the soft time limit,
                    # re-raise so we can catch and clean up
                    raise
                except Exception as exc:
                    s3_copy(bucket, key, 'review/%s' % file_name)
                    log.append('ERROR: %s has caused an unknown error. %s' %
                               (file_name, exc))
                    logger.error('Autoimport error: %s',
                                 exc,
                                 exc_info=sys.exc_info())
            # delete key after processing all requests for it
            s3_delete(bucket, key)
        log.append('End Time: %s' % datetime.now())