当前位置: 首页>>代码示例>>Python>>正文


Python Serializer.from_xml方法代码示例

本文整理汇总了Python中tastypie.serializers.Serializer.from_xml方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer.from_xml方法的具体用法?Python Serializer.from_xml怎么用?Python Serializer.from_xml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tastypie.serializers.Serializer的用法示例。


在下文中一共展示了Serializer.from_xml方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: dbDecode

# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_xml [as 别名]
 def dbDecode(dataString):
     assert_arg_type(dataString, basestring)
     
     ## this logic is poor - I believe the best way is to define a standard set of 
     ## representation formats and use try..except clauses to parse the incoming string
     ## according to the defined formats (formats: text, json, xml)
     ## For now we treat it as a default of text
     
     ## use tastypie's serializer to do the testing 
     from tastypie.serializers import Serializer
     ## instantiate a serializer to test
     serdes = Serializer()
     
     dataobj = None
     val = None
     try:
         val = serdes.from_xml(dataString)
     except Exception:
         val = None
     
     if val is None:
         try:
             from django.utils import simplejson
             #val = serdes.from_json(dataString)
             val = simplejson.loads(dataString, encoding='utf-8')
         except Exception, ex:
             val = None
         
         if not isinstance(val, (dict, list)):
             val = str(dataString)
             dataobj = Data(val, data_format = Data.TEXT)
         else:
             dataobj = Data(val, data_format = Data.JSON)
开发者ID:asorici,项目名称:envived,代码行数:35,代码来源:objects.py

示例2: test_round_trip_xml

# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_xml [as 别名]
 def test_round_trip_xml(self):
     serializer = Serializer()
     sample_data = self.get_sample2()
     serialized = serializer.to_xml(sample_data)
     # "response" tags need to be changed to "request" to deserialize properly.
     # A string substitution works here.
     serialized = serialized.replace('response', 'request')
     unserialized = serializer.from_xml(serialized)
     self.assertEqual(sample_data, unserialized)
开发者ID:hackoder,项目名称:django-tastypie,代码行数:11,代码来源:serializers.py

示例3: test_from_xml2

# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_xml [as 别名]
 def test_from_xml2(self):
     serializer = Serializer()
     data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><somelist type="list"><value>hello</value><value type="integer">1</value><value type="null"/></somelist><somehash type="hash"><pi type="float">3.14</pi><foo>bar</foo></somehash><false type="boolean">False</false><true type="boolean">True</true><somestring>hello</somestring></request>'
     self.assertEqual(serializer.from_xml(data), self.get_sample2())
开发者ID:hackoder,项目名称:django-tastypie,代码行数:6,代码来源:serializers.py

示例4: test_from_xml

# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_xml [as 别名]
 def test_from_xml(self):
     serializer = Serializer()
     data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><age type="integer">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined><rocksdahouse type="boolean">True</rocksdahouse></request>'
     self.assertEqual(serializer.from_xml(data), {'rocksdahouse': True, 'age': 27, 'name': 'Daniel', 'date_joined': '2010-03-27'})
开发者ID:hackoder,项目名称:django-tastypie,代码行数:6,代码来源:serializers.py

示例5: ResourceTestCase

# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_xml [as 别名]

#.........这里部分代码省略.........

    def assertHttpTooManyRequests(self, resp):
        """
        Ensures the response is returning a HTTP 429.
        """
        return self.assertEqual(resp.status_code, 429)

    def assertHttpApplicationError(self, resp):
        """
        Ensures the response is returning a HTTP 500.
        """
        return self.assertEqual(resp.status_code, 500)

    def assertHttpNotImplemented(self, resp):
        """
        Ensures the response is returning a HTTP 501.
        """
        return self.assertEqual(resp.status_code, 501)

    def assertValidJSON(self, data):
        """
        Given the provided ``data`` as a string, ensures that it is valid JSON &
        can be loaded properly.
        """
        # Just try the load. If it throws an exception, the test case will fail.
        self.serializer.from_json(data)

    def assertValidXML(self, data):
        """
        Given the provided ``data`` as a string, ensures that it is valid XML &
        can be loaded properly.
        """
        # Just try the load. If it throws an exception, the test case will fail.
        self.serializer.from_xml(data)

    def assertValidYAML(self, data):
        """
        Given the provided ``data`` as a string, ensures that it is valid YAML &
        can be loaded properly.
        """
        # Just try the load. If it throws an exception, the test case will fail.
        self.serializer.from_yaml(data)

    def assertValidPlist(self, data):
        """
        Given the provided ``data`` as a string, ensures that it is valid
        binary plist & can be loaded properly.
        """
        # Just try the load. If it throws an exception, the test case will fail.
        self.serializer.from_plist(data)

    def assertValidJSONResponse(self, resp):
        """
        Given a ``HttpResponse`` coming back from using the ``client``, assert that
        you get back:

        * An HTTP 200
        * The correct content-type (``application/json``)
        * The content is valid JSON
        """
        self.assertHttpOK(resp)
        self.assertTrue(resp['Content-Type'].startswith('application/json'))
        self.assertValidJSON(resp.content)

    def assertValidXMLResponse(self, resp):
        """
开发者ID:grunskis,项目名称:django-tastypie,代码行数:70,代码来源:test.py

示例6: test_from_xml

# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_xml [as 别名]
 def test_from_xml(self):
     serializer = Serializer()
     data = "<?xml version='1.0' encoding='utf-8'?>\n<request><age type=\"integer\">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined><rocksdahouse type=\"boolean\">True</rocksdahouse></request>"
     self.assertEqual(
         serializer.from_xml(data), {"rocksdahouse": True, "age": 27, "name": "Daniel", "date_joined": "2010-03-27"}
     )
开发者ID:robhudson,项目名称:django-tastypie,代码行数:8,代码来源:serializers.py

示例7: ApiTestCase

# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_xml [as 别名]
class ApiTestCase(DirectSEOTestCase):
    fixtures = ['seo_views_testdata.json']
    def setUp(self):
        super(ApiTestCase, self).setUp()
        # Create a test user and an API key for that user.
        self.user, created = User.objects.create_user(email='[email protected]',
                                                      password='password')
        self.username = self.user.email
        self.user.save()
        self.key = ApiKey(user=self.user)
        self.key.save()
        self.api_key = self.key.key
        self.auth_qs = '?&username=%s&api_key=%s' % (self.username,
                                                     self.api_key)
        self.entry_1 = SeoSite.objects.get(group=1)
        self.detail_url = '/api/v1/seosite/{0}/'.format(self.entry_1.pk)
        self.serializer = Serializer()

    def deserialize(self, resp):
        return self.serializer.deserialize(resp.content, format=resp['Content-Type'])

    def test_not_authorized(self):
        """
        Test if a user can gain access without an API key
        """
        user, created = User.objects.create_user(email='[email protected]',
                                                 password='password')
        self.username = self.user.email
        user.save()
        resp = self.client.get("api/v1/jobsearch/?format=xml")
        self.assertEqual(resp.status_code, 404)
        resp = self.client.get("api/v1/seosite/?format=xml")
        self.assertEqual(resp.status_code, 404)
        key = ApiKey(user=self.user)

    def test_list_xml(self):
        resp = self.client.get("/api/v1/seosite/?%s&format=xml" % (self.auth_qs))
        self.assertEqual(resp.status_code, 200)
        self.serializer.from_xml(resp.content)

    def test_list_json(self):
        resp = self.client.get("/api/v1/seosite/?%s&format=json" % (self.auth_qs))
        self.assertEqual(len(self.deserialize(resp)['objects']), 1)
        self.assertEqual(resp.status_code, 200)
        self.serializer.from_json(resp.content)

    def test_get_detail_xml(self):
        resp = self.client.get("/api/v1/seosite/1/%s&format=xml" % (self.auth_qs))
        self.assertEqual(resp.status_code, 200)
        self.serializer.from_xml(resp.content)

    def test_nopost(self):
        """
        Ensure that POST requests are rejected. This test can be removed
        if/when we allow other methods besides GET to our resources.

        """
        jobjson = ("""{"buid": 13543, "city": "Chester",\
                   "company": "Edward Jones", "country": "United States",\
                   "date_new": "2012-05-31T11:49:23",\
                   "mocs": "[\'021\', \'0193\', \'2820\', \'2G000\', \'2G091\',\
                   \'2R000\', \'2R071\', \'2R090\', \'2R171\', \'2S000\',\
                   \'2S071\', \'2S091\', \'2T000\', \'2T071\', \'2T091\',\
                   \'3A000\', \'3A071\', \'3A091\', \'3C000\', \'3C071\',\
                   \'3C090\', \'3C171\', \'3C191\', \'4A000\', \'4A091\',\
                   \'4A100\', \'4A191\', \'6F000\', \'6F091\', \'8M000\']",\
                   "onet": "43101100",\
                   "resource_uri": "/seo/v1/jobposting/29068157/",\
                   "state": "Virginia", "title": "Branch Office Administrator -\
                   Chester, VA - Branch 48113", "uid": "29068157"}""")
        resp = self.client.post("/api/v1/jobsearch/%s" % self.auth_qs,
                                data=jobjson, content_type="application/json")
        # HTTP 405 == 'Method Not Allowed'
        self.assertEqual(resp.status_code, 405)
开发者ID:DirectEmployers,项目名称:MyJobs,代码行数:76,代码来源:test_api.py


注:本文中的tastypie.serializers.Serializer.from_xml方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。