def _inject_views_into_page(self, _page): # this is a good proxy to a version of CKAN with views enabled. if not p.plugin_loaded('image_view'): return try: import lxml import lxml.html except ImportError: return try: root = lxml.html.fromstring(_page['content']) # Return if any errors are found while parsing the content except (lxml.etree.XMLSyntaxError, lxml.etree.ParserError): return for element in root.findall('.//iframe'): embed_element = element.attrib.pop('data-ckan-view-embed', None) if not embed_element: continue element.tag = 'div' error = None try: iframe_src = element.attrib.pop('src', '') width = element.attrib.pop('width', '80') if not width.endswith('%') and not width.endswith('px'): width = width + 'px' height = element.attrib.pop('height', '80') if not height.endswith('%') and not height.endswith('px'): height = height + 'px' align = element.attrib.pop('align', 'none') style = "width: %s; height: %s; float: %s; overflow: auto; vertical-align:middle; position:relative" % (width, height, align) element.attrib['style'] = style element.attrib['class'] = 'pages-embed' view = p.toolkit.get_action('resource_view_show')({}, {'id': iframe_src[-36:]}) context = {} resource = p.toolkit.get_action('resource_show')(context, {'id': view['resource_id']}) package_id = context['resource'].resource_group.package_id package = p.toolkit.get_action('package_show')(context, {'id': package_id}) except p.toolkit.ObjectNotFound: error = _('ERROR: View not found {view_id}'.format(view_id=iframe_src )) if error: resource_view_html = '<h4> %s </h4>' % error elif not helpers.resource_view_is_iframed(view): resource_view_html = helpers.rendered_resource_view(view, resource, package) else: src = helpers.url_for(qualified=True, controller='package', action='resource_view', id=package['name'], resource_id=resource['id'], view_id=view['id']) message = _('Your browser does not support iframes.') resource_view_html = '<iframe src="{src}" frameborder="0" width="100%" height="100%" style="display:block"> <p>{message}</p> </iframe>'.format(src=src, message=message) view_element = lxml.html.fromstring(resource_view_html) element.append(view_element) _page['content'] = lxml.html.tostring(root)
def ozwillo_theme_get_map(view_id, resource_id, package_id): resource_view = None try: resource_view = logic.get_action('resource_view_show')({}, {'id': view_id}) except (): return 'View not found' resource = logic.get_action('resource_show')({}, {'id': resource_id}) package = logic.get_action('package_show')({}, {'id': package_id}) return h.rendered_resource_view(resource_view, resource, package, True)
def view(package_type, id, resource_id, view_id=None): """ Embedded page for a resource view. Depending on the type, different views are loaded. This could be an img tag where the image is loaded directly or an iframe that embeds a webpage or a recline preview. """ context = { u'model': model, u'session': model.Session, u'user': g.user, u'auth_user_obj': g.userobj } try: package = get_action(u'package_show')(context, {u'id': id}) except (NotFound, NotAuthorized): return base.abort(404, _(u'Dataset not found')) try: resource = get_action(u'resource_show')(context, {u'id': resource_id}) except (NotFound, NotAuthorized): return base.abort(404, _(u'Resource not found')) view = None if request.params.get(u'resource_view', u''): try: view = json.loads(request.params.get(u'resource_view', u'')) except ValueError: return base.abort(409, _(u'Bad resource view data')) elif view_id: try: view = get_action(u'resource_view_show')(context, {u'id': view_id}) except (NotFound, NotAuthorized): return base.abort(404, _(u'Resource view not found')) if not view or not isinstance(view, dict): return base.abort(404, _(u'Resource view not supplied')) return h.rendered_resource_view(view, resource, package, embed=True)
def view(package_type, id, resource_id, view_id=None): """ Embedded page for a resource view. Depending on the type, different views are loaded. This could be an img tag where the image is loaded directly or an iframe that embeds a webpage or a recline preview. """ context = { u'model': model, u'session': model.Session, u'user': g.user, u'auth_user_obj': g.userobj } try: package = get_action(u'package_show')(context, {u'id': id}) except (NotFound, NotAuthorized): return base.abort(404, _(u'Dataset not found')) try: resource = get_action(u'resource_show')(context, {u'id': resource_id}) except (NotFound, NotAuthorized): return base.abort(404, _(u'Resource not found')) view = None if request.params.get(u'resource_view', u''): try: view = json.loads(request.params.get(u'resource_view', u'')) except ValueError: return base.abort(409, _(u'Bad resource view data')) elif view_id: try: view = get_action(u'resource_view_show')(context, {u'id': view_id}) except (NotFound, NotAuthorized): return base.abort(404, _(u'Resource view not found')) if not view or not isinstance(view, dict): return base.abort(404, _(u'Resource view not supplied')) return h.rendered_resource_view(view, resource, package, embed=True)
def _inject_views_into_page(self, _page): # this is a good proxy to a version of CKAN with views enabled. if not p.plugin_loaded('image_view'): return try: import lxml import lxml.html except ImportError: return try: root = lxml.html.fromstring(_page['content']) # Return if any errors are found while parsing the content except (lxml.etree.XMLSyntaxError, lxml.etree.ParserError): return for element in root.findall('.//iframe'): embed_element = element.attrib.pop('data-ckan-view-embed', None) if not embed_element: continue element.tag = 'div' error = None try: iframe_src = element.attrib.pop('src', '') width = element.attrib.pop('width', '80') if not width.endswith('%') and not width.endswith('px'): width = width + 'px' height = element.attrib.pop('height', '80') if not height.endswith('%') and not height.endswith('px'): height = height + 'px' align = element.attrib.pop('align', 'none') style = "width: %s; height: %s; float: %s; overflow: auto; vertical-align:middle; position:relative" % (width, height, align) element.attrib['style'] = style element.attrib['class'] = 'pages-embed' view = p.toolkit.get_action('resource_view_show')({}, {'id': iframe_src[-36:]}) context = {} resource = p.toolkit.get_action('resource_show')(context, {'id': view['resource_id']}) package_id = context['resource'].resource_group.package_id package = p.toolkit.get_action('package_show')(context, {'id': package_id}) except p.toolkit.ObjectNotFound: error = _('ERROR: View not found {view_id}'.format(view_id=iframe_src )) if error: resource_view_html = '<h4> %s </h4>' % error elif not helpers.resource_view_is_iframed(view): resource_view_html = helpers.rendered_resource_view(view, resource, package) else: src = helpers.url_for(qualified=True, controller='package', action='resource_view', id=package['name'], resource_id=resource['id'], view_id=view['id']) message = _('Your browser does not support iframes.') resource_view_html = '<iframe src="{src}" frameborder="0" width="100%" height="100%" style="display:block"> <p>{message}</p> </iframe>'.format(src=src, message=message) view_element = lxml.html.fromstring(resource_view_html) element.append(view_element) new_content = lxml.html.tostring(root) if new_content.startswith('<div>') and new_content.endswith('</div>'): # lxml will add a <div> tag to text that starts with an HTML tag, # which will cause the rendering to fail new_content = new_content[5:-6] elif new_content.startswith('<p>') and new_content.endswith('</p>'): # lxml will add a <p> tag to plain text snippet, which will cause the # rendering to fail new_content = new_content[3:-4] _page['content'] = new_content
def resource_view(self, id, resource_id, view_id=None): # custom_base.g_analitics() ''' Embedded page for a resource view. Depending on the type, different views are loaded. This could be an img tag where the image is loaded directly or an iframe that embeds a webpage or a recline preview. ''' context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj} try: package = get_action('package_show')(context, {'id': id}) except NotFound: abort(404, _('Dataset not found')) except NotAuthorized: abort(401, _('Unauthorized to read dataset %s') % id) try: resource = get_action('resource_show')( context, {'id': resource_id}) # removes the host to make it relative if config.get('ckan.upload_file_url'): url = resource['url'] if config.get('ckan.upload_file_url') in url: url = url.split(config.get('ckan.upload_file_url')) resource['url'] = url[1] except NotFound: abort(404, _('Resource not found')) except NotAuthorized: abort(401, _('Unauthorized to read resource %s') % resource_id) view = None if request.params.get('resource_view', ''): try: view = json.loads(request.params.get('resource_view', '')) except ValueError: abort(409, _('Bad resource view data')) elif view_id: try: view = get_action('resource_view_show')( context, {'id': view_id}) except NotFound: abort(404, _('Resource view not found')) except NotAuthorized: abort(401, _('Unauthorized to read resource view %s') % view_id) if not view or not isinstance(view, dict): abort(404, _('Resource view not supplied')) analytics_helpers.update_analytics_code_by_organization(package['organization']['id']) return h.rendered_resource_view(view, resource, package, embed=True)
def resource_view(self, id, resource_id, view_id=None): custom_base.g_analitics() ''' Embedded page for a resource view. Depending on the type, different views are loaded. This could be an img tag where the image is loaded directly or an iframe that embeds a webpage or a recline preview. ''' context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj} try: package = get_action('package_show')(context, {'id': id}) except NotFound: abort(404, _('Dataset not found')) except NotAuthorized: abort(401, _('Unauthorized to read dataset %s') % id) try: resource = get_action('resource_show')( context, {'id': resource_id}) # removes the host to make it relative if config.get('ckan.upload_file_url'): url = resource['url'] if config.get('ckan.upload_file_url') in url: url = url.split(config.get('ckan.upload_file_url')) resource['url'] = url[1] except NotFound: abort(404, _('Resource not found')) except NotAuthorized: abort(401, _('Unauthorized to read resource %s') % resource_id) view = None if request.params.get('resource_view', ''): try: view = json.loads(request.params.get('resource_view', '')) except ValueError: abort(409, _('Bad resource view data')) elif view_id: try: view = get_action('resource_view_show')( context, {'id': view_id}) except NotFound: abort(404, _('Resource view not found')) except NotAuthorized: abort(401, _('Unauthorized to read resource view %s') % view_id) if not view or not isinstance(view, dict): abort(404, _('Resource view not supplied')) return h.rendered_resource_view(view, resource, package, embed=True)