Ejemplo n.º 1
0
    def import_key(key, storage_bucket, comm, log, title=None):
        """Import a key"""

        foia = comm.foia
        file_name = os.path.split(key.name)[1]

        title = title or file_name
        access = 'private' if foia.embargo else 'public'

        foia_file = FOIAFile(
            comm=comm,
            title=title,
            datetime=comm.datetime,
            source=comm.get_source(),
            access=access,
        )
        full_file_name = foia_file.ffile.field.generate_filename(
            foia_file.ffile.instance,
            file_name,
        )
        full_file_name = default_storage.get_available_name(full_file_name)
        new_key = key.copy(storage_bucket, full_file_name)
        new_key.set_acl('public-read')

        foia_file.ffile.name = full_file_name
        foia_file.save()
        if key.size != foia_file.ffile.size:
            raise SizeError(key.size, foia_file.ffile.size, foia_file)

        log.append(
            'SUCCESS: %s uploaded to FOIA Request %s with a status of %s' %
            (file_name, foia.pk, foia.status))

        upload_document_cloud.apply_async(args=[foia_file.pk, False],
                                          countdown=3)
Ejemplo n.º 2
0
def success_comm(request):
    """"File has been succesfully uploaded directly to a communication"""
    try:
        comm = FOIACommunication.objects.get(pk=request.POST.get('id'))
    except FOIACommunication.DoesNotExist:
        return HttpResponseBadRequest()
    if not (
        comm.foia and comm.foia.has_perm(request.user, 'upload_attachment')
    ):
        return HttpResponseForbidden()
    if 'key' not in request.POST:
        return HttpResponseBadRequest()
    if len(request.POST['key']) > 255:
        return HttpResponseBadRequest()

    access = 'private' if comm.foia.embargo else 'public'
    file_ = FOIAFile(
        comm=comm,
        title=os.path.basename(request.POST['key']),
        datetime=timezone.now(),
        source=request.user.profile.full_name,
        access=access,
    )
    file_.ffile.name = request.POST['key']
    file_.save()

    return HttpResponse()
Ejemplo n.º 3
0
def _upload_file(foia, comm, file_, sender):
    """Upload a file to attach to a FOIA request"""

    access = 'private' if foia and foia.embargo else 'public'
    source = foia.agency.name if foia and foia.agency else sender

    foia_file = FOIAFile(foia=foia,
                         comm=comm,
                         title=os.path.splitext(file_.name)[0][:70],
                         date=datetime.now(),
                         source=source[:70],
                         access=access)
    # max db size of 255, - 22 for folder name
    foia_file.ffile.save(file_.name[:233].encode('ascii', 'ignore'), file_)
    foia_file.save()
    if foia:
        upload_document_cloud.apply_async(args=[foia_file.pk, False],
                                          countdown=3)
Ejemplo n.º 4
0
def key_name_comm(request):
    """Generate the S3 key name from the filename"""
    name = request.POST.get('name')
    name = _key_name_trim(name)
    file_ = FOIAFile()
    key = file_.ffile.field.generate_filename(
        file_.ffile.instance,
        name,
    )
    key = default_storage.get_available_name(key)
    return JsonResponse({'key': key})