def format_datetime(dt_str, tz_from, tz_to): dt = parse_ts(dt_str) if dt is None: if dt_str: logging.error('Failed to format datetime: %s', dt_str) return dt_str return localize_datetime(dt, tz_from, tz_to).strftime('%Y-%m-%d %H:%M')
def discount(self): if hasattr(self, '_discount'): return self._discount from datetime import datetime from B2SUtils.common import parse_ts from common.coupons import get_item_specific_discount_coupon coupon = get_item_specific_discount_coupon(self.sale.mother_brand.id, id_sale=self.sale.id) if coupon and (not coupon.valid.from_ or parse_ts(coupon.valid.from_) <= datetime.now()) \ and (not coupon.valid.to_ or parse_ts(coupon.valid.to_) > datetime.now()): self._discount = float(coupon.reward.rebate.ratio) else: self._discount = 0 return self._discount
def get_product_default_display_price(sale, type_attr=None): ori_price = sale.get('price', {}).get('#text') or '' discount_price = sale.get('discount_price') valid_from = sale.get('discount', {}).get('@from') or '' valid_to = sale.get('discount', {}).get('@to') or '' if type_attr: if 'price' in type_attr: ori_price = type_attr['price'].get('#text') discount_price = type_attr.get('discount_price') else: for type_attr in as_list(sale.get('type', {}).get('attribute')): if 'price' in type_attr: ori_price = type_attr['price'].get('#text') discount_price = type_attr.get('discount_price') break if not discount_price: discount_price = ori_price now = datetime.datetime.utcnow() if valid_from and parse_ts(valid_from) > now \ or valid_to and parse_ts(valid_to) < now: discount_price = ori_price return float(ori_price), float(discount_price)
def _save_result(conn, detail_obj, update_only=False): vessels = db_utils.select(conn, "vessel", columns=("id", ), where={'mmsi': str(detail_obj.mmsi)}, limit=1) vessel_values = { 'name': detail_obj.name, 'imo': detail_obj.imo, 'mmsi': detail_obj.mmsi, 'cs': detail_obj.cs, 'type': detail_obj.type, 'country_isocode': detail_obj.country_isocode, 'photos': ujson.dumps(detail_obj.photos), } if len(vessels) > 0: id_vessel = vessels[0][0] db_utils.update(conn, "vessel", values=vessel_values, where={'id': id_vessel}) else: if update_only: return id_vessel = db_utils.insert(conn, "vessel", values=vessel_values, returning='id')[0] if detail_obj.departure_time and detail_obj.arrival_time: navi_values = { 'id_vessel': id_vessel, 'departure_locode': detail_obj.departure_locode, 'departure_time': parse_ts(detail_obj.departure_time), 'arrival_locode': detail_obj.arrival_locode, } navi_update_values = { 'arrival_time': parse_ts(detail_obj.arrival_time), } vessel_nav = db_utils.select(conn, "vessel_navigation", columns=("id", ), where=navi_values, order=('arrival_time__desc', ), limit=1) if len(vessel_nav) > 0: id_navi = vessel_nav[0][0] db_utils.update(conn, "vessel_navigation", values=navi_update_values, where={'id': id_navi}) else: if update_only: return navi_values.update(navi_update_values) id_navi = db_utils.insert(conn, "vessel_navigation", values=navi_values, returning='id')[0] if update_only: return for pos in detail_obj.positions: pos_values = { 'id_vessel': id_vessel, 'location': pos.location, 'longitude': pos.longitude, 'latitude': pos.latitude, 'heading': pos.heading, 'speed': pos.speed, 'time': parse_ts(pos.time), 'status': pos.status, } db_utils.insert(conn, "vessel_position", values=pos_values)
def _lock_expired(self, lock_time): return parse_ts(lock_time) + timedelta(seconds=15*60) \ < datetime.utcnow()