def check_marketo_complete(course_id, email, course_map): """ check if a course is already marked as complete in Marketo """ # email = '*****@*****.**' # import pdb; pdb.set_trace() mkto_field_id = course_map[course_id] try: mc = get_marketo_client() complete = mc.execute(method='get_leads', filtr='email', values=(email,), fields=(mkto_field_id,)) if len(complete) > 1: raise MarketoException completeness = complete[0][mkto_field_id] if completeness: # only cache True cachekey = cache_key('marketo_complete_cache', course=course_id, email=email) # our version of keyedcache doesn't recognize a cache is # enabled in our multi-cache setup. if not cache_enabled(): cache_enable() cache_set(cachekey, value=completeness) return completeness except MarketoException: # if we can't connect to Marketo or have some error with API, # don't continue trying to check completion return True
def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) if use_db: try: setting = cache_get(ck) except NotCachedError, nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting)
def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) grp = overrides.get(group, None) if grp and key in grp: val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) elif use_db: try: setting = cache_get(ck) except NotCachedError, nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting)
def find_setting(group, key): """Get a setting or longsetting by group and key, cache and return it.""" setting = None use_db, overrides = (True, {}) ck = cache_key('Setting', group, key) grp = overrides.get(group, None) if grp and key in grp: val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) elif use_db: try: setting = cache_get(ck) except NotCachedError, nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get(key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting)
def _dict(data): """ Dictionaries. """ key = cache_key('json', data) try: ret = cache_get(key) log.debug('got json serialized data from cache for %s', key) except NotCachedError: ret = dict([ (k, _any(v)) for k, v in data.iteritems() ]) log.debug('setting json serialized data to cache for %s', key) cache_set(key, value=ret) return ret
def testDisable(self): keyedcache.cache_set("disabled", value=False) v = keyedcache.cache_get("disabled") self.assertEqual(v, False) keyedcache.cache_enable(False) keyedcache.cache_set("disabled", value=True) try: keyedcache.cache_get("disabled") self.fail("should have raised NotCachedError") except keyedcache.NotCachedError, nce: key = keyedcache.cache_key("disabled") self.assertEqual(nce.key, key)
def cached_check_marketo_complete(course_id, email, course_map): # email = '*****@*****.**' cachekey = cache_key('marketo_complete_cache', course=course_id, email=email) try: value = cache_get(cachekey) except NotCachedError: value = None if value is None: # import pdb; pdb.set_trace() return check_marketo_complete(course_id, email, course_map) else: return value
def testDisable(self): keyedcache.cache_set('disabled', value=False) v = keyedcache.cache_get('disabled') self.assertEqual(v, False) keyedcache.cache_enable(False) keyedcache.cache_set('disabled', value=True) try: keyedcache.cache_get('disabled') self.fail('should have raised NotCachedError') except keyedcache.NotCachedError, nce: key = keyedcache.cache_key('disabled') self.assertEqual(nce.key, key)
def _list(data): """ Lists. """ key = cache_key('jsonx', data) try: ret = cache_get(key) log.debug('got json serialized list from cache for %s', key) except NotCachedError: ret = [ _any(v) for v in data ] log.debug('setting json serialized list to cache for %s', key) cache_set(key, value=ret) return ret
def get_and_cache(self, **kwargs): key = cache_key('PlayaEvent', 'all', **kwargs) try: results = cache_get(key) log.debug('got all events from cache') except NotCachedError: log.debug('getting events from db') if kwargs: results = self.filter(**kwargs) else: results = self.all() results = list(results) cache_set(key, value=results, length=60*60*24) # set for one day return results
def testDisable(self): keyedcache.cache_set('disabled', value=False) v = keyedcache.cache_get('disabled') self.assertEqual(v, False) keyedcache.cache_enable(False) keyedcache.cache_set('disabled', value=True) try: keyedcache.cache_get('disabled') self.fail('should have raised NotCachedError') except keyedcache.NotCachedError as nce: key = keyedcache.cache_key('disabled') self.assertEqual(nce.key, key) keyedcache.cache_enable() v2 = keyedcache.cache_get('disabled') # should still be False, since the cache was disabled self.assertEqual(v2, False)
def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) grp = overrides.get(group, None) if grp and key in grp: val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) elif use_db: try: setting = cache_get(ck) except NotCachedError as nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting) else: grp = overrides.get(group, None) if grp and grp.has_key(key): val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) if not setting: raise SettingNotSet(key, cachekey=ck) return setting
def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None backend = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) if backend.is_editable: setting = backend.get_value(group, key) else: grp = overrides.get(group, None) if grp and grp.has_key(key): val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) if not setting: raise SettingNotSet(key, cachekey=ck) return setting
def cache_key(self, *args, **kwargs): return cache_key('Setting', self.site, self.group, self.key)
def testDualKey(self): v = keyedcache.cache_key('test', 2) self.assertEqual(v, 'test::2')
def testPairedDualKey(self): v = keyedcache.cache_key('test', 3, more='yes') self.assertEqual(v, 'test::3::more::yes')
def testPairedKey(self): v = keyedcache.cache_key('test', more='yes') self.assertEqual(v, keyedcache.CACHE_PREFIX + '::test::more::yes')
def testSimpleKey(self): v = keyedcache.cache_key('test') self.assertEqual(v, 'test')
def cache_key(self, *args, **kwargs): # note same cache pattern as Setting. This is so we can look up in one check. # they can't overlap anyway, so this is moderately safe. At the worst, the # Setting will override a LongSetting. return cache_key('Setting', self.group, self.key)
def calculate(self, cart, contact): """ Based on the chosen UPS method, we will do our call to UPS and see how much it will cost. We will also need to store the results for further parsing and return via the methods above """ from satchmo_store.shop.models import Config settings = config_get_group('shipping.modules.ups') self.delivery_days = _("3 - 4") #Default setting for ground delivery shop_details = Config.objects.get_current() # Get the code and description for the packaging container = settings.SHIPPING_CONTAINER.value container_description = settings.SHIPPING_CONTAINER.choices[int(container)][1] configuration = { 'xml_key': settings.XML_KEY.value, 'account': settings.ACCOUNT.value, 'userid': settings.USER_ID.value, 'password': settings.USER_PASSWORD.value, 'container': container, 'container_description': container_description, 'pickup': settings.PICKUP_TYPE.value, 'ship_type': self.service_type_code, 'shop_details':shop_details, } shippingdata = { 'single_box': False, 'config': configuration, 'contact': contact, 'cart': cart, 'shipping_address' : shop_details, 'shipping_phone' : shop_details.phone, 'shipping_country_code' : shop_details.country.iso2_code } if settings.SINGLE_BOX.value: log.debug("Using single-box method for ups calculations.") box_weight = Decimal("0.00") for product in cart.get_shipment_list(): if product.smart_attr('weight') is None: log.warn("No weight on product (skipping for ship calculations): %s", product) else: box_weight += product.smart_attr('weight') if product.smart_attr('weight_units') and product.smart_attr('weight_units') != "": box_weight_units = product.smart_attr('weight_units') else: log.warn("No weight units for product") if box_weight < Decimal("0.1"): log.debug("Total box weight too small, defaulting to 0.1") box_weight = Decimal("0.1") shippingdata['single_box'] = True shippingdata['box_weight'] = '%.1f' % box_weight shippingdata['box_weight_units'] = box_weight_units.upper() total_weight = 0 for product in cart.get_shipment_list(): try: total_weight += product.smart_attr('weight') except TypeError: pass signals.shipping_data_query.send(Shipper, shipper=self, cart=cart, shippingdata=shippingdata) c = Context(shippingdata) t = loader.get_template('shipping/ups/request.xml') request = t.render(c) self.is_valid = False if settings.LIVE.value: connection = settings.CONNECTION.value else: connection = settings.CONNECTION_TEST.value cachekey = cache_key( 'UPS_SHIP', #service_type = self.service_type_code, weight = str(total_weight), country = shop_details.country.iso2_code, zipcode = contact.shipping_address.postal_code) try: tree = cache_get(cachekey) except NotCachedError: tree = None if tree is not None: self.verbose_log('Got UPS info from cache [%s]', cachekey) else: self.verbose_log("Requesting from UPS [%s]\n%s", cachekey, request) cache_set(cachekey, value=request, length=600) tree = self._process_request(connection, request) self.verbose_log("Got from UPS [%s]:\n%s", cachekey, self.raw) cache_set(cachekey, value=tree) try: status_code = tree.getiterator('ResponseStatusCode') status_val = status_code[0].text self.verbose_log("UPS Status Code for cart #%s = %s", int(cart.id), status_val) except AttributeError: status_val = "-1" if status_val == '1': self.is_valid = False self._calculated = False all_rates = tree.getiterator('RatedShipment') for response in all_rates: if self.service_type_code == response.find('.//Service/Code').text: self.charges = response.find('.//TotalCharges/MonetaryValue').text if response.find('.//GuaranteedDaysToDelivery').text: self.delivery_days = response.find('.//GuaranteedDaysToDelivery').text self.is_valid = True self._calculated = True if not self.is_valid: self.verbose_log("UPS Cannot find rate for code: %s [%s]", self.service_type_code, self.service_type_text) else: self.is_valid = False self._calculated = False try: errors = tree.find('.//Error') log.info("UPS %s Error: Code %s - %s" % (errors[0].text, errors[1].text, errors[2].text)) except AttributeError: log.info("UPS error - cannot parse response:\n %s", self.raw) if self.is_valid and settings.TIME_IN_TRANSIT.value: self.verbose_log('Now getting time in transit for cart') self.time_in_transit(contact, cart)
def cache_key(self, *args, **kwargs): # note same cache pattern as Setting. This is so we can look up in one check. # they can't overlap anyway, so this is moderately safe. At the worst, the # Setting will override a LongSetting. return cache_key('Setting', self.site, self.group, self.key)
def testDualKey(self): v = keyedcache.cache_key('test', 2) self.assertEqual(v, keyedcache.CACHE_PREFIX + '::test::2')
def calculate(self, cart, contact): """ Based on the chosen UPS method, we will do our call to UPS and see how much it will cost. We will also need to store the results for further parsing and return via the methods above """ from satchmo_store.shop.models import Config settings = config_get_group('shipping.modules.ups') self.delivery_days = _("3 - 4") #Default setting for ground delivery shop_details = Config.objects.get_current() # Get the code and description for the packaging container = settings.SHIPPING_CONTAINER.value container_description = settings.SHIPPING_CONTAINER.choices[int( container)][1] configuration = { 'xml_key': settings.XML_KEY.value, 'account': settings.ACCOUNT.value, 'userid': settings.USER_ID.value, 'password': settings.USER_PASSWORD.value, 'container': container, 'container_description': container_description, 'pickup': settings.PICKUP_TYPE.value, 'ship_type': self.service_type_code, 'shop_details': shop_details, } shippingdata = { 'single_box': False, 'config': configuration, 'contact': contact, 'cart': cart, 'shipping_address': shop_details, 'shipping_phone': shop_details.phone, 'shipping_country_code': shop_details.country.iso2_code } if settings.SINGLE_BOX.value: log.debug("Using single-box method for ups calculations.") box_weight = Decimal("0.00") for product in cart.get_shipment_list(): if product.smart_attr('weight') is None: log.warn( "No weight on product (skipping for ship calculations): %s", product) else: box_weight += product.smart_attr('weight') if product.smart_attr('weight_units') and product.smart_attr( 'weight_units') != "": box_weight_units = product.smart_attr('weight_units') else: log.warn("No weight units for product") if box_weight < Decimal("0.1"): log.debug("Total box weight too small, defaulting to 0.1") box_weight = Decimal("0.1") shippingdata['single_box'] = True shippingdata['box_weight'] = '%.1f' % box_weight shippingdata['box_weight_units'] = box_weight_units.upper() total_weight = 0 for product in cart.get_shipment_list(): try: total_weight += product.smart_attr('weight') except TypeError: pass signals.shipping_data_query.send(Shipper, shipper=self, cart=cart, shippingdata=shippingdata) c = Context(shippingdata) t = loader.get_template('shipping/ups/request.xml') request = t.render(c) self.is_valid = False if settings.LIVE.value: connection = settings.CONNECTION.value else: connection = settings.CONNECTION_TEST.value cachekey = cache_key( 'UPS_SHIP', #service_type = self.service_type_code, weight=str(total_weight), country=shop_details.country.iso2_code, zipcode=contact.shipping_address.postal_code) try: tree = cache_get(cachekey) except NotCachedError: tree = None if tree is not None: self.verbose_log('Got UPS info from cache [%s]', cachekey) else: self.verbose_log("Requesting from UPS [%s]\n%s", cachekey, request) cache_set(cachekey, value=request, length=600) tree = self._process_request(connection, request) self.verbose_log("Got from UPS [%s]:\n%s", cachekey, self.raw) cache_set(cachekey, value=tree) try: status_code = tree.getiterator('ResponseStatusCode') status_val = status_code[0].text self.verbose_log("UPS Status Code for cart #%s = %s", int(cart.id), status_val) except AttributeError: status_val = "-1" if status_val == '1': self.is_valid = False self._calculated = False all_rates = tree.getiterator('RatedShipment') for response in all_rates: if self.service_type_code == response.find( './/Service/Code').text: self.charges = response.find( './/TotalCharges/MonetaryValue').text if response.find('.//GuaranteedDaysToDelivery').text: self.delivery_days = response.find( './/GuaranteedDaysToDelivery').text self.is_valid = True self._calculated = True if not self.is_valid: self.verbose_log("UPS Cannot find rate for code: %s [%s]", self.service_type_code, self.service_type_text) else: self.is_valid = False self._calculated = False try: errors = tree.find('.//Error') log.info("UPS %s Error: Code %s - %s" % (errors[0].text, errors[1].text, errors[2].text)) except AttributeError: log.info("UPS error - cannot parse response:\n %s", self.raw) if self.is_valid and settings.TIME_IN_TRANSIT.value: self.verbose_log('Now getting time in transit for cart') self.time_in_transit(contact, cart)
def ups_time_in_transit(self, contact, pickup_date=None, price=None, test=False): """Calculate est delivery days for a zipcode, from Store Zipcode""" from satchmo_store.shop.models import Config delivery_days = None if pickup_date is None: pickup_date = timezone.now() + timezone.timedelta(days=1) # UPS doesn't pick up on weekends if pickup_date.day == 5: pickup_date += timezone.timedelta(days=2) if pickup_date.day == 6: pickup_date += timezone.timedelta(days=1) if price is None: price = Decimal('10.0') shipaddr = contact.shipping_address shop_details = Config.objects.get_current() settings = config_get_group('shipping.modules.ups') configuration = { 'xml_key': settings.XML_KEY.value, 'account': settings.ACCOUNT.value, 'userid': settings.USER_ID.value, 'password': settings.USER_PASSWORD.value, 'container': settings.SHIPPING_CONTAINER.value, 'pickup': settings.PICKUP_TYPE.value, 'shop_details': shop_details, } shippingdata = { 'config': configuration, 'zipcode': shipaddr.postal_code, 'contact': contact, 'shipping_address': shop_details, 'shipping_phone': shop_details.phone, 'shipping_country_code': shop_details.country.iso2_code, 'pickup_date': pickup_date.strftime('%Y%m%d'), 'price': "%.2f" % price } c = Context(shippingdata) t = loader.get_template('shipping/ups/transit_request.xml') request = t.render(c) if settings.LIVE.value and not test: connection = 'https://wwwcie.ups.com/ups.app/xml/TimeInTransit' else: connection = 'https://onlinetools.ups.com/ups.app/xml/TimeInTransit' cachekey = cache_key("UPS-TIT", shipaddr.postal_code, pickup_date.strftime('%Y%m%d'), "%.2f" % price) try: ups = cache_get(cachekey) except NotCachedError: ups = None if ups is None: log.debug('Requesting from UPS: %s\n%s', connection, request) conn = urllib.request.Request(url=connection, data=request.encode("utf-8")) f = urllib.request.urlopen(conn) all_results = f.read() self.verbose_log("Received from UPS:\n%s", all_results) ups = fromstring(all_results) needs_cache = True else: needs_cache = False ok = False try: ok = ups.find('Response/ResponseStatusCode').text == '1' except AttributeError: log.warning('Bad response from UPS TimeInTransit') pass if not ok: try: response = ups.find('Response/ResponseStatusDescription').text log.warning('Bad response from UPS TimeInTransit: %s', response) except AttributeError: log.warning('Unknown UPS TimeInTransit response') if ok: services = ups.findall('TransitResponse/ServiceSummary') for service in services: transit_code = service.find('Service/Code').text if self.service_type_code == TRANSIT_CODE_MAP.get( transit_code, ''): try: delivery_days = service.find( 'EstimatedArrival/BusinessTransitDays').text self.verbose_log('Found delivery days %s for %s', delivery_days, self.service_type_code) except AttributeError: log.warning( 'Could not find BusinessTransitDays in UPS response' ) try: delivery_days = int(delivery_days) except ValueError: pass break if delivery_days is not None and needs_cache: cache_set(cachekey, value=ups, length=600) return delivery_days
def cache_key(self, *args, **kwargs): keys = [self.__class__.__name__, self] keys.extend(args) return keyedcache.cache_key(keys, **kwargs)
def testSimpleKey(self): v = keyedcache.cache_key("test") self.assertEqual(v, keyedcache.CACHE_PREFIX + "::test")
def testDualKey(self): v = keyedcache.cache_key("test", 2) self.assertEqual(v, keyedcache.CACHE_PREFIX + "::test::2")
def ups_time_in_transit(self, contact, pickup_date = None, price=None, test=False): """Calculate est delivery days for a zipcode, from Store Zipcode""" from satchmo_store.shop.models import Config delivery_days = None if pickup_date is None: pickup_date = timezone.now() + timezone.timedelta(days=1) # UPS doesn't pick up on weekends if pickup_date.day == 5: pickup_date += timezone.timedelta(days=2) if pickup_date.day == 6: pickup_date += timezone.timedelta(days=1) if price is None: price = Decimal('10.0') shipaddr = contact.shipping_address shop_details = Config.objects.get_current() settings = config_get_group('shipping.modules.ups') configuration = { 'xml_key': settings.XML_KEY.value, 'account': settings.ACCOUNT.value, 'userid': settings.USER_ID.value, 'password': settings.USER_PASSWORD.value, 'container': settings.SHIPPING_CONTAINER.value, 'pickup': settings.PICKUP_TYPE.value, 'shop_details':shop_details, } shippingdata = { 'config': configuration, 'zipcode': shipaddr.postal_code, 'contact': contact, 'shipping_address' : shop_details, 'shipping_phone' : shop_details.phone, 'shipping_country_code' : shop_details.country.iso2_code, 'pickup_date' : pickup_date.strftime('%Y%m%d'), 'price' : "%.2f" % price } c = Context(shippingdata) t = loader.get_template('shipping/ups/transit_request.xml') request = t.render(c) if settings.LIVE.value and not test: connection = 'https://wwwcie.ups.com/ups.app/xml/TimeInTransit' else: connection = 'https://onlinetools.ups.com/ups.app/xml/TimeInTransit' cachekey = cache_key("UPS-TIT", shipaddr.postal_code, pickup_date.strftime('%Y%m%d'), "%.2f" % price) try: ups = cache_get(cachekey) except NotCachedError: ups = None if ups is None: log.debug('Requesting from UPS: %s\n%s', connection, request) conn = urllib2.Request(url=connection, data=request.encode("utf-8")) f = urllib2.urlopen(conn) all_results = f.read() self.verbose_log("Received from UPS:\n%s", all_results) ups = fromstring(all_results) needs_cache = True else: needs_cache = False ok = False try: ok = ups.find('Response/ResponseStatusCode').text == '1' except AttributeError: log.warning('Bad response from UPS TimeInTransit') pass if not ok: try: response = ups.find('Response/ResponseStatusDescription').text log.warning('Bad response from UPS TimeInTransit: %s', response) except AttributeError: log.warning('Unknown UPS TimeInTransit response') if ok: services = ups.findall('TransitResponse/ServiceSummary') for service in services: transit_code = service.find('Service/Code').text if self.service_type_code == TRANSIT_CODE_MAP.get(transit_code, ''): try: delivery_days = service.find('EstimatedArrival/BusinessTransitDays').text self.verbose_log('Found delivery days %s for %s', delivery_days, self.service_type_code) except AttributeError: log.warning('Could not find BusinessTransitDays in UPS response') try: delivery_days = int(delivery_days) except ValueError: pass break if delivery_days is not None and needs_cache: cache_set(cachekey, value=ups, length=600) return delivery_days
def cache_key(self, *args, **kwargs): return cache_key('OverrideSetting', self.site, self.group, self.key)
def cache_key(self, *args, **kwargs): return cache_key('OverrideSetting', self.group, self.key)
def testSimpleKey(self): v = keyedcache.cache_key('test') self.assertEqual(v, keyedcache.CACHE_PREFIX + '::test')
def cache_key(self, *args, **kwargs): return cache_key("Setting", self.site, self.group, self.key)
def cache_key(self, *args, **kwargs): return cache_key('Setting', self.group, self.key)
def testPairedDualKey(self): v = keyedcache.cache_key("test", 3, more="yes") self.assertEqual(v, keyedcache.CACHE_PREFIX + "::test::3::more::yes")