def store_error(url, code): """Store an error for a given url. Url can also be a list of urls.""" if isinstance(url, list): urls = url cache.set_multi({u: code for u in urls}, expires=ERROR_TTL) return cache.set(url, code, expires=ERROR_TTL)
def get_currencies(cls): """Get the list of currencies from free.currencyconverterapi.com Return: A list of valid currencies """ memcache_timeout = 60 * 30 # try to get from memcache currencies = cache.get(u'CURRENCIES') if currencies: return currencies # not found in memcache, get from api service try: data = requests.get(cls.CURRENCY_LIST_URL) data = json.loads(data.content) cache.set(u'CURRENCIES', data['results'], timeout=memcache_timeout) return data['results'] except Exception as e: return Exception( 'Unable to load the list of available currencies'.format( e.message))
def load_from_json(cls, json_uri=None): """Get the pricelist from the specified json file and load in memcache Args: json_uri: The uri of the pricelist file. Raises: An exception if the operation fails """ if json_uri is None: json_uri = app.config['PRICING_JSON_URI'] try: data = json.load(open(json_uri)) products = {} for p in data[u'prices']: products.update({ p[u'product_id']: { k: v for k, v in p.iteritems() } }) cache.set(u'PRODUCTS', products, cls.MEMCACHE_TIMEOUT) cache.set(u'VAT_BANDS', data[u'vat_bands'], cls.MEMCACHE_TIMEOUT) except Exception as e: raise Exception(u'Unable to load the data from {}: {}'.format(json_uri, e.message))
def set_progress(cls, user_id, episode_id, progress): id = cls._make_id(user_id, episode_id) cls.run(cls.get_table().get(id).replace({ "id": id, "episode_id": episode_id, "user_id": user_id, "progress": progress })) cache.set("progress_%s" % id, progress)
def set_progress(cls, user_id, episode_id, progress): id = cls._make_id(user_id, episode_id) cls.run( cls.get_table().get(id).replace({ "id": id, "episode_id": episode_id, "user_id": user_id, "progress": progress }) ) cache.set("progress_%s" % id, progress)
def get_by_id(cls, id): """Gets the Client with the given id.""" # Overriding this to return trusted clients. logging.debug("Retrieving client with id %s (trusted: %s)" % (id, id in TRUSTED_CLIENTS)) client = TRUSTED_CLIENTS.get(id) if not client: client = cache.get("client-%s" % id) if not client: client = cls.from_dict(cls.get(id)) cache.set("client-%s" % id, client) return client
def get_exchange_rate(cls, currency): """Get the exchange rate from GBP to given currency Args: currency: The id of the currency (3 chars) Return: The exchange rate from GBP to the given currency """ memcache_timeout = 60 * 5 currency = currency.upper() currencies = cls.get_currencies() # check if the given currency is in the allowed currencis if currency not in currencies: raise Exception('Invalid currency: {}'.format(currency)) # set the exchange currencies exchange = u'GBP_{}'.format(currency) # try to get the exchange rate from memcache rate = cache.get(exchange) if rate: return rate # the exchange rate is not in memcache get api service try: url = cls.EXCHANGE_RATE_URL.format(exchange) data = requests.get(url) data = json.loads(data.content) rate = data[exchange][u'val'] except Exception as e: raise Exception(u'Unable to retrieve the rate for {}'.format( e.message)) # try to convert the rate to float try: rate = float(rate) cache.set(exchange, rate, memcache_timeout) return rate except Exception as e: raise Exception( u'Unable to convert the rate for {} -> {}: {}'.format( exchange, rate, e.message))
def create_session_token(user): """Creates a new session token, and stores it on the session.""" token = os.urandom(16).encode("hex") cache.set("session-" + token, str(user.id), 600) session["user_id"] = str(user.id) session["token"] = token
def _save_next(next): """Save the url to go to after login, and return te key with which it is associated.""" key = os.urandom(16).encode("hex") cache.set(key, next, 600) return key
def save(self): """Override save(), because we want to store granttokens in the cache.""" s = cPickle.dumps(self) cache.set("GRANT_TOKEN_"+self.id, s)