Convert django model into soaplib model, to expose webservices

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#contrib.soaplib.serializer.py

from soaplib.serializers.clazz import ClassSerializer

class BaseClassSerializer(ClassSerializer):
    '''
    Helper class to convert django model into soaplib model
    
    Constructor read from django model, custom dict, 
    and permit to override parameters using kwargs.

    
    Example:

    #modelsws.py
    
        from contrib.soaplib.serializer import BaseClassSerializer
        from soaplib.serializers.primitive import *
        
        class WsComment(BaseClassSerializer):
            class types:
                content = String 
                rating = Integer
                ratingText = String
    
        class WsBlog(BaseClassSerializer):
            class types:
                title = String
                subtitle = String
                pub_date = DateTime
                replay_to = String 
                content = String
                comments = Array(WsComment)
            
            
    #views.py
    
        from contrib.soaplib.handler import DjangoSoapApp, soapmethod, soap_types
        from foo.models import Blog
        from foo.wsmodels import WsBlog
        from foo.wsmodels import WsComment
        
        class BlogService(DjangoSoapApp):
    
            __tns__ = 'http://ws.javapress.org/blog/'
        
            @soapmethod(_returns=soap_types.Array(WsBlog))
            def get_blogs(self):
                blogs = Blog.objects.all()
                results = []
                for blog in blogs:
                    comments = []
                    for comment in blog.comment_set.all():
                        comments.append(WsComment(comment, ratingText=comment.ratingText()))
                    b = WsBlog(blog, comments=comments)
                    results.append(b)
                return results
        
        blog_service = BlogService()
        
    #urls.py
        urlpatterns = patterns('',
            (r'^blog/', 'foo.views.blog_service'),
            (r'^blog/service.wsdl', 'foo.views.blog_service'),
        )
    
    '''
    def __init__(self, *args, **kwargs):
        super(BaseClassSerializer, self).__init__()
               
        # for each args read attributes and update wsobjcet
        for source in args:
            if isinstance(source, dict):
                self.__dict__.update(source)
            else:
                # if arg is not an dict, take his dict
                self.__dict__.update(source.__dict__)
        
        # update object also with kwargs
        self.__dict__.update(kwargs)

More like this

  1. Automatically expose soaplib methods in WSDL by AndrewIngram 5 years, 2 months ago
  2. soaplib service integration 2 by erny 5 years ago
  3. SOAP web service with soaplib 0.9+ by wRAR 3 years, 6 months ago
  4. SOAP web service with soaplib 2.0 by treyh 2 years, 3 months ago
  5. SOAP views with on-demand WSDL generation by chewie 5 years, 8 months ago

Comments

aronchi (on August 25, 2010):

When I try to use this code, I get this error:

Traceback (most recent call last): File "/home/aronchi/workspace/bonfiglioli/libraries/soaplib/wsgi_soap.py", line 244, in call retval = func(params) File "/home/aronchi/workspace/bonfiglioli/libraries/soaplib/service.py", line 56, in explainMethod return f(args, **kwargs) File "/home/aronchi/workspace/bonfiglioli/src/bonfiglioli/catalogue/web_service.py", line 26, in get_product results = ProductSoap(p, html_tables=p.get_product_tables_html(language_code,orientation="vertical")) File "/home/aronchi/workspace/bonfiglioli/libraries/studiopleiadi/soap/serializer.py", line 69, in init super(BaseClassSerializer, self).init() File "/home/aronchi/workspace/bonfiglioli/libraries/soaplib/serializers/clazz.py", line 42, in init for k,v in cls.soap_members.items(): AttributeError: type object 'ProductSoap' has no attribute 'soap_members'

#

(Forgotten your password?)