WIP: simplistic shipcloud webhook support

shipcloud can generate events towards a webhook whenever there's
status/tracking updates for a shipment.  Let's
* store the latest status in the stock.picking
* post a timestamped message to the stock.picking
master
Harald Welte 2 years ago
parent b8decb3ec8
commit 76665e61c0
  1. 2
      __init__.py
  2. 1
      controllers/__init__.py
  3. 60
      controllers/main.py
  4. 1
      models/shipcloud_shipping_service.py
  5. 1
      views/sc_delivery_carrier.xml

@ -1 +1 @@
from . import models
from . import models, controllers

@ -0,0 +1,60 @@
import json
import logging
import pprint
#import urllib2
#import werkzeug
from openerp import http, SUPERUSER_ID
from openerp.http import request
_logger = logging.getLogger(__name__)
# map event types to human-readable strings
TYPE_MAP = {
'shipment.status.deleted': 'Label has been deleted',
'shipment.tracking.label_created': 'Label has been created',
'shipment.tracking.picked_up': 'Shipment was picked up by carrier',
'shipment.tracking.transit': 'Shipment is in transit',
'shipment.tracking.out_for_delivery': 'Shipment is out for delivery',
'shipment.tracking.delivered': 'Shipment was delivered',
'shipment.tracking.awaits_pickup_by_receiver': 'Shipment awaits pick-up by recipient',
'shipment.tracking.canceled': 'Label has been cancelled',
'shipment.tracking.delayed': 'Delivery will be delayed',
'shipment.tracking.exception': 'Shipment problem / exception',
'shipment.tracking.not_delivered': 'Shipment was not delivered',
'shipment.tracking.destroyed': 'Shipment destroyed by customs or higher authorities',
'shipment.tracking.notification': 'Carrier internal notification',
'shipment.tracking.unknown': 'Shipment status unknown',
}
class ShipcloudController(http.Controller):
def shipcloud_tracking_event(**post):
event_type = post.get('type')
event_time = post.get('occured_at')
event_data = post.get('data')
event_id = event_data['id']
if event_data['object_type'] == 'shipment':
# look up the picking by sc_shipment_id == id
picking_obj = self.env['stock.picking']
picking = picking_obj.search([('sc_shipment_id','=',event_id)], limit=1)
if not picking:
_logger.error('shipcloud webhook for unknown shipment %s' % event_id)
return
# update event type
picking.sc_tracking_state = event_type.replace('shipment.tracking.','')
# post a message to the picking
event_str = TYPE_MAP[event_type] if event_type in TYPE_MAP else event_type
msg = 'shipcloud event: %s at %s' % (event_str, event_time)
picking.message_post(type='comment', subtype='mt_comment', content_subtype='plaintext',
body=msg)
@http.route('/webhook/shipcloud/tracking', type='json', auth='none', methods=['POST'])
def shipcloud_tracking(self, **post):
_logger.info('Beginning shipcloud tracking webhook with post data %s', pprint.pformat(post))
self.shipcloud_process_tracking_event(**post)
return ''

@ -13,6 +13,7 @@ class SMCStockPickingShipclodu(models.Model):
sc_shipment_id = fields.Char(string='shipcloud shipment ID', readonly=True, copy=False)
sc_tracking_url = fields.Char(string='shipcloud tracking URL', readonly=True, copy=False)
sc_content_desc = fields.Char(string='Description of Contents')
sc_tracking_state = fields.Char(string='shipcloud tracking status', readonly=True, copy=Fasle)
class SMCShippingScCarrierService(models.Model):

@ -8,6 +8,7 @@
<xpath expr="//label[@for='carrier_tracking_ref']" position="before">
<field name="sc_shipment_id" attrs="{'invisible':[('delivery_type', '!=', 'sc')]}"/>
<field name="sc_content_desc" attrs="{'invisible':[('delivery_type', '!=', 'sc')]}"/>
<field name="sc_tracking_state" attrs="{'invisible':[('delivery_type', '!=', 'sc')]}"/>
</xpath>
</field>
</record>

Loading…
Cancel
Save