-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quote.py
128 lines (101 loc) · 5.86 KB
/
Quote.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
import requests
from xml.etree.ElementTree import fromstring
import cm_exceptions
# Map wcfmc service names to choice mechanics auto quote api. See GetQuote function
SERVICE_TERMS = {
'Clutch Replacement': 'clutch'
}
class Quote():
""" Represents a repair quote from choice mechanics API """
vehicle_registration = None
location = None
service = None
def __init__(self, api_key, vehicle_registration, location, service):
""" Do auto quote using CM's api. $Service will be mapped against SERVICE_TERMS """
self.api_key = api_key
self.vehicle_registration = vehicle_registration
self.location = location
self.service = service
self._get_quote_from_cm_api()
def _get_quote_from_cm_api(self):
""" Queries the CM api for a quote and saves the information on the self """
if self.service not in SERVICE_TERMS.keys():
raise cm_exceptions.UnrecognisedService('Unrecognised service %s' % self.service)
api_name = SERVICE_TERMS[self.service]
if api_name == 'clutch':
quote_request = requests.get('http://www.choicemobilemechanics.com/api/quote/clutch/' \
+ '?key=%(api_key)s&numberplate=%(vehicle_registration)s&location=%(location)s'\
% {
'api_key': self.api_key,
'vehicle_registration': self.vehicle_registration,
'location': self.location,
})
if 'This vehicle has not been looked up correctly' in quote_request.content:
raise ValueError('Look up failed (This vehicle has not been looked up correctly)')
quote_xml = quote_request.content
self.quote_raw = quote_xml
quote_element = fromstring(quote_xml)
# extract kit
budget_kit_element = quote_element.find('.//BudgetKit')
genuine_kit_element = quote_element.find('.//GenuineKit')
if str(budget_kit_element) == 'None' and str(genuine_kit_element) == 'None': # normal None check is not supported by xml lib
raise cm_exceptions.NoKitPriceError()
# extract budget option info
self.budget_option = bool(budget_kit_element)
if self.budget_option:
self.budget_kit_name = budget_kit_element.find('.//KitName').text or ''
self.budget_kit_type = budget_kit_element.find('.//KitType').text or ''
self.budget_parts_cost = float(budget_kit_element.find('.//TotalCost').text or '0')
self.budget_parts_retail = float(budget_kit_element.find('.//TotalPrice').text or '0')
self.budget_margin = float(budget_kit_element.find('.//TotalMargin').text or '0')
self.budget_bearing_name = budget_kit_element.find('.//BearingName').text or ''
self.budget_bearing_retail = budget_kit_element.find('.//BearingRetail').text or 0
self.budget_bearing_cost = budget_kit_element.find('.//BearingCost').text or 0
else:
self.budget_kit_name = ''
self.budget_kit_type = ''
self.budget_parts_cost = 0
self.budget_parts_retail = 0
self.budget_margin = 0
self.budget_bearing_name = ''
self.budget_bearing_retail = 0
self.budget_bearing_cost = 0
# extract genuine option info
self.genuine_option = bool(genuine_kit_element)
if self.genuine_option:
self.genuine_kit_name = genuine_kit_element.find('.//KitName').text or ''
self.genuine_kit_type = genuine_kit_element.find('.//KitType').text or ''
self.genuine_parts_cost = float(genuine_kit_element.find('.//TotalCost').text or '0')
self.genuine_parts_retail = float(genuine_kit_element.find('.//TotalPrice').text or '0')
self.genuine_margin = float(genuine_kit_element.find('.//TotalMargin').text or '0')
self.genuine_bearing_name = genuine_kit_element.find('.//BearingName').text or ''
self.genuine_bearing_retail = genuine_kit_element.find('.//BearingRetail').text or 0
self.genuine_bearing_cost = genuine_kit_element.find('.//BearingCost').text or 0
else:
self.genuine_kit_name = ''
self.genuine_kit_type = ''
self.genuine_parts_cost = 0
self.genuine_parts_retail = 0
self.genuine_margin = 0
self.genuine_bearing_name = ''
self.genuine_bearing_retail = 0
self.genuine_bearing_cost = 0
# extract quote details
self.quote_details = quote_element.find('.//QuoteDetails')
self.bearing_type = self.quote_details.find('BearingType').text or ''
self.labour_rate = self.quote_details.find('LabourRate').text or ''
self.labour_hours = self.quote_details.find('LabourTime').text or ''
self.approx_milage = self.quote_details.find('Mileage').text or ''
# extract flywheel information
flywheel_element = quote_element.find('.//Flywheel')
self.flywheel_option = (flywheel_element.find('.//DualMass').text != '0') # True if dual mass is not 0
if self.flywheel_option:
self.flywheel_cost = float(flywheel_element.find('.//TotalCost').text or '0')
self.flywheel_retail = float(flywheel_element.find('.//TotalPrice').text or '0')
self.flywheel_margin = float(flywheel_element.find('.//TotalMargin').text or '0')
else:
self.flywheel_cost = 0
self.flywheel_retail = 0
self.flywheel_margin = 0
else:
raise NotImplementedError('Service %s recognised but not implemented' % service)