Show tracking status on list

master
informatic 2020-04-24 14:23:03 +02:00
parent fccf493f8e
commit 3619b888a3
2 changed files with 66 additions and 4 deletions

View File

@ -22,6 +22,51 @@ class Status(enum.Enum):
pickuppending = 10
class ShipmentStatus(enum.Enum):
unknown = 0
new = 1
booked = 2
collected = 3
inDelivery = 4
delivered = 5
returned = 6
def __ge__(self, other):
if self.__class__ is other.__class__:
return self.value >= other.value
return NotImplemented
def __gt__(self, other):
if self.__class__ is other.__class__:
return self.value > other.value
return NotImplemented
def __le__(self, other):
if self.__class__ is other.__class__:
return self.value <= other.value
return NotImplemented
def __lt__(self, other):
if self.__class__ is other.__class__:
return self.value < other.value
return NotImplemented
def get_shipment_status(shipment_info):
tracking = shipment_info.get('tracking', {})
if tracking.get('returned'):
return ShipmentStatus.returned
if tracking.get('delivered'):
return ShipmentStatus.delivered
if tracking.get('inDelivery'):
return ShipmentStatus.inDelivery
if tracking.get('collected'):
return ShipmentStatus.collected
if tracking.get('booked'):
return ShipmentStatus.booked
if tracking:
return ShipmentStatus.new
return ShipmentStatus.unknown
class PostalCode(db.Model):
postalcode = db.Column(db.String, primary_key=True)
address = db.Column(db.String, nullable=False)
@ -117,9 +162,9 @@ class FaceshieldRequest(db.Model):
def cache_shipment_info(self, fetch, id):
key = 'shipment_info_%d_%s' % (self.id, id)
res = cache.get(key)
if not res:
if res is None:
try:
res = fetch(id)
res = fetch(id) or {'error': 'No shipment found'}
except Exception as exc:
res = {'error': str(exc)}
if res.get('tracking', {}).get('delivered'):
@ -137,6 +182,15 @@ class FaceshieldRequest(db.Model):
def shipping_info(self):
return self.fetch_shipping_info()
@property
def shipment_status(self):
try:
if self.shipping_info:
return min([get_shipment_status(s) for s in self.shipping_info], default=ShipmentStatus.unknown)
except Exception as exc:
print(exc)
return ShipmentStatus.unknown
def changelog_default_user_id():
from flask_login import current_user

View File

@ -5,8 +5,16 @@
<a href="{{ url_for('faceshieldrequest.label', id=row.id) }}" class="icon" title="Render package label"><span class="glyphicon glyphicon-print"></span></a>
<a class="icon" role="button" data-toggle="popover" data-html="true" data-content="<b>Extras:</b><br />{{ row.extra or '' }}<hr><b>Remarks:</b><br />{{ row.remarks or '' }}"><span class="glyphicon glyphicon-comment"></span></a>
{% if row.shipping_id %}
<a class="icon" data-target="#fa_modal_window" title="View tracking" href="{{ url_for('faceshieldrequest.tracking', id=row.id) }}" data-toggle="modal">
<span class="fa fa-eye glyphicon glyphicon-plane"></span>
{% set color = {
'new': 'text-primary',
'booked': 'text-info',
'collected': 'text-warning',
'inDelivery': 'text-warning',
'delivered': 'text-success',
'returned': 'text-danger',
}.get(row.shipment_status.name, 'text-default') %}
<a class="icon" data-target="#fa_modal_window" title="View tracking ({{ row.shipment_status.name }})" href="{{ url_for('faceshieldrequest.tracking', id=row.id) }}" data-toggle="modal">
<span class="fa fa-eye glyphicon glyphicon-plane {{ color }}"></span>
</a>
{% endif %}
{% endblock %}