Login

Convert django model into soaplib model, to expose webservices

Author:
s.federici
Posted:
January 16, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

Snippet to convert django model into soaplib model and expose it by SOAP

This snippet use DjangoSoapApp sniplet to autogenerate WSDL.

 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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 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'

#

Please login first before commenting.