Example #1
0
    def wrap_request(self, srvReq, message_ref, dt, user_params):
        # ~ xg.set_default_namespace(None)
        any = etree.tostring(srvReq)
        serviceRequest = SSDN.ServiceRequest(
            SSDN.ServiceId(self.service_id), SSDN.Version(self.service_version), etree.XML(any)
        )

        context = SSDN.RequestContext(
            common.authorized_user(**user_params),
            SSDN.Message(SSDN.Reference(message_ref), SSDN.TimeRequest(format(dt))),
        )
        # ~ xg.set_default_namespace(SSDN)
        elem = SSDN.SSDNRequest(context, serviceRequest)
        # ~ elem.nsmap={None:self._url}
        # ~ elem = WSC.soap_request(etree.tostring(elem))
        if True:
            return elem
        else:
            elem = WSC.soap_request(etree.tostring(elem))
        # ~ xmlString = """<?xml version="1.0" encoding="utf-8"?>""" +
        return elem
Example #2
0
    def wrap_request(self, srvReq, message_ref, dt, user_params):
        #~ xg.set_default_namespace(None)
        any = etree.tostring(srvReq)
        serviceRequest = SSDN.ServiceRequest(
            SSDN.ServiceId(self.service_id),
            SSDN.Version(self.service_version), etree.XML(any))

        context = SSDN.RequestContext(
            common.authorized_user(**user_params),
            SSDN.Message(SSDN.Reference(message_ref),
                         SSDN.TimeRequest(format(dt))))
        #~ xg.set_default_namespace(SSDN)
        elem = SSDN.SSDNRequest(context, serviceRequest)
        #~ elem.nsmap={None:self._url}
        #~ elem = WSC.soap_request(etree.tostring(elem))
        if True:
            return elem
        else:
            elem = WSC.soap_request(etree.tostring(elem))
        #~ xmlString = """<?xml version="1.0" encoding="utf-8"?>""" +
        return elem
Example #3
0
    def execute(self, req, unique_id=None, dt=None):
        # ~ print 20120302
        # ~ if user_params is None:
        # ~ raise Warning(
        # ~ "Not actually sending because user_params is empty.")
        # ~ self.validate_against_xsd(req)
        from django.conf import settings

        env = settings.SITE.cbss_environment
        user_params = settings.SITE.cbss_user_params

        req = self.wrap_request(req, unique_id, dt, user_params)

        # ~ xml = etree.tostring(req,xml_declaration=True)
        xml = etree.tostring(req)

        if not env:
            raise Warning(
                """\
Not actually sending because environment is empty. Request would be:
"""
                + prettify(xml)
            )

        # ~ assert env in (ENV_TEST, ENV_ACPT, ENV_PROD)
        assert env in CBSS_ENVS

        url = self.get_url(env)

        if True:
            # ~ x = etree.tostring(elem)
            from suds.client import Client

            url += "?WSDL"
            client = Client(url)
            # ~ print 20120507, url
            # ~ print client
            res = client.service.sendXML(xml)
        else:

            if isinstance(self, NewStyleService):
                server = Resource(url, measure=True, **user_params)
            else:
                server = Resource(url, measure=True)
            res = server.soap(xml)
        return res
Example #4
0
    def execute(self, req, unique_id=None, dt=None):
        #~ print 20120302
        #~ if user_params is None:
        #~ raise Warning(
        #~ "Not actually sending because user_params is empty.")
        #~ self.validate_against_xsd(req)
        from django.conf import settings
        env = settings.SITE.cbss_environment
        user_params = settings.SITE.cbss_user_params

        req = self.wrap_request(req, unique_id, dt, user_params)

        #~ xml = etree.tostring(req,xml_declaration=True)
        xml = etree.tostring(req)

        if not env:
            raise Warning("""\
Not actually sending because environment is empty. Request would be:
""" + prettify(xml))

        #~ assert env in (ENV_TEST, ENV_ACPT, ENV_PROD)
        assert env in CBSS_ENVS

        url = self.get_url(env)

        if True:
            #~ x = etree.tostring(elem)
            from suds.client import Client
            url += '?WSDL'
            client = Client(url)
            #~ print 20120507, url
            #~ print client
            res = client.service.sendXML(xml)
        else:

            if isinstance(self, NewStyleService):
                server = Resource(url, measure=True, **user_params)
            else:
                server = Resource(url, measure=True)
            res = server.soap(xml)
        return res
Example #5
0
File: memo.py Project: cuchac/lino
 def format_value(self, v):
     if etree.iselement(v):
         return unicode(etree.tostring(v))
     return unicode(v)
Example #6
0
def pretty_print(elem):
    """Return a pretty-printed XML string for the Element.
    """
    return prettify(etree.tostring(elem, 'utf-8'))
Example #7
0
def pretty_print(elem):
    """Return a pretty-printed XML string for the Element.
    """
    return prettify(etree.tostring(elem, "utf-8"))
Example #8
0
def py2js(v):
    """
    Note that None values are rendered as ``null`` (not ``undefined``.
    """
    #~ assert _for_user_profile is not None
    #~ logger.debug("py2js(%r)",v)
    for cv in CONVERTERS:
        v = cv(v)

    #~ if isinstance(v,LanguageInfo):
        #~ return v.django_code

    if isinstance(v, Value):
        return v.as_ext()
        #~ v = v.as_ext()
        #~ if not isinstance(v, basestring):
            #~ raise Exception("20120121b %r is of type %s" % (v,type(v)))
        #~ return v
    if isinstance(v, Promise):
        #~ v = force_unicode(v)
        return json.dumps(force_unicode(v))

    if isinstance(v, types.GeneratorType):
        return "".join([py2js(x) for x in v])
    if etree.iselement(v):
    #~ if isinstance(v,etree.Element):
        return json.dumps(etree.tostring(v))

    #~ if type(v) is types.GeneratorType:
        #~ raise Exception("Please don't call the generator function yourself")
        #~ return "\n".join([ln for ln in v])
    if callable(v):
        #~ print 20120114, repr(v)
        #~ raise Exception("Please call the function yourself")
        return "\n".join([ln for ln in v()])
    if isinstance(v, js_code):
        return str(v.s)  # v.s might be a unicode
    if v is None:
        #~ return 'undefined'
        return 'null'
    if isinstance(v, (list, tuple)):  # (types.ListType, types.TupleType):
        #~ return "[ %s ]" % ", ".join([py2js(x) for x in v])
        elems = [py2js(x) for x in v
                 if (not isinstance(x, VisibleComponent)) or x.get_view_permission(_for_user_profile)]
        return "[ %s ]" % ", ".join(elems)
    if isinstance(v, dict):  # ) is types.DictType:
        #~ print 20100226, repr(v)
        return "{ %s }" % ", ".join([
            "%s: %s" % (py2js(k), py2js(v)) for k, v in v.items()
            if (not isinstance(v, VisibleComponent)) or v.get_view_permission(_for_user_profile)
        ])
            #~ "%s: %s" % (k,py2js(v)) for k,v in v.items()])
    if isinstance(v, bool):  # types.BooleanType:
        return str(v).lower()
    #~ if isinstance(v,CRL):
        #~ return str(v)
    if isinstance(v, Quantity):
        return '"%s"' % v
    if isinstance(v, (int, long, decimal.Decimal, fractions.Fraction)):
        return str(v)
    if isinstance(v, IncompleteDate):
        return '"%s"' % v.strftime(settings.SITE.date_format_strftime)
        #~ return '"%s"' % v
    if isinstance(v, datetime.datetime):
        #~ """20120120"""
        return '"%s"' % v.strftime(settings.SITE.datetime_format_strftime)
        #~ return '"%s"' % v.strftime('%Y-%m-%d %H:%M:%S')
    if isinstance(v, datetime.time):
        return '"%s"' % v.strftime(settings.SITE.time_format_strftime)
    if isinstance(v, datetime.date):
        if v.year < 1900:
            v = IncompleteDate(v)
            return '"%s"' % v.strftime(settings.SITE.date_format_strftime)
        return '"%s"' % v.strftime(settings.SITE.date_format_strftime)
        #~ return 'new Date(%d,%d,%d)' % (v.year,v.month-1,v.day)
        #~ return repr('%d.%d.%d' % (v.day,v.month,v.year))
        #~ return repr(str(v))

    if isinstance(v, float):
        return repr(v)
    # return json.encoder.encode_basestring(v)
    # print repr(v)
    # http://docs.djangoproject.com/en/dev/topics/serialization/
    #~ if not isinstance(v, (str,unicode)):
        #~ raise Exception("20120121 %r is of type %s" % (v,type(v)))
    return json.dumps(v)
Example #9
0
def py2js(v):
    """Note that None values are rendered as ``null`` (not ``undefined``.

    """
    # assert _for_user_profile is not None
    # logger.debug("py2js(%r)",v)
    for cv in CONVERTERS:
        v = cv(v)

    # if isinstance(v,LanguageInfo):
        # return v.django_code

    if isinstance(v, Value):
        return v.as_ext()
        # v = v.as_ext()
        # if not isinstance(v, basestring):
            # raise Exception("20120121b %r is of type %s" % (v,type(v)))
        # return v
    if isinstance(v, Promise):
        # v = force_text(v)
        return json.dumps(force_text(v.encode('utf8')))

    if isinstance(v, types.GeneratorType):
        return "".join([py2js(x) for x in v])
    if etree.iselement(v):
        return json.dumps(force_text(etree.tostring(v)))

    # if type(v) is types.GeneratorType:
        # raise Exception("Please don't call the generator function yourself")
        # return "\n".join([ln for ln in v])
    if callable(v):
        # print 20120114, repr(v)
        # raise Exception("Please call the function yourself")
        return "\n".join([ln for ln in v()])
    if isinstance(v, js_code):
        return str(v.s)  # v.s might be a unicode
    if v is None:
        # return 'undefined'
        return 'null'
    if isinstance(v, (list, tuple)):  # (types.ListType, types.TupleType):
        elems = [py2js(x) for x in v
                 if (not isinstance(x, VisibleComponent))
                 or x.get_view_permission(_for_user_profile)]
        return "[ %s ]" % ", ".join(elems)

    if isinstance(v, dict):
        items = [
            # i for i in sorted(v.items(), key=lambda x: str(x))
            # i for i in sorted(v.items())
            i for i in v.items()
            if (not isinstance(v, VisibleComponent))
            or v.get_view_permission(_for_user_profile)]
        
        # "sorted(v.items())" without sortkey caused TypeError when
        # the dictionary contained a mixture of unicode and
        # future.types.newstr objects.
        def sortkey(x):
            if isinstance(x[0], newstr):
                return six.text_type(x[0])
            return x[0]
        items = sorted(items, key=sortkey)
        # try:
        #     items = sorted(items, key=sortkey)
        # except TypeError as e:
        #     raise TypeError("Failed to sort {0!r} : {1}".format(items, e))
        return "{ %s }" % ", ".join(
            ["%s: %s" % (py2js(k), py2js(i)) for k, i in items])

    if isinstance(v, bool):  # types.BooleanType:
        return str(v).lower()
    if isinstance(v, Quantity):
        return '"%s"' % v
    if isinstance(v, (int, decimal.Decimal, fractions.Fraction)):
        return str(v)
    if isinstance(v, IncompleteDate):
        return '"%s"' % v.strftime(settings.SITE.date_format_strftime)
    if isinstance(v, datetime.datetime):
        return '"%s"' % v.strftime(settings.SITE.datetime_format_strftime)
    if isinstance(v, datetime.time):
        return '"%s"' % v.strftime(settings.SITE.time_format_strftime)
    if isinstance(v, datetime.date):
        if v.year < 1900:
            v = IncompleteDate(v)
            return '"%s"' % v.strftime(settings.SITE.date_format_strftime)
        return '"%s"' % v.strftime(settings.SITE.date_format_strftime)

    if isinstance(v, float):
        return repr(v)
    # return json.encoder.encode_basestring(v)
    # print repr(v)
    # http://docs.djangoproject.com/en/dev/topics/serialization/
    # if not isinstance(v, (str,unicode)):
        # raise Exception("20120121 %r is of type %s" % (v,type(v)))
    return json.dumps(v)
Example #10
0
 def format_value(self, v):
     if etree.iselement(v):
         return str(etree.tostring(v))
     return str(v)