示例#1
0
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"})
示例#2
0
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"})
示例#3
0
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' })
示例#4
0
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"})
示例#5
0
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' })
示例#6
0
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' })
示例#7
0
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' })
示例#8
0
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' })
示例#9
0
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' })
示例#10
0
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' })
示例#11
0
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' })
示例#12
0
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' })
示例#13
0
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' })
示例#14
0
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"})
示例#15
0
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"})
示例#16
0
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' })
示例#17
0
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' })
示例#18
0
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' })
示例#19
0
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' })
示例#20
0
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' })
示例#21
0
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' })
示例#22
0
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' })
示例#23
0
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' })