예제 #1
0
파일: services.py 프로젝트: stefanw/froide
    def add_attachments(self, foirequest, message, attachments):
        account_service = AccountService(foirequest.user)
        names = set()

        for i, attachment in enumerate(attachments):
            att = FoiAttachment(
                belongs_to=message,
                name=attachment.name,
                size=attachment.size,
                filetype=attachment.content_type
            )
            if not att.name:
                att.name = _("attached_file_%d") % i

            # Translators: replacement for person name in filename
            repl = str(_('NAME'))
            att.name = account_service.apply_name_redaction(att.name, repl)
            att.name = re.sub(r'[^A-Za-z0-9_\.\-]', '', att.name)
            att.name = att.name[:250]

            # Assure name is unique
            if att.name in names:
                att.name = add_number_to_filename(att.name, i)
            names.add(att.name)

            if foirequest.not_publishable:
                att.can_approve = False

            attachment._committed = False
            att.file = File(attachment)
            att.save()

            if att.can_convert_to_pdf():
                self.trigger_convert_pdf(att.id)
예제 #2
0
    def add_attachments(self, foirequest, message, attachments):
        account_service = AccountService(foirequest.user)
        names = set()

        for i, attachment in enumerate(attachments):
            att = FoiAttachment(belongs_to=message,
                                name=attachment.name,
                                size=attachment.size,
                                filetype=attachment.content_type)
            if not att.name:
                att.name = _("attached_file_%d") % i

            # Translators: replacement for person name in filename
            repl = str(_('NAME'))
            att.name = account_service.apply_name_redaction(att.name, repl)
            att.name = re.sub(r'[^A-Za-z0-9_\.\-]', '', att.name)
            att.name = att.name[:250]

            # Assure name is unique
            if att.name in names:
                att.name = add_number_to_filename(att.name, i)
            names.add(att.name)

            attachment._committed = False
            att.file = File(attachment)
            att.save()
예제 #3
0
def convert_to_pdf(request, foirequest, message, data):
    att_ids = [a['id'] for a in data['images']]
    title = data.get('title') or _('letter')
    names = set(a.name for a in message.attachments)

    atts = message.foiattachment_set.filter(
        id__in=att_ids, filetype__startswith='image/'
    )
    safe_att_ids = {a.id for a in atts}
    att_ids = [aid for aid in att_ids if aid in safe_att_ids]

    name = '{}.pdf'.format(slugify(title))

    i = 0
    while True:
        if name not in names:
            break
        i += 1
        name = add_number_to_filename(name, i)

    can_approve = not foirequest.not_publishable
    att = FoiAttachment.objects.create(
        name=name,
        belongs_to=message,
        approved=False,
        filetype='application/pdf',
        is_converted=True,
        can_approve=can_approve,
    )

    FoiAttachment.objects.filter(id__in=att_ids).update(
        converted_id=att.id, can_approve=False, approved=False
    )
    instructions = {
        d['id']: d for d in data['images'] if d['id'] in att_ids
    }
    instructions = [
        instructions[i] for i in att_ids
    ]
    convert_images_to_pdf_task.delay(
        att_ids, att.id, instructions, can_approve=can_approve
    )

    attachment_data = FoiAttachmentSerializer(att, context={
        'request': request
    }).data
    return JsonResponse(attachment_data)
예제 #4
0
파일: message.py 프로젝트: stefanw/froide
def convert_to_pdf(request, foirequest, message, data):
    att_ids = [a['id'] for a in data['images']]
    title = data.get('title') or _('letter')
    names = set(a.name for a in message.attachments)

    atts = message.foiattachment_set.filter(
        id__in=att_ids, filetype__startswith='image/'
    )
    safe_att_ids = {a.id for a in atts}
    att_ids = [aid for aid in att_ids if aid in safe_att_ids]

    name = '{}.pdf'.format(slugify(title))

    i = 0
    while True:
        if name not in names:
            break
        i += 1
        name = add_number_to_filename(name, i)

    can_approve = not foirequest.not_publishable
    att = FoiAttachment.objects.create(
        name=name,
        belongs_to=message,
        approved=False,
        filetype='application/pdf',
        is_converted=True,
        can_approve=can_approve,
    )

    FoiAttachment.objects.filter(id__in=att_ids).update(
        converted_id=att.id, can_approve=False, approved=False
    )
    instructions = {
        d['id']: d for d in data['images'] if d['id'] in att_ids
    }
    instructions = [
        instructions[i] for i in att_ids
    ]
    convert_images_to_pdf_task.delay(
        att_ids, att.id, instructions, can_approve=can_approve
    )

    attachment_data = FoiAttachmentSerializer(att, context={
        'request': request
    }).data
    return JsonResponse(attachment_data)