-
Notifications
You must be signed in to change notification settings - Fork 0
/
soap1.py
163 lines (145 loc) · 7.11 KB
/
soap1.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging
from soaplib.serializers.primitive import String
from soaplib.service import DefinitionBase, rpc
from soaplib.wsgi import Application
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import httplib2
from django.template import Template
from django.template.context import Context
from django.utils.encoding import smart_str, smart_unicode
from xml.etree.ElementTree import XML
# the class with actual web methods
#logger = logging.getLogger(__name__)
class NewQuoteSOAPService(DefinitionBase):
@rpc(String,String,String,String,String,String,String,String,String,String,String,String, _returns=String)
def updatequotewithparameters(self, quote_id,ExternalReference,Grossvalue,netvalue,postingDate,RefDate,SoldToParty,SoldToPartyAdd,Status,TaxAmt,ValidFrm,ValidTo):
logging.info("SAP is sending quote with more parameters")
logging.info(locals())
logging.info("CONNECTING TO SALESFORCE PARTNER WSDL FOR SESSION ID")
url = "https://login.salesforce.com/services/Soap/u/28.0"
data = """<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">
<soapenv:Header>
<urn:CallOptions>
<urn:client></urn:client>
<urn:defaultNamespace></urn:defaultNamespace>
</urn:CallOptions>
<urn:LoginScopeHeader>
<urn:organizationId></urn:organizationId>
<urn:portalId></urn:portalId>
</urn:LoginScopeHeader>
</soapenv:Header>
<soapenv:Body>
<urn:login>
<urn:username>{{username}}</urn:username>
<urn:password>{{password}}</urn:password>
</urn:login>
</soapenv:Body>
</soapenv:Envelope>"""
t = Template(data)
c = Context({
# "username": "testmogo@iserviceglobe.net",
# "password": "venkatesh123ffk0XM9LgYDkBpmRJEJ6PuL7g"
# "username": "admin@sambodhi.com",
# "password": "sfdc*123Lw8jQCSB0ygTot7YagKHoiBJK"
"username": "admin@isg.com",
"password": "iservice123ZmYFLUDAgDlyV068L9mFxyNF5"
})
data = t.render(c)
logging.info("SENDING:")
logging.info(data)
headers = {
'Content-Type': 'text/xml; charset=utf-8',
'SOAPAction' : 'https://login.salesforce.com/services/Soap/u/28.0'
}
# httplib2.debuglevel = 1
head = httplib2.Http()
# head.follow_all_redirects = True
response, content = head.request(url, "POST", smart_str(data), headers)
logging.info("########### SESSION ID response ###############%s"%response)
logging.info("########## SESSION ID content ############## \n %s"%pretty(content))
if response.get('status') == '200':
logging.info("GOT THE SESSION ID FROM SALESFORCE")
xml = XML(content)
session_response=xml.find("{http://schemas.xmlsoap.org/soap/envelope/}Body").getchildren()[0]
session_id = session_response[0][4].text
quote_id_to_sf(session_id,quote_id,ExternalReference,Grossvalue,netvalue,postingDate,RefDate,SoldToParty,SoldToPartyAdd,Status,TaxAmt,ValidFrm,ValidTo)
else:
return content
return "OK"
def quote_id_to_sf(session_id,quote_id,ExternalReference,Grossvalue,netvalue,postingDate,RefDate,SoldToParty,SoldToPartyAdd,Status,TaxAmt,ValidFrm,ValidTo):
logging.info("############## CONNECTING TO SALESFORCE QUOTE WSDL ##############")
url = "https://na15.salesforce.com/services/Soap/class/QuoteClass"
data = """<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:quot="http://soap.sforce.com/schemas/class/QuoteClass">
<soapenv:Header>
<quot:SessionHeader>
<quot:sessionId>{{session_id}}</quot:sessionId>
</quot:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<quot:insertQuote>
<quot:quoteId>{{quote_id}}</quot:quoteId>
<quot:ExternalReference>{{external}}</quot:ExternalReference>
<quot:Grossvalue>{{gross}}</quot:Grossvalue>
<quot:netvalue>{{netvalue}}</quot:netvalue>
<quot:SoldToParty>{{SoldToParty}}</quot:SoldToParty>
<quot:Status>{{Status}}</quot:Status>
<quot:TaxAmt>{{TaxAmt}}</quot:TaxAmt>
<quot:ValidFrm>{{ValidFrm}}</quot:ValidFrm>
<quot:ValidTo>{{ValidTo}}</quot:ValidTo>
</quot:insertQuote>
</soapenv:Body>
</soapenv:Envelope>"""
t = Template(data)
c = Context({
"session_id": session_id,
"quote_id": quote_id,
"external": ExternalReference,
"gross": Grossvalue if Grossvalue else 0.00,
"netvalue": netvalue if netvalue else 0.00,
"postingDate" : postingDate,
"RefDate" : RefDate,
"SoldToParty" : SoldToParty,
"SoldToPartyAdd" : SoldToPartyAdd,
"Status" : Status,
"TaxAmt" : TaxAmt if TaxAmt else 0.00,
"ValidFrm" : ValidFrm,
"ValidTo" : ValidTo
})
data = t.render(c)
logging.info("SENDING:")
logging.info(data)
headers = {
'Content-Type': 'text/xml; charset=utf-8',
'SOAPAction' : 'https://na15.salesforce.com/services/Soap/class/QuoteClass'
}
# httplib2.debuglevel = 1
head = httplib2.Http()
# head.follow_all_redirects = True
response, content = head.request(url, "POST", smart_str(data), headers)
logging.info("######################### QUOTE response ############## %s"%response)
logging.info("###################### QUOTE content ################# \n%s"%pretty(content))
if response.get('status') == "200":
xml = XML(content)
quote_response=xml.find("{http://schemas.xmlsoap.org/soap/envelope/}Body").getchildren()[0]
return
def pretty(xml):
from lxml import etree
root = etree.fromstring(xml)
return etree.tostring(root, pretty_print=True)
class NewQuoteSoapApp(Application):
def __call__(self, request):
# wrap the soaplib response into a Django response object
django_response = HttpResponse()
def start_response(status, headers):
status, reason = status.split(' ', 1)
django_response.status_code = int(status)
for header, value in headers:
django_response[header] = value
response = super(NewQuoteSoapApp, self).__call__(request.META, start_response)
django_response.content = '\n'.join(response)
return django_response
# the view to use in urls.py
my_soap_service = csrf_exempt(NewQuoteSoapApp([NewQuoteSOAPService], "http://schema.iserviceglobe.com/sap_sync/"))