-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtracking.py
125 lines (109 loc) · 3.52 KB
/
tracking.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
# -*- coding: utf-8 -*-
"""
tracking.py
"""
from trytond.model import fields, ModelView, ModelSQL
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval
__metaclass__ = PoolMeta
__all__ = ['ShipmentTracking']
class ShipmentTracking(ModelSQL, ModelView):
"""Shipment Tracking
"""
__name__ = 'shipment.tracking'
_rec_name = 'tracking_number'
#: Boolean to indicate if tracking number is master.
is_master = fields.Boolean("Is Master ?", readonly=True, select=True)
origin = fields.Reference(
'Origin', selection='get_origin', select=True, readonly=True
)
tracking_number = fields.Char(
"Tracking Number", required=True, select=True, readonly=True
)
carrier = fields.Many2One(
'carrier', 'Carrier', required=True, readonly=True
)
tracking_url = fields.Char("Tracking Url", readonly=True)
delivery_date = fields.Date("Delivery Date", readonly=True)
delivery_time = fields.Time("Delivery Time", readonly=True)
state = fields.Selection([
('waiting', 'Waiting'),
('in_transit', 'In Transit'),
('exception', 'Exception'),
('delivered', 'Delivered'),
('out_for_delivery', 'Out For Delivery'),
('failure', 'Failure'),
('returned', 'Returned'),
('cancelled', 'Cancelled'),
('unknown', 'Unknown'),
('pending_cancellation', 'Pending Cancellation'),
], 'State', readonly=True, required=True, select=True)
@staticmethod
def default_state():
return 'waiting'
@classmethod
def __setup__(cls):
"""
Setup the class before adding to pool
"""
super(ShipmentTracking, cls).__setup__()
cls._buttons.update({
'cancel_tracking_number_button': {
'invisible': Eval('state') == 'cancelled',
},
'refresh_status_button': {},
})
def cancel_tracking_number(self):
"Cancel tracking number"
self.state = 'cancelled'
self.save()
@classmethod
@ModelView.button
def cancel_tracking_number_button(cls, tracking_numbers):
"""
Cancel tracking numbers
"""
for tracking_number in tracking_numbers:
tracking_number.cancel_tracking_number()
def refresh_status(self):
"""
Downstream module can implement this
"""
pass
@classmethod
@ModelView.button
def refresh_status_button(cls, tracking_numbers):
"""
Update tracking numbers state
"""
for tracking_number in tracking_numbers:
tracking_number.refresh_status()
@classmethod
def refresh_tracking_numbers_cron(cls):
"""
This is a cron method, responsible for updating state of
shipments.
"""
states_to_refresh = [
'pending_cancellation',
'failure',
'waiting',
'in_transit',
]
tracking_numbers = cls.search([
('state', 'in', states_to_refresh),
])
for tracking_number in tracking_numbers:
tracking_number.refresh_status()
@classmethod
def _get_origin(cls):
'Return list of Model names for origin Reference'
return ['stock.shipment.out', 'stock.package']
@classmethod
def get_origin(cls):
Model = Pool().get('ir.model')
models = cls._get_origin()
models = Model.search([
('model', 'in', models),
])
return [(None, '')] + [(m.model, m.name) for m in models]