def test_render_string(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render({'field': 'astring'}, 'application/xml') self.assertXMLContains(content, '<field>astring</field>')
def test_render_float(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render({"field": 123.4}, "application/xml") self.assertXMLContains(content, "<field>123.4</field>")
def test_render_none(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render({"field": None}, "application/xml") self.assertXMLContains(content, "<field></field>")
def test_render_float(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render({'field': 123.4}, 'application/xml') self.assertXMLContains(content, '<field>123.4</field>')
def test_render_none(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render({'field': None}, 'application/xml') self.assertXMLContains(content, '<field></field>')
def test_render_decimal(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render({'field': Decimal('111.2')}, 'application/xml') self.assertXMLContains(content, '<field>111.2</field>')
class XMLGenerator(BaseGenerator): def __init__(self, pretty_print: bool = False): self.pretty_print = pretty_print self.renderer = XMLRenderer() self.stream = StringIO() self.xml = SimplerXMLGenerator(self.stream, XMLRenderer.charset) def start(self): self.xml.startDocument() self.xml.startElement(XMLRenderer.root_tag_name, {}) def add_list_item(self, data: dict): self.xml.startElement(XMLRenderer.item_tag_name, {}) self.renderer._to_xml(self.xml, data) self.xml.endElement(XMLRenderer.item_tag_name) def finish(self): self.xml.endElement(XMLRenderer.root_tag_name) self.xml.endDocument() def get_data(self): if not self.pretty_print: return self.stream.getvalue() else: tree = etree.XML(self.stream.getvalue().encode('utf-8')) string = etree.tostring(tree, encoding='utf-8', pretty_print=True, xml_declaration=True) return string.decode('utf-8')
def render(self, data, accepted_media_type=None, renderer_context=None): """ First renders `data` into serialized XML, afterwards start fop and return PDF """ if data is None: return '' super(XSLFORenderer, self).render(data, accepted_media_type, renderer_context) xml_renderer = XMLRenderer() xml_string = xml_renderer.render(data, accepted_media_type, renderer_context) xml_file = open(renderer_context['view'].file_name, "wb+") xml_file.truncate() xml_file.write(xml_string.encode('utf-8')) xml_file.close() # perform xsl transformation self.perform_xsl_transformation(xml_file, self.xsl_file, self.fop_config_file, self.file_output_pdf) # Read file with open(self.file_output_pdf, 'rb') as f: rendered_output = f.read() return rendered_output
def render(self, data, accepted_media_type=None, renderer_context=None): """ First renders `data` into serialized XML, afterwards start fop and return PDF """ if data is None: return '' super(XSLFORenderer, self).render(data, accepted_media_type, renderer_context) xml_renderer = XMLRenderer() xml_string = xml_renderer.render(data, accepted_media_type, renderer_context) xml_file = open(renderer_context['view'].file_name, "wb+") xml_file.truncate() xml_file.write(xml_string.encode('utf-8')) xml_file.close() # perform xsl transformation self.perform_xsl_transformation(xml_file, self.xsl_file, self.fop_config_file, self.file_output_pdf) # Read file with open(self.file_output_pdf, 'rb') as f: rendered_output = f.read() return rendered_output
def test_render_string(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render({"field": "astring"}, "application/xml") self.assertXMLContains(content, "<field>astring</field>")
def test_render_complex_data(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render(self._complex_data, 'application/xml') self.assertXMLContains(content, '<sub_name>first</sub_name>') self.assertXMLContains(content, '<sub_name>second</sub_name>')
def test_render_decimal(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render( {"field": Decimal("111.2")}, "application/xml" ) self.assertXMLContains(content, "<field>111.2</field>")
def test_render_datetime(self): """ Test XML rendering. """ renderer = XMLRenderer() content = renderer.render({ 'field': datetime.datetime(2011, 12, 25, 12, 45, 00) }, 'application/xml') self.assertXMLContains(content, '<field>2011-12-25 12:45:00</field>')
def test_render_and_parse_complex_data(self): """ Test XML rendering. """ renderer = XMLRenderer() content = StringIO(renderer.render(self._complex_data, 'application/xml')) parser = XMLParser() complex_data_out = parser.parse(content) error_msg = "complex data differs!IN:\n %s \n\n OUT:\n %s" % (repr(self._complex_data), repr(complex_data_out)) self.assertEqual(self._complex_data, complex_data_out, error_msg)
def get(self, request, *args, **kwargs): conf_ids = json.loads(kwargs["hash"]) logger.info("got download request for {}: {}".format( logging_tools.get_plural("config", len(conf_ids)), ", ".join(["{:d}".format(val) for val in sorted(conf_ids)]))) configs = [] # res_xml.append(configs) conf_list = getattr(config, "objects").filter( Q(pk__in=conf_ids)).prefetch_related( "config_str_set", "config_int_set", "config_bool_set", "config_blob_set", "mon_check_command_set", "config_script_set", "categories", ) for cur_conf in conf_list: configs.append(config_dump_serializer(cur_conf).data) xml_tree = etree.fromstring(XMLRenderer().render(configs)) # remove all idxs and parent_configs for pk_el in xml_tree.xpath( ".//idx|.//parent_config|.//categories|.//date", smart_strings=False): pk_el.getparent().remove(pk_el) act_resp = HttpResponse(etree.tostring(xml_tree, pretty_print=True), content_type="application/xml") act_resp[ "Content-disposition"] = "attachment; filename=config_{}.xml".format( datetime.datetime.now().strftime("%Y%m%d_%H%M%S")) return act_resp
def post( self, request, *args, **kwargs, ): try: output_format = str(args[0]) print(output_format) except IndexError: output_format = 'json' serializer = EstimatorSerializer(data=request.data) if serializer.is_valid(): estimator_response = estimator(request.data) if output_format == 'json': return Response(estimator_response, status=status.HTTP_200_OK, content_type='application/json') elif output_format == 'xml': rendered_data = XMLRenderer().render(estimator_response) return Response(rendered_data, status=status.HTTP_200_OK, content_type='text/xml') else: raise Http404('Unsupported format') else: return Response({}, status=status.HTTP_200_OK, content_type='application/json')
def get_context_data(self, *, object=None, **kwargs): context = super(MovieDetailXml, self).get_context_data(object=object, **kwargs) context.update({ 'serialized_data': XMLRenderer().render(MovieSerializer(self.get_object()).data) }) return context
def render(self, data, accepted_media_type=None, renderer_context=None): renderer_context = renderer_context or {} response = renderer_context['response'] if response and response.exception: return XMLRenderer().render(data, accepted_media_type, renderer_context) return super().render(data, accepted_media_type, renderer_context)
def save(self, user): logger.debug('saving data') logger.debug('data ->' + str(self.data)) logger.debug('validated data ->' + str(self.validated_data)) logger.debug('fields ->' + str(self.fields)) logger.debug('errors ->' + str(self.errors)) logger.debug('rendering XML') xml = XMLRenderer().render(self.validated_data) logger.debug('calling trigger handler') id = trigger_handler.handle_trigger(user, xml) return id
def get_renderer(self): """ Cf from rest_framework.renderers import JSONRenderer """ if ROA_FORMAT == 'json': return JSONRenderer() elif ROA_FORMAT == 'xml': return XMLRenderer() elif ROAException == 'yaml': return YAMLRenderer() else: raise NotImplementedError
def render(self, data, accepted_media_type=None, renderer_context=None): """ In case a request is called on /api/v1/, we now redirect the call to DRF instead of Tastypie. The differences between DRF and Tastypie are: - Different names and values for several fields (see _rename_fields). On paginated results: - An additional 'meta' object containing the limit, next, previous and total_count; - Next and previous links do not show the domain; - The root tag name is 'response'; - The tag name of the results is 'objects'; - The tag name for each result is 'object'. On non-paginated results: - The tag name of the result is 'object'. """ request = renderer_context.get('request') if '/api/v1/' in request.path: if data is None: return '' stream = StringIO() xml = SimplerXMLGenerator(stream, self.charset) xml.startDocument() if all(k in data.keys() for k in ['count', 'next', 'previous', 'offset', 'results']): # Paginated result xml.startElement("response", {}) data = _convert_data_for_tastypie(request, renderer_context['view'], data) self._to_xml(xml, data) xml.endElement("response") else: # Non-paginated result xml.startElement("object", {}) self._to_xml(xml, _rename_fields([data])[0]) xml.endElement("object") xml.endDocument() return stream.getvalue() elif isinstance(data, dict) and 'offset' in data.keys(): data.pop('offset') return XMLRenderer().render(data, accepted_media_type, renderer_context)
def _get_repo_list(self, srv_com): repo_list = package_repo.objects.filter(Q(publish_to_nodes=True)) send_ok = [ cur_repo for cur_repo in repo_list if cur_repo.distributable ] self.log("{}, {:d} to send".format( logging_tools.get_plural("publish repo", len(repo_list)), len(send_ok), )) if self.__client_gen == 1: resp = etree.fromstring(XMLRenderer().render( package_repo_serializer(send_ok, many=True).data)) # @UndefinedVariable else: resp = srv_com.builder( "repos", *[cur_repo.get_xml() for cur_repo in send_ok]) srv_com["repo_list"] = resp
def test_render_lazy(self): renderer = XMLRenderer() lazy = gettext_lazy('hello') content = renderer.render({'field': lazy}, 'application/xml') self.assertXMLContains(content, '<field>hello</field>')
def __init__(self, pretty_print: bool = False): self.pretty_print = pretty_print self.renderer = XMLRenderer() self.stream = StringIO() self.xml = SimplerXMLGenerator(self.stream, XMLRenderer.charset)
def test_render_lazy(self): renderer = XMLRenderer() lazy = gettext_lazy('hello') content = renderer.render({'field': lazy}, 'application/xml') self.assertXMLContains(content, '<field>hello</field>')
def get_renderer(self): if self.file_format == 'xml': return XMLRenderer() if self.file_format == 'json': pass raise Exception('Undefined format type')
def _get_package_list(self, srv_com): _kernels = self.device.kerneldevicehistory_set.all() if _kernels.count(): cur_kernel = _kernels[0].kernel else: cur_kernel = None _images = self.device.imagedevicehistory_set.all() if _images.count(): cur_image = _images[0].image else: cur_image = None pdc_list = package_device_connection.objects.filter( Q(device=self.device) ).prefetch_related( "kernel_list", "image_list" ).select_related( "package", "package__target_repo" ) # send to client send_list = [] # pre-delete list pre_delete_list = [] for cur_pdc in pdc_list: take = True pre_delete = False if cur_pdc.image_dep: if cur_image not in cur_pdc.image_list.all(): self.log( "appending package '{}' to pre-delete list because image '{}' not in image_list '{}'".format( str(cur_pdc.package), str(cur_image), ", ".join([str(_v) for _v in cur_pdc.image_list.all()]), ) ) pre_delete = True take = False if cur_pdc.kernel_dep: if cur_kernel not in cur_pdc.kernel_list.all(): self.log( "appending package '{}' to pre-delete list because kernel '{}' not in kernel_list '{}'".format( str(cur_pdc.package), str(cur_kernel), ", ".join([str(_v) for _v in cur_pdc.kernel_list.all()]), ) ) pre_delete = True take = False if pre_delete: pre_delete_list.append(cur_pdc) if take: send_list.append(cur_pdc) self.log( "{} in source list, {} in send_list, {} in pre-delete list".format( logging_tools.get_plural("package", len(pdc_list)), logging_tools.get_plural("package", len(send_list)), logging_tools.get_plural("package", len(pre_delete_list)), ) ) if self.__client_gen == 1: # new generation _pre_del_xml = etree.fromstring( XMLRenderer().render( package_device_connection_wp_serializer(pre_delete_list, many=True).data ).encode("utf-8") ) resp = etree.fromstring( XMLRenderer().render( package_device_connection_wp_serializer(send_list, many=True).data ).encode("utf-8") ) for _entry in resp: _entry.append(E.pre_delete("False")) if len(_pre_del_xml): for _entry in _pre_del_xml: _entry.append(E.pre_delete("True")) # insert at top of the list resp.insert(0, _entry) else: resp = srv_com.builder( "packages", # we don't support pre_delete *[cur_pdc.get_xml(with_package=True) for cur_pdc in send_list] ) srv_com["package_list"] = resp
def parse_xml(self, schema): return self.prettify_xml_example(XMLRenderer().render( self.schema_to_example(self.resolve_schema(schema))))
def __init__(self, data, **kwargs): content = XMLRenderer().render(data) kwargs['content_type'] = 'application/xml' super(XMLResponse, self).__init__(content, **kwargs)
def get_context_data(self, **kwargs): context = super(ApiMovieListXML, self).get_context_data() movies_serializer = MovieSerializer(self.get_queryset(), many=True) context['xml'] = XMLRenderer().render(movies_serializer.data) return context
def test_render_list(self): renderer = XMLRenderer() content = renderer.render(self._complex_data, 'application/xml') self.assertXMLContains(content, '<sub_data_list><list-item>') self.assertXMLContains(content, '</list-item></sub_data_list>')
def test_render_lazy(self): renderer = XMLRenderer() lazy = gettext_lazy("hello") content = renderer.render({"field": lazy}, "application/xml") self.assertXMLContains(content, "<field>hello</field>")
def test_render_list(self): renderer = XMLRenderer() content = renderer.render(self._complex_data, "application/xml") self.assertXMLContains(content, "<sub_data_list><list-item>") self.assertXMLContains(content, "</list-item></sub_data_list>")