Beispiel #1
0
    def get(self):
        req_parser = reqparse.RequestParser()
        req_parser.add_argument('filename', type=str, required=True)
        args = req_parser.parse_args()

        bucket = storage.get_storage_bucket()

        blob = bucket.blob(args['filename'])
        with tempfile.NamedTemporaryFile() as temp:
            blob.download_to_filename(temp.name)
            return send_file(temp.name,
                             as_attachment=True,
                             attachment_filename=args['filename'],
                             mimetype='application/pdf')
Beispiel #2
0
    def post(self):
        args = self.req_parser.parse_args()

        bucket = storage.get_storage_bucket()

        unique_name = str(uuid.uuid4().hex)
        blob = bucket.blob(unique_name)

        file = args['file']
        bytes_file = file.read()
        content_type = file.content_type
        file_size = len(bytes_file)

        if file_size > FILE_SIZE_LIMIT:
            LOGGER.debug('File size of {} exceeds limit of {}'.format(
                file_size, FILE_SIZE_EXCEEDED))
            return FILE_SIZE_EXCEEDED

        blob.upload_from_string(bytes_file, content_type=content_type)

        return {
            'file_id': unique_name,
        }, 201
Beispiel #3
0
    def get(self):
        def _get_answer(question_id, answers):
            # Get the answer for a question
            for a in answers:
                if a.question_id == question_id:
                    return a

            return None

        def _get_files(application_form, answers):
            # Get the response files that should be exported in the ZIP file

            file_names = []

            for section in application_form.sections:
                for question in section.questions:
                    answer = _get_answer(question.id, answers)
                    if answer is not None:
                        # We are only interested in the files,
                        # the text answers will be in the main PDF file
                        if question.type == 'multi-file':
                            file_names.extend(json.loads(answer.value))
                        if question.type == 'file':
                            file_names.append(json.loads(answer.value))

            return file_names

        req_parser = reqparse.RequestParser()
        req_parser.add_argument('response_id', type=int, required=True)
        req_parser.add_argument('language', type=str, required=True)
        args = req_parser.parse_args()

        response_id = args['response_id']
        language = args['language']

        response = response_repository.get_by_id(response_id)
        application_form = application_form_repository.get_by_id(response_id)

        # Build the HTML string
        response_string = strings.build_response_html_app_info(response, language) + \
        strings.build_response_html_answers(response.answers, language, application_form)

        # Convert to PDF
        files_to_compress = [('response.pdf',
                              pdfconvertor.html_to_pdf(response.id,
                                                       response_string))]

        # The files that were uploaded as part of the response
        files_to_get = _get_files(application_form, response.answers)
        bucket = storage.get_storage_bucket()

        # Download and name the files
        files_to_compress.extend([
            (f['rename'] or f['filename'],
             io.BytesIO(bucket.blob(f['filename']).download_as_bytes()))
            for f in files_to_get
        ])

        # Zip files
        zipped_files = zip_in_memory(files_to_compress)

        with tempfile.NamedTemporaryFile() as temp:
            temp.write(zipped_files.getvalue())
            return send_file(temp.name,
                             as_attachment=True,
                             attachment_filename=f"response_{response.id}.zip")