0

我想用正则表达式扩展 spyne Unicode 字段,以确保它是有效的电子邮件格式。但即使从 spyne 文档http://spyne.io/docs/2.10/manual/03_types.html复制粘贴基本示例,我在访问localhost/my-url-endpoint?wsdl.

我使用 Django 1.6 和 Spyne 2.10.10。在 Windows8 64 位上。任何建议为什么它失败?

编码:

from django.views.decorators.csrf import csrf_exempt
from spyne.protocol.soap import Soap11
from spyne.interface import Wsdl11
from spyne.service import ServiceBase
from spyne.decorator import srpc, rpc
from spyne.model.primitive import Unicode, Integer, Mandatory
from spyne.model.complex import Iterable
from spyne.application import Application
from spyne.server.django import DjangoApplication

class EmailString(Unicode):
    __type_name__ = 'EmailString'

    class Attributes(Unicode.Attributes):
        max_length = 128
        pattern = '[^@]+@[^@]+'

class MyService(ServiceBase):

    @rpc(EmailString, _returns=Unicode)
    def my_function(ctx, my_email):
        return "Your email is %s" % my_email

application = Application(
        [                  
            MyService
        ],
        tns="http://tempuri.org",
        interface=Wsdl11(),
        in_protocol=Soap11(validator='lxml'),
        out_protocol=Soap11()
)
myServiceApp = csrf_exempt(DjangoApplication(application))

然后在 urls.py 中指向 MyServiceApp:

urlpatterns += patterns('',
    (r'^my-url-endpoint$', 'myapp.views.myServiceApp'),
)

堆栈跟踪:

Internal Server Error: /en/wsCRMService
Traceback (most recent call last):
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\core\handlers\base.py", line 101, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\core\urlresolvers.py", line 320, in resolve
    sub_match = pattern.resolve(new_path)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\core\urlresolvers.py", line 320, in resolve
    sub_match = pattern.resolve(new_path)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\core\urlresolvers.py", line 320, in resolve
    sub_match = pattern.resolve(new_path)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\core\urlresolvers.py", line 222, in resolve
    return ResolverMatch(self.callback, args, kwargs, self.name)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\core\urlresolvers.py", line 229, in callback
    self._callback = get_callable(self._callback_str)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\utils\functional.py", line 32, in wrapper
    result = func(*args)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\core\urlresolvers.py", line 96, in get_callable
    mod = import_module(mod_name)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\django-1.6.1-py2.7.egg\django\utils\importlib.py", line 40, in import_module
    __import__(name)
  File "E:\my_project\myapp\views.py", line 146, in <module>
    out_protocol=Soap11()
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\spyne\application.py", line 104, in __init__
    self.in_protocol.set_app(self)
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\spyne\protocol\xml\_base.py", line 413, in set_app
    xml_schema.build_validation_schema()
  File "C:\Users\Anze\Virtual Environments\my_project\lib\site-packages\spyne\interface\xml_schema\_base.py", line 189, in build_validation_schema
    self.validation_schema = etree.XMLSchema(etree.parse(f))
  File "xmlschema.pxi", line 102, in lxml.etree.XMLSchema.__init__ (src\lxml\lxml.etree.c:154067)
XMLSchemaParseError: element decl. '{http://tempuri.org}my_email', attribute 'type': The QName value '{http://www.w3.org/2001/XMLSchema}EmailString' does not resolve to a(n) type definition., line 4

请帮忙。

4

2 回答 2

0

经过数小时的研究,我发现如果你这样做,它也有效 class EmailString(Unicode(pattern='[^@]+@[^@]+')): __namespace__ = 'tempuri.org' __type_name__ = 'EmailString'

似乎 EmailString 的元类属性不应被覆盖,然后才能正常工作。相反,您必须将自定义放在扩展类的构造函数中(在这种情况下为 Unicode)

于 2014-03-20T07:36:04.803 回答
0

如果您这样做,它将起作用:

EmailString = Unicode(128, pattern='[^@]+@[^@]+', type_name="EmailStringType")

不过,该模式只是一个示例,您可以在那里找到更好的模式。

于 2014-03-18T23:01:47.610 回答