Ejemplo n.º 1
0
    def attach_file(self, file_=None, content=None, name=None, source=None):
        """Given a file or name and the file contents, attach a file to this"""
        # must supply either file_ or content and name_
        from muckrock.foia.tasks import upload_document_cloud
        if file_ is None:
            file_ = ContentFile(content)
        if name is None:
            name = file_.name
        if source is None:
            source = self.get_source()

        title = os.path.splitext(name)[0][:255]
        access = 'private' if not self.foia or self.foia.embargo else 'public'
        with transaction.atomic():
            foia_file = self.files.create(
                title=title,
                datetime=timezone.now(),
                source=source[:70],
                access=access,
            )
            name = name[:233].encode('ascii', 'ignore')
            foia_file.ffile.save(name, file_)
            if self.foia:
                transaction.on_commit(
                    lambda: upload_document_cloud.delay(foia_file.pk, False))
        return foia_file
Ejemplo n.º 2
0
    def save_formset(self, request, form, formset, change):
        """Actions to take while saving inline files"""
        # pylint: disable=unused-argument

        if formset.model != FOIAFile:
            super(FOIACommunicationAdmin,
                  self).save_formset(request, form, formset, change)
            return

        instances = formset.save(commit=False)
        for instance in instances:
            # only way to tell if its new or not is to check the db
            change = True
            try:
                formset.model.objects.get(pk=instance.pk)
            except formset.model.DoesNotExist:
                change = False

            instance.save()
            # its new, so notify the user about it
            if not change:
                instance.comm.foia.update(instance.anchor())

            transaction.on_commit(lambda instance=instance:
                                  upload_document_cloud.delay(instance.pk))

        formset.save_m2m()

        for obj in formset.deleted_objects:
            obj.delete()
Ejemplo n.º 3
0
def foia_update_embargo(sender, **kwargs):
    """When embargo has possibly been switched, update the document cloud permissions"""
    # pylint: disable=unused-argument
    request = kwargs["instance"]
    old_request = request.get_saved()
    # if we are saving a new FOIA Request, there are no docs to update
    if old_request and request.embargo != old_request.embargo:
        for doc in request.get_files().get_doccloud():
            transaction.on_commit(
                lambda doc=doc: upload_document_cloud.delay(doc.pk))
Ejemplo n.º 4
0
    def attach_file(self,
                    file_=None,
                    content=None,
                    path=None,
                    name=None,
                    source=None,
                    now=True):
        """Attach a file to this communication"""
        # must supply either:
        # * a file_
        # * content and name_
        # * path and name_ (for files already uploaded to s3)
        # pylint: disable=import-outside-toplevel, too-many-arguments
        from muckrock.foia.tasks import upload_document_cloud

        assert ((file_ is not None)
                or (content is not None and name is not None)
                or (path is not None and name is not None))

        if file_ is None and content is not None:
            file_ = ContentFile(content)
        if name is None and file_ is not None:
            name = file_.name
        if source is None:
            source = self.get_source()

        title = os.path.splitext(name)[0][:255]
        with transaction.atomic():
            foia_file = FOIAFile(
                comm=self,
                title=title,
                datetime=timezone.now() if now else self.datetime,
                source=source,
            )
            if file_:
                name = name[:233].encode("ascii", "ignore").decode()
                foia_file.ffile.save(name, UnclosableFile(file_))
            else:
                foia_file.ffile.name = path
                foia_file.save()
            if self.foia:
                transaction.on_commit(
                    lambda: upload_document_cloud.delay(foia_file.pk))
        return foia_file
Ejemplo n.º 5
0
    def clone(self, new_comm):
        """Clone this file to a new communication"""
        # pylint: disable=import-outside-toplevel
        from muckrock.foia.tasks import upload_document_cloud

        original_id = self.pk
        self.pk = None
        self.comm = new_comm
        self.source = new_comm.get_source()
        # make a copy of the file on the storage backend
        try:
            new_ffile = ContentFile(self.ffile.read())
        except ValueError:
            error_msg = ("FOIAFile #%s has no data in its ffile field. "
                         "It has not been cloned.")
            logger.error(error_msg, original_id)
            return
        new_ffile.name = self.name()
        self.ffile = new_ffile
        self.save()
        transaction.on_commit(lambda: upload_document_cloud.delay(self.pk))
Ejemplo n.º 6
0
    def move(self, foia_pks, user):
        """
        Move this communication. If more than one foia_pk is given, move the
        communication to the first request, then clone it across the rest of
        the requests. Returns the moved and cloned communications.
        """
        # avoid circular imports
        # pylint: disable=import-outside-toplevel
        from muckrock.foia.tasks import upload_document_cloud

        foias = FOIARequest.objects.filter(pk__in=foia_pks)
        if not foias:
            raise ValueError(
                "Expected a request to move the communication to.")
        old_foia = self.foia
        self.foia = foias[0]

        with transaction.atomic():
            for file_ in self.files.all():
                file_.source = self.get_source()
                file_.save()
                transaction.on_commit(
                    lambda file_=file_: upload_document_cloud.delay(file_.pk))
            self.save()
            CommunicationMoveLog.objects.create(communication=self,
                                                foia=old_foia,
                                                user=user)
        logger.info("Communication #%d moved to request #%d", self.id,
                    self.foia.id)
        # if cloning happens, self gets overwritten. so we save it to a variable here
        comm = FOIACommunication.objects.get(pk=self.pk)
        moved = [comm]
        cloned = []
        if foias[1:]:
            cloned = self.clone(foias[1:], user)
        return moved + cloned