def generate_pdf(url, context, ignore_context=False): """ Builds a PDF of the passed in url. The URL is fetched relative to the context, unless ignore_context is True """ request = getattr(context, "REQUEST", None) base_url = context.getParentDatabase().absolute_url() # Use subrequest to access the passed in url if ignore_context: page = subrequest(url) else: page = subrequest(url, root=context) # Transform the html # Have to set the content-type header to text/html before we can transform actual_header = request.response.getHeader('Content-Type') request.response.setHeader('Content-Type', 'text/html') html = transform_html(page.getBody(), request) request.response.setHeader('Content-Type', actual_header) pdf = HTML(string=html, base_url=base_url, url_fetcher=my_fetcher) output = StringIO() pdf.write_pdf(output) output.seek(0) return output
def my_fetcher(url): uri = url # Otherwise fetch the data response = subrequest(unquote(uri)) # Handle redirects if response.status == 301: uri = response.getHeader('location') response = subrequest(unquote(uri)) if response.status != 200: raise Exception("URI not found") content_type = response.getHeader('content-type') # Default encoding encoding = 'utf-8' if content_type: if ';' in content_type: ctype, encoding = content_type.split(';') encoding = encoding.split('charset=')[-1] else: ctype = content_type # Guess the content_type from the URI if needed else: ctype, encoding = guess_content_type(uri) if ctype and ctype.startswith('text/'): ctype = text_type(uri) data = response.getBody() # I don't think we need to encode ctype == 'text/css' to ascii anymore return dict(string=data, mime_type=ctype, encoding=encoding)
def __call__(self, url, use_vhm=True): url = normalize_url(url) if not url: return if not url.startswith(self.public_url): return if '++plone++production' in url: front, end = url.rsplit('/', 1) # check blacklist for black_listed in self._blacklisted_content: if url.startswith(front + black_listed): return # since we're looking at plone here... let's try fixing up urls... if '++plone++' in url: # can always be from site root url = self.public_url + '/++plone++' + url.rsplit('++plone++', 1)[-1] # noqa if use_vhm: parsed = urlparse(url) vhm_path = self.vhm_base + parsed.path resp = subrequest(vhm_path) else: resp = subrequest(url) if resp.getStatus() == 404: return return { 'data': resp.getBody(), 'headers': resp.headers, 'code': resp.getStatus() }
def resolveResource(url): """Resolve the given URL to a unicode string. If the URL is an absolute path, it will be made relative to the Plone site root. """ if url.startswith('/'): site = getSite() url = '/'.join(site.getPhysicalPath()) + url response = subrequest(url) if response.status == 404: raise NotFound(url) resolved = response.getBody() if isinstance(resolved, str): charset = extractCharset(response) resolved = resolved.decode(charset) if response.status in (301, 302): site = getSite() location = response.headers.get('location') or '' if location.startswith(site.absolute_url()): return resolveResource(location[len(site.absolute_url()):]) elif response.status != 200: raise RuntimeError(resolved) return resolved
def test_virtual_host_space(self): parts = ("folder2", "folder2A/folder2Ai space/@@url") url = "/VirtualHostBase/http/example.org:80/" "{0}/VirtualHostRoot/{1}".format(*parts) traverse(url) app = self.layer["app"] response = subrequest("/folder2A/@@url", root=app.folder2) self.assertEqual(response.body, "http://example.org/folder2A")
def test_virtual_hosting_relative(self): url = VH_TPL % ('folder1', 'folder1A?url=folder1B/@@url') response = subrequest(url) self.assertEqual( response.body, 'http://example.org/fizz/buzz/fizzbuzz/folder1B' )
def test_error(self): registry = getUtility(IRegistry) bundles = registry.collectionOfInterface(IBundleRegistry, prefix="plone.bundles") bundle = bundles.add('foobar') bundle.jscompilation = '++plone++static/foobar-compiled.js' bundle.csscompilation = '++plone++static/foobar-compiled.css' resources = registry.collectionOfInterface(IResourceRegistry, prefix="plone.resources") resource = resources.add('foobar') resource.js = '++plone++static/foobar.js' bundle.resources = ['foobar'] persistent_directory = getUtility(IResourceDirectory, name="persistent") if OVERRIDE_RESOURCE_DIRECTORY_NAME not in persistent_directory: persistent_directory.makeDirectory( OVERRIDE_RESOURCE_DIRECTORY_NAME) container = persistent_directory[OVERRIDE_RESOURCE_DIRECTORY_NAME] container.makeDirectory('static') directory = container['static'] directory.writeFile('foobar.js', 'sdlfk ldsf lksdjfl s') cookWhenChangingSettings(self.portal, bundle) resp = subrequest('{0}/++plone++static/foobar-compiled.js'.format( self.portal.absolute_url())) self.assertTrue('error cooking' in resp.getBody())
def fetch_resources(uri, rel): """ Callback to allow pisa/reportlab to retrieve Images,Stylesheets, etc. `uri` is the href attribute from the html link element. `rel` gives a relative path, but it's not used here. """ urltool = getToolByName(self.context, "portal_url") portal = urltool.getPortalObject() base = portal.absolute_url() if uri.startswith(base): response = subrequest(unquote(uri[len(base)+1:])) if response.status != 200: return None try: # stupid pisa doesn't let me send charset. ctype,encoding = response.getHeader('content-type').split('charset=') ctype = ctype.split(';')[0] # pisa only likes ascii css data = response.getBody().decode(encoding).encode('ascii',errors='ignore') except ValueError: ctype = response.getHeader('content-type').split(';')[0] data = response.getBody() data = data.encode("base64").replace("\n", "") data_uri = 'data:{0};base64,{1}'.format(ctype, data) return data_uri return uri
def view_page(self, obj): if obj is self.context: return self.sections_template() al = self.request.get('ajax_load') self.request.set('ajax_load', True) path = '/'.join(obj.getPhysicalPath()) resp = subrequest(path) body = resp.getBody() e = fromstring(body) nodes = e.cssselect("#content-core > *") if not nodes: nodes = e.cssselect("#content > *") classes = e.xpath('//body')[0].get('class') ptype = [x for x in classes.split(' ') if x.startswith('portaltype-')] ptype = ptype and ptype[0] or '' root = E('div') if ptype: root.set('class', ptype) for node in nodes: root.append(node) content = tostring(root, pretty_print=True) if al: self.request.set('ajax_load', al) return content
def test_virtual_host_root_at_root_trailing(self): url = ("/VirtualHostBase/http/example.org:80/folder1/VirtualHostRoot/" "_vh_fizz/_vh_buzz/_vh_fizzbuzz/") traverse(url) response = subrequest('/folder1B/@@url') self.assertEqual(response.body, 'http://example.org/fizz/buzz/fizzbuzz/folder1B')
def test_error(self): registry = getUtility(IRegistry) bundles = registry.collectionOfInterface(IBundleRegistry, prefix="plone.bundles") bundle = bundles.add('foobar') bundle.jscompilation = '++plone++static/foobar-compiled.js' bundle.csscompilation = '++plone++static/foobar-compiled.css' resources = registry.collectionOfInterface(IResourceRegistry, prefix="plone.resources") resource = resources.add('foobar') resource.js = '++plone++static/foobar.js' bundle.resources = ['foobar'] persistent_directory = getUtility( IResourceDirectory, name="persistent") if OVERRIDE_RESOURCE_DIRECTORY_NAME not in persistent_directory: persistent_directory.makeDirectory( OVERRIDE_RESOURCE_DIRECTORY_NAME) container = persistent_directory[OVERRIDE_RESOURCE_DIRECTORY_NAME] container.makeDirectory('static') directory = container['static'] directory.writeFile('foobar.js', 'sdlfk ldsf lksdjfl s') cookWhenChangingSettings(self.portal, bundle) resp = subrequest( '{0}/++plone++static/foobar-compiled.js'.format( self.portal.absolute_url() ) ) self.assertTrue('error cooking' in resp.getBody())
def test_site(self): traverse('/folder1') site_url1 = getSite().absolute_url() response = subrequest('/folder2/@@url') self.assertEqual(response.status, 200) site_url2 = getSite().absolute_url() self.assertEqual(site_url1, site_url2)
def resolveResource(url): """Resolve the given URL to a unicode string. If the URL is an absolute path, it will be made relative to the Plone site root. """ if url.startswith('/'): site = getSite() portal_url = getToolByName(site, 'portal_url') url = portal_url.getPortalObject().absolute_url_path() + url response = subrequest(url) if response.status == 404: raise NotFound(url) resolved = response.getBody() if isinstance(resolved, str): charset = extractCharset(response) resolved = resolved.decode(charset) if response.status != 200: raise RuntimeError(resolved) can_edit = True if response.headers.get('x-tile-editable', '0') == '1' else False return resolved, can_edit
def test_stream_iterator(self): # Only a ZServerHTTPResponse is IStreamIterator Aware from ZServer.HTTPResponse import ZServerHTTPResponse request = getRequest() request.response.__class__ = ZServerHTTPResponse response = subrequest('/@@stream') self.assertEqual(response.getBody(), "hello")
def test_virtual_host_space(self): parts = ('folder2', 'folder2A/folder2Ai space/@@url') url = "/VirtualHostBase/http/example.org:80/%s/VirtualHostRoot/%s" % parts traverse(url) app = self.layer['app'] response = subrequest('/folder2A/@@url', root=app.folder2) self.assertEqual(response.body, 'http://example.org/folder2A')
def test_virtual_host_root_with_root(self): parts = ('folder1', 'folder1A/@@url') url = "/VirtualHostBase/http/example.org:80/%s/VirtualHostRoot/_vh_fizz/_vh_buzz/_vh_fizzbuzz/%s" % parts traverse(url) app = self.layer['app'] response = subrequest('/folder1Ai/@@url', root=app.folder1.folder1A) self.assertEqual(response.body, 'http://example.org/fizz/buzz/fizzbuzz/folder1A/folder1Ai')
def fetch_resources(uri, rel): """ Callback to allow pisa/reportlab to retrieve Images,Stylesheets, etc. `uri` is the href attribute from the html link element. `rel` gives a relative path, but it's not used here. """ urltool = getToolByName(self.context, "portal_url") portal = urltool.getPortalObject() base = portal.absolute_url() if uri.startswith(base): response = subrequest(unquote(uri[len(base) + 1:])) if response.status != 200: return None try: # stupid pisa doesn't let me send charset. ctype, encoding = response.getHeader('content-type').split( 'charset=') ctype = ctype.split(';')[0] # pisa only likes ascii css data = response.getBody().decode(encoding).encode( 'ascii', errors='ignore') except ValueError: ctype = response.getHeader('content-type').split(';')[0] data = response.getBody() data = data.encode("base64").replace("\n", "") data_uri = 'data:{0};base64,{1}'.format(ctype, data) return data_uri return uri
def resolve(self, system_url, public_id, context): request = getRequest() if request is None: return None # Ignore URLs with a scheme if '://' in system_url: return None # Ignore the special 'diazo:' resolvers if system_url.startswith('diazo:'): return None context = findContext(request) portalState = queryMultiAdapter( (context, request), name=u"plone_portal_state") if portalState is None: root = None else: root = portalState.navigation_root() if not system_url.startswith('/'): # only for relative urls root_path = root.getPhysicalPath() context_path = context.getPhysicalPath()[len(root_path):] if len(context_path) == 0: system_url = '/' + system_url else: system_url = '/%s/%s' % ('/'.join(context_path), system_url) response = subrequest(system_url, root=root) if response.status != 200: LOGGER.error("Couldn't resolve %s", system_url) return None result = response.getBody() content_type = response.headers.get('content-type') encoding = None if content_type is not None and ';' in content_type: content_type, encoding = content_type.split(';', 1) if encoding is None: encoding = 'utf-8' else: # e.g. charset=utf-8 encoding = encoding.split('=', 1)[1].strip() result = result.decode(encoding).encode('ascii', 'xmlcharrefreplace') if content_type in ('text/javascript', 'application/x-javascript'): result = ''.join([ '<html><body><script type="text/javascript">', result, '</script></body></html>', ]) elif content_type == 'text/css': result = ''.join([ '<html><body><style type="text/css">', result, '</style></body></html>', ]) return self.resolve_string(result, context)
def test_relative(self): response = subrequest('/folder1?url=folder1B/@@url') # /folder1 resolves to /folder1/@@test self.assertEqual( response.body, b'http://nohost/folder1/folder1B' )
def test_subrequest_root(self): app = self.layer['app'] response = subrequest('/folder1Ai/@@url', root=app.folder1.folder1A) self.assertEqual( response.body, b'http://nohost/folder1/folder1A/folder1Ai' )
def test_virtual_hosting(self): url = VH_TPL.format('folder1', 'folder1A/@@url') response = subrequest(url) self.assertEqual( response.body, 'http://example.org/fizz/buzz/fizzbuzz/folder1A' )
def test_virtual_hosting_relative(self): url = VH_TPL.format('folder1', 'folder1A?url=folder1B/@@url') response = subrequest(url) self.assertEqual( response.body, b'http://nohost/fizz/buzz/fizzbuzz/folder1B' )
def test_virtual_host_root_with_root(self): parts = ("folder1", "folder1A/@@url") url = VH_TPL.format(*parts) traverse(url) app = self.layer["app"] response = subrequest("/folder1Ai/@@url", root=app.folder1.folder1A) self.assertEqual(response.body, "http://example.org/fizz/buzz/fizzbuzz/folder1A/folder1Ai")
def fetch_resources(portalbase, uri, resource_type="image"): """ Callback to allow pisa/reportlab to retrieve Images,Stylesheets, etc. `uri` is the href attribute from the html link element. """ if uri.startswith(portalbase): response = subrequest(unquote(uri[len(portalbase) + 1:])) if response.status != 200: return None ct = response.getHeader('content-type') data = response.getBody() else: response = requests.get(uri) if response.status_code != 200: return None ct = response.headers['content-type'] data = response.text if resource_type == 'image': try: # stupid pisa doesn't let me send charset. ctype, encoding = ct.split('charset=') ctype = ctype.split(';')[0] # pisa only likes ascii css data = data.decode(encoding).encode('ascii', errors='ignore') except ValueError: ctype = ct.split(';')[0] data = data.encode("base64").replace("\n", "") data_uri = 'data:{0};base64,{1}'.format(ctype, data) return data_uri else: return data
def test_cook_only_css(self): registry = getUtility(IRegistry) bundles = registry.collectionOfInterface(IBundleRegistry, prefix="plone.bundles") bundle = bundles.add('foobar') bundle.jscompilation = '' bundle.csscompilation = '++plone++static/foobar-compiled.css' resources = registry.collectionOfInterface(IResourceRegistry, prefix="plone.resources") resource = resources.add('foobar') resource.css = ['++plone++static/foobar.min.css'] bundle.resources = ['foobar'] persistent_directory = getUtility(IResourceDirectory, name="persistent") if OVERRIDE_RESOURCE_DIRECTORY_NAME not in persistent_directory: persistent_directory.makeDirectory( OVERRIDE_RESOURCE_DIRECTORY_NAME) container = persistent_directory[OVERRIDE_RESOURCE_DIRECTORY_NAME] container.makeDirectory('static') directory = container['static'] directory.writeFile('foobar.min.css', 'body {\ncolor: red;\n}') cookWhenChangingSettings(self.portal, bundle) resp_css = subrequest('{0}/++plone++static/foobar-compiled.css'.format( self.portal.absolute_url())) self.assertIn(b'body {\ncolor: red;\n}', resp_css.getBody())
def set_theme(obj, event): """ """ # When using /_vh_ subpaths it is safer to use this approach base = '/'.join(obj.getPhysicalPath()) diazo_setter = subrequest( # noqa base + '/@@local-diazo-setter')
def test_virtual_host_root(self): parts = ('folder1', 'folder1A/@@url') url = VH_TPL % parts traverse(url) response = subrequest('/folder1B/@@url') self.assertEqual(response.body, 'http://example.org/fizz/buzz/fizzbuzz/folder1B')
def get_data_from_url(url, portal=None, site_url=None): if portal is None: portal = api.portal.get() if site_url is None: site_url = portal.absolute_url() data = None path = url.replace(site_url, '').strip('/') ct = '' if '/@@images/' in path: try: path, _, im_info = path.partition('/@@images/') parts = im_info.split('/') if len(parts) == 2: fieldname = parts[0] size = parts[1] images = portal.restrictedTraverse(str(path + '/@@images'), None) if images is not None: im = images.traverse(fieldname, [size]) try: data = im.scale.data.data ct = im.scale.data.contentType except AttributeError: pass else: # grab full size ob = portal.restrictedTraverse(str(path), None) data = ob.image.data ct = ob.image.contentType except: logger.error('Could not traverse image ' + url, exc_info=True) if data is None: ob = portal.restrictedTraverse(str(path), None) file_path = getattr(ob, 'path', None) if file_path is None: try: file_path = ob.context.path except: pass if file_path and os.path.exists(file_path): fi = open(file_path) data = fi.read() fi.close() ct = 'image/' + file_path.split('.')[-1].lower() else: resp = subrequest(unquote(url)) if resp.status != 200: return None, None try: ct, encoding = resp.getHeader('content-type').split('charset=') ct = ct.split(';')[0] # pisa only likes ascii css data = resp.getBody().decode(encoding) except ValueError: ct = resp.getHeader('content-type').split(';')[0] data = resp.getBody() return ct, data
def test_filestream_iterator(self): # Only a ZServerHTTPResponse is IStreamIterator Aware request = getRequest() request.response.__class__ = ZServerHTTPResponse response = subrequest('/@@filestream') from ZPublisher.Iterators import filestream_iterator self.assertTrue(isinstance(response.stdout, filestream_iterator)) self.assertEqual(response.getBody(), 'Test')
def test_virtual_host_with_root_double_slash(self): url = ("/VirtualHostBase/http/example.org:80/VirtualHostRoot/" "_vh_fizz/folder1/folder2//folder2A") traverse(url) root = self.layer['app'].folder1 response = subrequest('/folder1B/@@url', root=root) self.assertEqual(response.body, 'http://example.org/fizz/folder1/folder1B')
def test_blobstream_iterator(self): # Only a ZServerHTTPResponse is IStreamIterator Aware request = getRequest() request.response.__class__ = ZServerHTTPResponse response = subrequest('/@@blobstream') from ZODB.blob import BlobFile self.assertTrue(isinstance(response.stdout, BlobFile)) self.assertEqual(response.getBody(), 'Hi, Blob!')
def set_theme(obj, event): """ """ # When using /_vh_ subpaths it is safer to use this approach base = '/'.join(obj.getPhysicalPath()) diazo_setter = subrequest( # noqa base + '/@@local-diazo-setter' )
def render(self): context = aq_inner(self.context) plonesites = listPloneSites(context) output = [] for plonesite in plonesites: response = subrequest('/'.join(plonesite.getPhysicalPath()) + '/reinstall_gwcontrolpanel') output.append(response.getBody()) return '\n'.join(output)
def test_virtual_host_space(self): parts = ('folder2', 'folder2A/folder2Ai space/@@url') url = ("/VirtualHostBase/http/example.org:80/%s/VirtualHostRoot/%s" % parts) traverse(url) app = self.layer['app'] response = subrequest('/folder2A/@@url', root=app.folder2) self.assertEqual(response.body, 'http://example.org/folder2A')
def wakeCssResources(self, resources, alreadyDone): byteCount = 0 urlResources = set() cssURLResourcePattern = re.compile(r"url\('?([^')]+)'?\)") # Get css resources and parse for url(...) directives for url in resources: self._lastRequestedURL = url response = subrequest(url) status = response.getStatus() if status == 200: if self.verbose: logger.info("\tGot status %s for resource: %s" % (status, url)) body = response.getBody() byteCount += len(body) # detect if document is CSS if ( url.endswith(".css") or response.getHeader("content-type").startswith("text/css") ) and not url.endswith(".kss"): # get CSS working path workingPath = url.split("/") workingPath.pop() workingPath = self.request.base + "/".join(workingPath) # parse URLs foundURLs = [] for mo in cssURLResourcePattern.finditer(body): groups = mo.groups() if len(groups) > 0: foundURLs.append(groups[0]) urlResources = urlResources.union(self.parseLinks(workingPath, foundURLs)) else: logger.debug("\tNot a CSS document: %s" % url) elif status >= 500: if not self.ignoreResourceServerError: logger.info("\tGot status %s for resource: %s" % (status, url)) raise ServerError() logger.debug("\tGot status %s for resource: %s" % (status, url)) else: logger.debug("\tGot status %s for resource: %s" % (status, url)) # wake those extra resources alreadyDone = alreadyDone.union(resources) urlRresources = urlResources.difference(alreadyDone) logger.debug("\tFound %s resources referenced from CSS" % len(urlResources)) byteCount += self.wakeResources(urlResources) return byteCount
def wakePlone(self, plone): """Pre-caching mechenisim for plones sites. By making sub requests to bring objects into memory""" cssImportPattern = re.compile(r"@import\s+url\(([^)]+)\)\s*;") # Request the front page url_path = "/".join(plone.getPhysicalPath()) self._lastRequestedURL = url_path response = subrequest(url_path) status = int(response.getStatus()) logger.info("Plone Site: %s" % url_path) logger.debug("\tHTTP status: %s" % status) if status >= 400 and status != 401: # Bad news - 4xx (client) and 5xx (server) errors. # With the exception of 401 for unautherized access # which is an acceptable error raise RequestError() # Process output byteCount = 0 body = response.getBody() byteCount += len(body) try: doc = etree.parse(StringIO(body), etree.HTMLParser()) except etree.XMLSyntaxError: logger.debug("\tWarning: XMLSyntaxError on front page") else: links = doc.xpath("/html/body//a/@href") images = doc.xpath("/html/body//img/@src") headLink = doc.xpath("/html//link/@href") scripts = doc.xpath("/html//script/@src") cssImports = [] for mo in cssImportPattern.finditer(body): groups = mo.groups() if len(groups) > 0: cssImports.append(groups[0]) resources = self.parseLinks(url_path, links) resources = resources.union(self.parseLinks(url_path, images)) resources = resources.union(self.parseLinks(url_path, scripts)) logger.debug("\tFound %s sub resources to load." % len(resources)) byteCount += self.wakeResources(resources) cssResources = self.parseLinks(url_path, headLink) cssResources = cssResources.union(self.parseLinks(url_path, cssImports)) cssResources = cssResources.difference(resources) logger.debug("\tFound %s css resources to load." % len(cssResources)) byteCount += self.wakeCssResources(cssResources, alreadyDone=resources) logger.info("Approximate of bytes retrieved for site resources: %s" % len(body))
def test_other_variables(self): request = getRequest() request["foo"] = "bar" request["VIRTUAL_URL"] = "parent" request["URL9"] = "parent" response = subrequest("/folder1/@@parameter") self.assertTrue("'foo'" in response.body) self.assertFalse("'URL9'" in response.body) self.assertFalse("'VIRTUAL_URL'" in response.body)
def test_virtual_host_root_with_root(self): parts = ('folder1', 'folder1A/@@url') url = VH_TPL % parts traverse(url) app = self.layer['app'] response = subrequest('/folder1Ai/@@url', root=app.folder1.folder1A) self.assertEqual( response.body, 'http://example.org/fizz/buzz/fizzbuzz/folder1A/folder1Ai')
def test_other_variables(self): request = getRequest() request['foo'] = 'bar' request['VIRTUAL_URL'] = 'parent' request['URL9'] = 'parent' response = subrequest('/folder1/@@parameter') self.assertTrue(b"'foo'" in response.body) self.assertFalse(b"'URL9'" in response.body) self.assertFalse(b"'VIRTUAL_URL'" in response.body)
def test_virtual_host_root(self): parts = ('folder1', 'folder1A/@@url') url = VH_TPL.format(*parts) traverse(url) response = subrequest('/folder1B/@@url') self.assertEqual( response.body, b'http://nohost/fizz/buzz/fizzbuzz/folder1B' )
def test_virtual_host_root(self): parts = ('folder1', 'folder1A/@@url') url = VH_TPL % parts traverse(url) response = subrequest('/folder1B/@@url') self.assertEqual( response.body, 'http://example.org/fizz/buzz/fizzbuzz/folder1B' )
def render(self): context = aq_inner(self.context) plonesites = listPloneSites(context) output = [] for plonesite in plonesites: response = subrequest('/'.join(plonesite.getPhysicalPath()) + '/reinstall_ulearncontrolpanel') output.append(response.getBody()) return '\n'.join(output)
def test_other_variables(self): request = getRequest() request['foo'] = 'bar' request['VIRTUAL_URL'] = 'parent' request['URL9'] = 'parent' response = subrequest('/folder1/@@parameter') self.assertTrue("'foo'" in response.body) self.assertFalse("'URL9'" in response.body) self.assertFalse("'VIRTUAL_URL'" in response.body)
def test_blobstream_iterator(self): # Only a ZServerHTTPResponse is IStreamIterator Aware from ZServer.HTTPResponse import ZServerHTTPResponse request = getRequest() request.response.__class__ = ZServerHTTPResponse response = subrequest('/@@blobstream') from ZODB.blob import BlobFile self.assertTrue(isinstance(response.stdout, BlobFile)) self.assertEqual(response.getBody(), "Hi, Blob!")
def test_filestream_iterator(self): # Only a ZServerHTTPResponse is IStreamIterator Aware from ZServer.HTTPResponse import ZServerHTTPResponse request = getRequest() request.response.__class__ = ZServerHTTPResponse response = subrequest('/@@filestream') from ZPublisher.Iterators import filestream_iterator self.assertTrue(isinstance(response.stdout, filestream_iterator)) self.assertEqual(response.getBody(), "Test")
def test_virtual_host_root_with_root(self): parts = ('folder1', 'folder1A/@@url') url = VH_TPL.format(*parts) traverse(url) app = self.layer['app'] response = subrequest('/folder1Ai/@@url', root=app.folder1.folder1A) self.assertEqual( response.body, b'http://nohost/fizz/buzz/fizzbuzz/folder1A/folder1Ai' )
def test_dont_minify_already_minified(self): registry = getUtility(IRegistry) bundles = registry.collectionOfInterface(IBundleRegistry, prefix="plone.bundles") bundle = bundles.add('foobar') bundle.jscompilation = '++plone++static/foobar-compiled.js' bundle.csscompilation = '++plone++static/foobar-compiled.css' resources = registry.collectionOfInterface(IResourceRegistry, prefix="plone.resources") resource = resources.add('foobar') resource.js = '++plone++static/foobar.min.js' resource.css = ['++plone++static/foobar.min.css'] bundle.resources = ['foobar'] persistent_directory = getUtility( IResourceDirectory, name="persistent") if OVERRIDE_RESOURCE_DIRECTORY_NAME not in persistent_directory: persistent_directory.makeDirectory( OVERRIDE_RESOURCE_DIRECTORY_NAME) container = persistent_directory[OVERRIDE_RESOURCE_DIRECTORY_NAME] container.makeDirectory('static') directory = container['static'] directory.writeFile('foobar.min.js', 'alert("Hi!");\n\nalert("Ho!");') directory.writeFile('foobar.min.css', 'body {\ncolor: blue;\n}') cookWhenChangingSettings(self.portal, bundle) resp_js = subrequest( '{0}/++plone++static/foobar-compiled.js'.format( self.portal.absolute_url() ) ) self.assertTrue('alert("Hi!");\n\nalert("Ho!");' in resp_js.getBody()) resp_css = subrequest( '{0}/++plone++static/foobar-compiled.css'.format( self.portal.absolute_url() ) ) self.assertTrue('body {\ncolor: blue;\n}' in resp_css.getBody())
def test_virtual_host_root_at_root_trailing(self): url = ( '/VirtualHostBase/http/nohost:80/folder1/VirtualHostRoot/' '_vh_fizz/_vh_buzz/_vh_fizzbuzz/' ) traverse(url) response = subrequest('/folder1B/@@url') self.assertEqual( response.body, b'http://nohost/fizz/buzz/fizzbuzz/folder1B' )
def _inlineImages(qn, xmlString): parser = etree.HTMLParser() xml = etree.parse(StringIO("<div>" + xmlString + "</div>"), parser) for n in xml.xpath('//img'): imgData = None if n.get('src').startswith('/'): imgData = subrequest(n.get('src')).getBody() else: import urllib2 response = urllib2.urlopen(n.get('src')) imgData = response.read() if imgData: n.set( 'src', "data:image/png;base64," + imgData.encode("base64").replace("\n", "")) return etree.tostring(xml.xpath('/html/body/div')[0])
def getFrame(self): theme_name = self.context.__name__ theme = None if self.context.isFile(MANIFEST_FILENAME): manifest = self.context.openFile(MANIFEST_FILENAME) theme = getManifest(manifest, MANIFEST_FORMAT, None) theme['name'] = theme_name manifest.close() theme = getTheme(theme_name, theme) if theme.rules and theme.rules.endswith('.xml'): return super(ThemeMapper, self).getFrame() # we're assuming it is a castle type theme self.request.environ[theming.OVERRIDE_ENVIRON_KEY] = theme_name response = subrequest('/', root=api.portal.get()) result = response.getBody() return result
def url_fetcher(self, url): """Fetches internal URLs by path and not via an external request. N.B. Multiple calls to this method might exhaust the available threads of the server, which causes a hanging instance. """ if url.startswith("data"): logger.info("Data URL, delegate to default URL fetcher...") return default_url_fetcher(url) logger.info("Fetching URL '{}' for WeasyPrint".format(url)) # get the pyhsical path from the URL request = api.get_request() host = request.get_header("HOST") path = "/".join(request.physicalPathFromURL(url)) # fetch the object by sub-request portal = api.get_portal() context = portal.restrictedTraverse(path, None) if context is None or host not in url: logger.info("External URL, delegate to default URL fetcher...") return default_url_fetcher(url) logger.info("Local URL, fetching data by path '{}'".format(path)) # get the data via an authenticated subrequest response = subrequest(path) # Prepare the return data as required by WeasyPrint string = response.getBody() filename = url.split("/")[-1] mime_type = mimetypes.guess_type(url)[0] redirected_url = url return { "string": string, "filename": filename, "mime_type": mime_type, "redirected_url": redirected_url, }
def process_task_queue(self): queue = self._task_queue.queue self.log.info('Processing %d task queue jobs...' % queue.qsize()) request = getRequest() alsoProvides(request, ITaskQueueLayer) while not queue.empty(): job = queue.get() # Process job using plone.subrequest response = subrequest(job['url']) assert response.status == 200 # XXX: We don't currently handle the user that is supposed to be # authenticated, and the task ID, both of which c.taskqueue # provides in the job. noLongerProvides(request, ITaskQueueLayer) self.log.info('All task queue jobs processed.')
def test_cooking_missing(self): registry = getUtility(IRegistry) bundles = registry.collectionOfInterface(IBundleRegistry, prefix="plone.bundles") bundle = bundles.add('foobar') bundle.jscompilation = '++plone++static/foobar-compiled.js' bundle.csscompilation = '++plone++static/foobar-compiled.css' resources = registry.collectionOfInterface(IResourceRegistry, prefix="plone.resources") resource = resources.add('foobar') resource.js = '++plone++static/foobar.js' bundle.resources = ['foobar'] cookWhenChangingSettings(self.portal, bundle) resp = subrequest( '%s/++plone++static/foobar-compiled.js' % self.portal.absolute_url()) self.assertTrue('Could not find resource' in resp.getBody())
def render(self): if CSRF: alsoProvides(self.request, IDisableCSRFProtection) context = aq_inner(self.context) args = self.request.form view_name = self.request.form['view'] exclude_sites = self.request.form.get('exclude_sites', '').split(',') plonesites = listPloneSites(context) output = [] for plonesite in plonesites: if plonesite.id not in exclude_sites: print('======================') print('Executing view in {}'.format(plonesite.id)) print('======================') quoted_args = urllib.urlencode(args) response = subrequest('/'.join(plonesite.getPhysicalPath()) + '/{}?{}'.format(view_name, quoted_args)) output.append( """<br/>-- Executed view {} in site {} --""".format( view_name, plonesite.id)) output.append(response.getBody()) return '\n'.join(output)
# REQUIRE JS CONFIGURATION paths = {} shims = {} for requirejs, script in resources.items(): if script.js: # Main resource js file resource_file = portal.unrestrictedTraverse(script.js, None) src = None if resource_file: local_file = resource_to_dir(resource_file) else: # In case is not found on traverse we dump it from request to file response = subrequest(portal.absolute_url() + '/' + script.js) local_file = None if response.status == 200: js_body = response.getBody() file_name = uuid.uuid4().hex try: os.mkdir(temp_resource_folder) except OSError: pass local_file = temp_resource_folder + '/' + file_name + '.js' if isinstance(js_body, six.text_type): js_body = js_body.encode('utf-8') with open(local_file, 'w') as temp_file: temp_file.write(js_body) if local_file:
def get_snippet_html(self, url): return subrequest(url).getBody()