Exemplo n.º 1
0
    def get(self):

        # Ensure that the token is valid
        token = request.args.get('token', '')
        token_data = cache.get(NOW_DOCUMENT_DOWNLOAD_TOKEN(token))
        cache.delete(NOW_DOCUMENT_DOWNLOAD_TOKEN(token))
        if not token_data:
            raise BadRequest('Valid token required for download')

        # Get the template associated with the token
        doc_type = NOWApplicationDocumentType.query.unbound_unsafe().get(
            token_data['document_type_code'])
        template_path = os.path.join(current_app.root_path,
                                     doc_type.document_template.template_file_path)

        # Generate the document using the template and template data
        docgen_resp = DocumentGeneratorService.generate_document_and_stream_response(
            template_path, data=token_data['template_data'])

        # Push the document to the Document Manager
        filename = docgen_resp.headers['X-Report-Name']
        now_application_guid = token_data['now_application_guid']
        now_application_identity = NOWApplicationIdentity.query.unbound_unsafe().get(
            now_application_guid)
        document_manager_guid = DocumentManagerService.pushFileToDocumentManager(
            file_content=docgen_resp.content,
            filename=filename,
            mine=now_application_identity.mine,
            document_category='noticeofwork',
            authorization_header=token_data['authorization_header'])

        # Add the document to the Notice of Work's documents
        username = token_data['username']
        new_mine_doc = MineDocument(
            mine_guid=now_application_identity.now_application.mine_guid,
            document_manager_guid=document_manager_guid,
            document_name=filename,
            create_user=username,
            update_user=username)
        now_doc = NOWApplicationDocumentXref(
            mine_document=new_mine_doc,
            now_application_document_type=doc_type,
            now_application_id=now_application_identity.now_application_id,
            create_user=username,
            update_user=username)
        now_application_identity.now_application.documents.append(now_doc)
        now_application_identity.save()

        # Return the generated document
        file_gen_resp = Response(
            stream_with_context(docgen_resp.iter_content(chunk_size=2048)),
            headers=dict(docgen_resp.headers))
        return file_gen_resp
Exemplo n.º 2
0
    def test_generate_document_returns_token(self, test_client, db_session,
                                             auth_headers):
        """Should return the a token for successful generation"""
        now_application = NOWApplicationFactory()
        now_application_identity = NOWApplicationIdentityFactory(
            now_application=now_application)

        changed_mine_no = str(now_application_identity.mine.mine_no + '1')
        data = {
            'now_application_guid':
            now_application_identity.now_application_guid,
            'template_data': {
                'mine_no': changed_mine_no
            }
        }

        post_resp = test_client.post(
            f'/now-applications/application-document-types/RJL/generate',
            json=data,
            headers=auth_headers['full_auth_header'])

        assert post_resp.status_code == 200
        post_data = json.loads(post_resp.data.decode())
        token_data = cache.get(NOW_DOCUMENT_DOWNLOAD_TOKEN(post_data['token']))
        assert token_data is not None
        assert token_data['template_data']['mine_no'] != changed_mine_no
        assert post_data['token']
Exemplo n.º 3
0
    def post(self, document_type_code):
        document_type = NOWApplicationDocumentType.query.get(
            document_type_code)
        if not document_type:
            raise NotFound('Document type not found')

        if not document_type.document_template:
            raise BadRequest(f'Cannot generate a {document_type.description}')

        # TODO: Generate document using the provided data.
        data = self.parser.parse_args()
        template_data = data['template_data']

        ##ENFORCE READ-ONLY CONTEXT DATA
        enforced_data = [
            x for x in document_type.document_template._form_spec_with_context(
                data['now_application_guid']) if x.get('read-only', False)
        ]
        for enforced_item in enforced_data:
            if template_data.get(
                    enforced_item['id']) != enforced_item['context-value']:
                current_app.logger.debug(
                    f'OVERWRITING ENFORCED key={enforced_item["id"]}, value={template_data.get(enforced_item["id"])} -> {enforced_item["context-value"]}'
                )
            template_data[enforced_item['id']] = enforced_item['context-value']

        token = uuid.uuid4()
        cache.set(
            NOW_DOCUMENT_DOWNLOAD_TOKEN(token), {
                'document_type_code': document_type_code,
                'now_application_guid': data['now_application_guid'],
                'template_data': template_data
            }, TIMEOUT_5_MINUTES)

        return {'token': token}
Exemplo n.º 4
0
    def get(self):
        token = request.args.get('token', '')
        token_data = cache.get(NOW_DOCUMENT_DOWNLOAD_TOKEN(token))
        cache.delete(NOW_DOCUMENT_DOWNLOAD_TOKEN(token))

        if not token_data:
            raise BadRequest('Valid token required for download')

        doc_type = NOWApplicationDocumentType.query.unbound_unsafe().get(
            token_data['document_type_code'])
        template_path = os.path.join(current_app.root_path,
                                     doc_type.document_template.template_file_path)

        file_gen_resp = DocumentGeneratorService.generate_document_and_stream_response(
            template_path, data=token_data['template_data'])

        return file_gen_resp
    def post(self, document_type_code):
        document_type = NOWApplicationDocumentType.query.get(
            document_type_code)
        if not document_type:
            raise NotFound('Document type not found')

        if not document_type.document_template:
            raise BadRequest(f'Cannot generate a {document_type.description}')

        data = self.parser.parse_args()
        template_data = data['template_data']

        ##ENFORCE READ-ONLY CONTEXT DATA
        enforced_data = [
            x for x in document_type.document_template._form_spec_with_context(
                data['now_application_guid']) if x.get('read-only', False)
        ]
        for enforced_item in enforced_data:
            if template_data.get(
                    enforced_item['id']) != enforced_item['context-value']:
                current_app.logger.debug(
                    f'OVERWRITING ENFORCED key={enforced_item["id"]}, value={template_data.get(enforced_item["id"])} -> {enforced_item["context-value"]}'
                )
            template_data[enforced_item['id']] = enforced_item['context-value']

        token = uuid.uuid4()
        # For now, we don't have a "proper" means of authorizing communication between our microservices, so this temporary solution
        # has been put in place to authorize with the document manager (pass the authorization headers into the token and re-use them
        # later). A ticket (MDS-2744) to set something else up as been created.
        cache.set(
            NOW_DOCUMENT_DOWNLOAD_TOKEN(token), {
                'document_type_code': document_type_code,
                'now_application_guid': data['now_application_guid'],
                'template_data': template_data,
                'username': User().get_user_username(),
                'authorization_header': request.headers['Authorization']
            }, TIMEOUT_5_MINUTES)

        return {'token': token}