def rateModify(params): """ Modify rate """ try: srv = zones.write_zone(params["tariff_id"]) if not srv["local"]: return jsonrpc.result({"status": "redirect", "server": srv["server"]}) if len(params) <= 2: return jsonrpc.result_error("InvalidRequest", {"status": "error", "message": "More arguments required"}) if "currency" in params: v = rates.constants.import_currency(params["currency"]) if v == None: return jsonrpc.result_error( "InvalidRequest", {"status": "error", "message": "Wrong currency: " + str(params["currency"])} ) params["currency"] = v tid, mid = params["tariff_id"], params["metric_id"] del params["tariff_id"], params["metric_id"] rates.modify(tid, mid, params) except Exception, e: LOG.error(e) return jsonrpc.result_error("ServerError", {"status": "error", "message": "Unable to modify rate"})
def rateAdd(params): """ Adds new rate """ try: srv = zones.write_zone(params["tariff_id"]) if not srv["local"]: return jsonrpc.result({"status": "redirect", "server": srv["server"]}) if "currency" in params: v = rates.constants.import_currency(params["currency"]) w = rates.constants.import_state(params["state"]) if v == None: return jsonrpc.result_error( "InvalidRequest", {"status": "error", "message": "Wrong currency: " + str(params["currency"])} ) if w == None: return jsonrpc.result_error( "InvalidRequest", {"status": "error", "message": "Wrong state: " + str(params["state"])} ) params["currency"] = v params["state"] = w o = rates.Rate(params) rates.add(o) except Exception, e: LOG.error(e) return jsonrpc.result_error("ServerError", {"status": "error", "message": "Unable to add new rate"})
def customerModify(params): """ Modify customer's record """ try: srv = zones.write_zone(params['id']) if not srv['local']: return jsonrpc.result({ 'status':'redirect', 'server': srv['server'] }) if len(params) == 1: return jsonrpc.result({ 'status':'ok' }) if 'state' in params: v = customers.constants.import_state(params['state']) if v == None or v == customers.constants.STATE_DELETED: return jsonrpc.result_error('InvalidRequest', {'status':'error', 'message':'Wrong state: ' + str(params['state'])}) params['state'] = v if 'wallet_mode' in params: v = customers.constants.import_wallet_mode(params['wallet_mode']) if v == None: return jsonrpc.result_error('InvalidRequest', {'status':'error', 'message':'Wrong wallet_mode: ' + str(params['wallet_mode'])}) params['wallet_mode'] = v customers.modify('id', params['id'], params) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to modify customer' })
def rateGet(params): try: ret = rates.get_by_metric(params["tariff_id"], params["metric_id"]) if not ret: srv = zones.write_zone(params["tariff_id"]) if not srv["local"]: return jsonrpc.result({"status": "redirect", "server": srv["server"]}) return jsonrpc.result_error("InvalidRequest", {"status": "error", "message": "Rate not found"}) except Exception, e: LOG.error(e) return jsonrpc.result_error("ServerError", {"status": "error", "message": "Unable to obtain rate list"})
def metricGet(request): """ Return metric object by name """ try: ret = metrics.get(request.get('id')) if not ret: return jsonrpc.result_error('InvalidRequest', { 'status': 'error', 'message': 'Metric not found' }) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to obtain metric' })
def tariffGet(params): try: ret = tariffs.get(params['id']) if not ret: srv = zones.write_zone(params['id']) if not srv['local']: return jsonrpc.result({ 'status':'redirect', 'server': srv['server'] }) return jsonrpc.result_error('InvalidRequest', { 'status': 'error', 'message': 'Tariff not found' }) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to obtain tariff' })
def taskRemove(request): try: tasks.remove('id', request['id'], request['time-destroy']) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to remove task' })
def sync(params): try: bc_sync.record(params['table'], params['record']) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to sync object' })
def tariffList(params): try: ret = map(lambda c: c.values, tariffs.get_all()) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to obtain tariff list' })
def syncList(params): try: for r in params['list']: bc_sync.record(params['table'], r) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to sync list of objects' })
def customerList(request): """ Returns a list of all registered customers """ try: ret = map(lambda c: c.values, customers.get_all()) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to obtain customer list' })
def metricList(request): """ Returns a list of all registered metrics """ try: ret = map(lambda m: m.values, metrics.get_all()) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to obtain metric list' })
def metricAdd(request): """ Adds new billing metric """ try: m = metrics.Metric(request) metrics.add(m) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to add new metric' })
def rateList(params): try: if len(params) == 0: ret = map(lambda c: c.values, rates.get_all()) elif len(params) == 1: ret = map(lambda c: c.values, rates.get_by_tariff(params["tariff_id"])) except Exception, e: LOG.error(e) return jsonrpc.result_error("ServerError", {"status": "error", "message": "Unable to obtain rate list"})
def rateRemove(params): try: srv = zones.write_zone(params["tariff_id"]) if not srv["local"]: return jsonrpc.result({"status": "redirect", "server": srv["server"]}) rates.remove(params["tariff_id"], params["metric_id"]) except Exception, e: LOG.error(e) return jsonrpc.result_error("ServerError", {"status": "error", "message": "Unable to remove rate"})
def tariffRemove(params): try: srv = zones.write_zone(params['id']) if not srv['local']: return jsonrpc.result({ 'status':'redirect', 'server': srv['server'] }) tariffs.remove('id', params['id']) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to remove tariff' })
def tariffAdd(params): try: srv = zones.write_zone(params['id']) if not srv['local']: return jsonrpc.result({ 'status':'redirect', 'server': srv['server'] }) c = tariffs.Tariff(params) tariffs.add(c) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to add new tariff' })
def tariffModify(params): try: srv = zones.write_zone(params['id']) if not srv['local']: return jsonrpc.result({ 'status':'redirect', 'server': srv['server'] }) if len(params) == 1: return jsonrpc.result({ 'status':'ok' }) if 'state' in params: v = tariffs.constants.import_state(params['state']) if v == None or v == tariffs.constants.STATE_DELETED: return jsonrpc.result_error('InvalidRequest', { 'status':'error', 'message':'Wrong state: ' + str(params['state'])}) params['state'] = v tariffs.modify('id', params['id'], params) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to modify tariff' })
def customerAdd(params): """ Adds new customer account """ try: srv = zones.write_zone(params['id']) if not srv['local']: return jsonrpc.result({ 'status':'redirect', 'server': srv['server'] }) wallet_mode = customers.constants.import_wallet_mode(params['wallet_mode']) if wallet_mode == None: return jsonrpc.result_error('InvalidRequest', {'status':'error', 'message':'Wrong wallet_mode: ' + str(params['wallet_mode'])}) params['wallet_mode'] = wallet_mode c = customers.Customer(params) customers.add(c) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to add new customer' })
def customerRemove(params): """ Remove customer by name """ try: srv = zones.write_zone(params['id']) if not srv['local']: return jsonrpc.result({ 'status':'redirect', 'server': srv['server'] }) c = customers.remove('id', params['id']) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to remove customer' })
def customerDeposit(params): """ Make deposit to customer """ try: srv = zones.write_zone(params['id']) if not srv['local']: return jsonrpc.result({ 'status':'redirect', 'server': srv['server'] }) if params['value'] != 0: customers.deposit(params['id'], params['value']) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to make a deposit' })
def taskModify(request): try: tasks.update(request['id'], { 'value': request['value'], 'target_user': request['user'], 'target_uuid': request['uuid'], 'target_descr': request['descr'], }, request['time'] ) except Exception, e: LOG.error(e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to modify task' })
def taskAdd(request): """ Open new billing task """ if request['time-create'] == 0: request['time-create'] = int(time.time()) try: rid, rate = ('', 0) customer = customers.get(request['customer'], typ='id') if customer: rid, rate = rates.resolve(request['type'], customer.tariff_id) if not rid: LOG.error("task(%s): Unable to find rate for metric", request['uuid']) else: LOG.error("task(%s): Unknown customer (%s)", request['uuid'], request['customer']) t = tasks.Task({ 'group_id': GROUPID.next(), 'base_id': request['uuid'], 'customer': request['customer'], 'metric_id': request['type'], 'rate_id': rid, 'rate': rate, 'value': request['value'], 'time_create': request['time-create'], 'time_check': request['time-create'], 'time_destroy': request['time-destroy'], 'target_user': request['user'], 'target_uuid': request['uuid'], 'target_descr': request['descr'], }) tasks.add(t) except Exception, e: LOG.exception("Unable to add new task: %s", e) return jsonrpc.result_error('ServerError', { 'status': 'error', 'message': 'Unable to add new task' })