def get(self, bank_id): bank = banks.get_bank(bank_id) if not bank: abort(404, message="Not found. Supported banks = %s" % ",".join(banks.get_bank_ids())) data = bank.quote() quote_date = datetime.datetime.fromtimestamp(data["date"]) if "date" in data else None ancestor_key = ndb.Key("Bank", bank.name()) # sanity check if quotes are already stored if quote_date: qry = XchgRecord.query(XchgRecord.quote_date == quote_date, ancestor=ancestor_key) if qry.get(keys_only=True): return "", 204 for c, r in data["data"].iteritems(): rec = XchgRecord(parent=ancestor_key) rec.base_currency = c rec.to_currency = "TWD" rec.cash_buy = r[0] rec.cash_sell = r[1] rec.spot_buy = r[2] rec.spot_sell = r[3] if quote_date: rec.quote_date = quote_date # TODO: put quote records at once rec.put() return "", 201
def get(self, bank_id): args = self.parser.parse_args(strict=True) bank = banks.get_bank(bank_id) if not bank: abort(404, message="Not found. Supported banks = %s" % ",".join(banks.get_bank_ids())) try: start_date = datetime.datetime.strptime(args["start_date"], "%Y%m%d") # until the end of the day end_date = datetime.datetime.strptime(args["end_date"], "%Y%m%d") + datetime.timedelta(days=1) except: abort(400, message="Unable to parse start/end date") ancestor_key = ndb.Key("Bank", bank.name()) data = {"quotes": []} if (end_date - start_date) > datetime.timedelta(days=1): # FIXME we need to generate a different table for this instead of query ndb day by day for sd in daterange(start_date, end_date): ed = sd + datetime.timedelta(days=1) x = ( XchgRecord.get_latest_quotes(ancestor_key) .filter(XchgRecord.base_currency == args["currency"]) .filter(XchgRecord.quote_date <= ed) .filter(XchgRecord.quote_date >= sd) .get() ) if x: x.quote_date -= datetime.timedelta(hours=8) # FIXME: ndb records are stored as GMT +8 data["quotes"].append( [ x.quote_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), x.cash_buy, x.cash_sell, x.spot_buy, x.spot_sell, ] ) else: for x in ( XchgRecord.get_latest_quotes(ancestor_key) .filter(XchgRecord.base_currency == args["currency"]) .filter(XchgRecord.quote_date <= end_date) .filter(XchgRecord.quote_date >= start_date) .fetch() ): x.quote_date -= datetime.timedelta(hours=8) # FIXME: ndb records are stored as GMT +8 data["quotes"].append( [x.quote_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), x.cash_buy, x.cash_sell, x.spot_buy, x.spot_sell] ) return jsonify(data)
def read_role(name, norm=None, country="XX", code=None, bank_name=None, acc_country="XX", core=False): jurisdiction_id = jurisdictions.jurisdiction_by_code(country) code = re.sub(r"\s", "", code).lstrip("0") acc_type = banks.account_type(code) try: bank_code = banks.account_bank_code( code, offline=True ) if util.is_blank(bank_name) and not util.is_blank(code) else None bank_code = None if util.is_blank( bank_code) or util.contains_whitespace(bank_code) else bank_code except LookupError as e: bank_code = None if acc_type == "CASH" and banks.account_type(name) == "SWIFT": bank_code = name code = None name = None norm = None acc_bank_id = banks.get_bank(jurisdiction_id, bank_code)\ or banks.upsert_bank(jurisdiction_id, bank_code=bank_code, name=bank_name) acc_id = banks.upsert_account(code, acc_type, acc_bank_id, None) return acc_id elif acc_type == "IBAN": bank_country = code[0:2] if not bank_country or ( bank_country not in jurisdictions.cached_jurisdictions().keys()): print("Unrecognised account country: %s" % bank_country) bank_country = "XX" if acc_country != bank_country: if not util.is_blank(acc_country): print("Account %s with conflicting bank country: jurisdiction: '%s'; code: '%s'"\ %(code, acc_country, bank_country)) acc_country = bank_country elif acc_type == "SWIFT": acc_country = code[4:6] acc_jurisdiction_id = jurisdictions.jurisdiction_by_code(acc_country) acc_id = banks.get_account_by_code(code) if acc_id: org_id = banks.get_organisation_by_account(acc_id)\ or organisations.upsert_organisation(norm, core) # or organisations.upsert_organisation(norm, org_type, core) organisations.upsert_alias(name, org_id, jurisdiction_id) if norm != name: organisations.upsert_alias(norm, org_id, jurisdiction_id) else: org_id = organisations.upsert_organisation(norm, core) # org_id = organisations.upsert_organisation(norm, org_type, core) # TODO: Problem creating alias if the organisation is not yet persisted organisations.upsert_alias(name, org_id, jurisdiction_id) if norm != name: organisations.upsert_alias(norm, org_id, jurisdiction_id) acc_bank_id = banks.get_bank(jurisdiction_id, bank_code)\ or banks.upsert_bank(jurisdiction_id, bank_code=bank_code, name=bank_name) acc_id = banks.upsert_account(code, acc_type, acc_bank_id, org_id) return acc_id