forked from priyankarani/trytond-shipping-dpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarrier.py
More file actions
167 lines (143 loc) · 5.2 KB
/
carrier.py
File metadata and controls
167 lines (143 loc) · 5.2 KB
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
163
164
165
166
167
# -*- coding: utf-8 -*-
"""
carrier
"""
from decimal import Decimal
from suds import WebFault
from trytond.transaction import Transaction
from trytond.pool import PoolMeta, Pool
from trytond.model import fields, ModelView
from trytond.pyson import Eval
from trytond.wizard import Wizard, StateView, Button
from dpd_client import DPDClient
__all__ = ['Carrier', 'TestConnectionStart', 'TestConnection']
__metaclass__ = PoolMeta
STATES = {
'required': Eval('carrier_cost_method') == 'dpd',
'invisible': Eval('carrier_cost_method') != 'dpd'
}
class Carrier:
"Carrier"
__name__ = 'carrier'
dpd_url = fields.Char(
'Base URL', help="Ex. https://public-ws-stage.dpd.com",
states=STATES, depends=['carrier_cost_method']
)
dpd_login_service_wsdl = fields.Char(
'Login Service URL', states=STATES, depends=['carrier_cost_method']
)
dpd_shipment_service_wsdl = fields.Char(
'Shipment Service URL', states=STATES, depends=['carrier_cost_method']
)
dpd_depot_data_service_wsdl = fields.Char(
'Depot Data Service URL', states=STATES, depends=['carrier_cost_method']
)
dpd_parcel_shop_finder_service_wsdl = fields.Char(
'Parcel Shop Finder Service URL', states=STATES,
depends=['carrier_cost_method']
)
dpd_username = fields.Char(
'Username/DelisID', states=STATES, depends=['carrier_cost_method']
)
dpd_password = fields.Char(
'Password', states=STATES, depends=['carrier_cost_method']
)
dpd_depot = fields.Char(
'Depot', states=STATES, depends=['carrier_cost_method']
)
@classmethod
def view_attributes(cls):
return super(Carrier, cls).view_attributes() + [
('//group[@id="dpd_config"]', 'states', {
'invisible': Eval('carrier_cost_method') != 'dpd'
})]
@classmethod
def __setup__(cls):
super(Carrier, cls).__setup__()
selection = ('dpd', 'DPD')
if selection not in cls.carrier_cost_method.selection:
cls.carrier_cost_method.selection.append(selection)
cls._buttons.update({
'test_dpd_credentials': {},
})
@fields.depends(
'carrier_cost_method', 'dpd_url', 'dpd_login_service_wsdl',
'dpd_shipment_service_wsdl', 'dpd_depot_data_service_wsdl',
'dpd_parcel_shop_finder_service_wsdl'
)
def on_change_dpd_url(self):
"""
Set the login_service and shipment_service URL on change of dpd_url
"""
if self.carrier_cost_method != 'dpd':
return
if not self.dpd_url:
return
self.dpd_login_service_wsdl = (
self.dpd_url + '/services/LoginService/V2_0?wsdl'
)
self.dpd_shipment_service_wsdl = (
self.dpd_url + '/services/ShipmentService/V3_2?wsdl'
)
self.dpd_depot_data_service_wsdl = (
self.dpd_url + '/services/DepotDataService/V1_0?wsdl'
)
self.dpd_parcel_shop_finder_service_wsdl = (
self.dpd_url + '/services/DepotDataService/V1_0?wsdl'
)
def get_dpd_client(self):
"""
Return the DPD client with the username and password set
"""
return DPDClient(
self.dpd_login_service_wsdl,
self.dpd_shipment_service_wsdl,
self.dpd_depot_data_service_wsdl,
self.dpd_parcel_shop_finder_service_wsdl,
self.dpd_username,
self.dpd_password,
message_language=Transaction().context.get('language', 'en_US')
)
@classmethod
@ModelView.button_action('shipping_dpd.wizard_test_connection')
def test_dpd_credentials(cls, carriers):
"""
Tests the connection. If there is a WebFault, raises an UserError
"""
if len(carriers) != 1:
cls.raise_user_error('Only one carrier can be tested at a time.')
client = carriers[0].get_dpd_client()
try:
client.get_auth()
except WebFault, exc:
cls.raise_user_error(exc.fault)
def get_sale_price(self):
"""Estimates the shipment rate for the current shipment
DPD dont provide and shipping cost, so here shipping_cost will be 0
returns a tuple of (value, currency_id)
:returns: A tuple of (value, currency_id which in this case is USD)
"""
Currency = Pool().get('currency.currency')
Company = Pool().get('company.company')
if self.carrier_cost_method != 'dpd':
return super(Carrier, self).get_sale_price() # pragma: no cover
currency, = Currency.search([('code', '=', 'USD')])
company = Transaction().context.get('company')
if company:
currency = Company(company).currency
return Decimal('0'), currency.id
class TestConnectionStart(ModelView):
"Test Connection"
__name__ = 'shipping_dpd.wizard_test_connection.start'
class TestConnection(Wizard):
"""
Test Connection Wizard
"""
__name__ = 'shipping_dpd.wizard_test_connection'
start = StateView(
'shipping_dpd.wizard_test_connection.start',
'shipping_dpd.wizard_test_connection_view_form',
[
Button('Ok', 'end', 'tryton-ok'),
]
)