我想用regex扩展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
请帮忙。
答案 0 :(得分:0)
如果您这样做,它将起作用:
EmailString = Unicode(128, pattern='[^@]+@[^@]+', type_name="EmailStringType")
这种模式只是一个例子,你可以在那里找到更好的模式。
答案 1 :(得分:0)
经过数小时的研究后,我发现如果你这样做也会有效
class EmailString(Unicode(pattern='[^@]+@[^@]+')):
__namespace__ = 'tempuri.org'
__type_name__ = 'EmailString'
看起来像元类的EmailString的属性不应该被覆盖然后它的工作原理。相反,您必须将自定义放在扩展类的构造函数中(在本例中为Unicode)