示例#1
0
def process(req=None, user=None, runstat=None):
    if not req.get_acct_status_type() == STATUS_TYPE_UPDATE:
        return

    if not user:
        return log.err(
            "[Acct] Received an accounting update request but user[%s] not exists"
            % req.get_user_name())

    runstat.acct_update += 1
    online = store.get_online(req.get_nas_addr(), req.get_acct_sessionid())

    if not online:
        sessiontime = req.get_acct_sessiontime()
        updatetime = datetime.datetime.now()
        _starttime = updatetime - datetime.timedelta(seconds=sessiontime)

        online = utils.Storage(
            account_number=user['account_number'],
            nas_addr=req.get_nas_addr(),
            acct_session_id=req.get_acct_sessionid(),
            acct_start_time=_starttime.strftime("%Y-%m-%d %H:%M:%S"),
            framed_ipaddr=req.get_framed_ipaddr(),
            mac_addr=req.get_mac_addr(),
            nas_port_id=req.get_nas_portid(),
            billing_times=0,
            start_source=STATUS_TYPE_UPDATE)
        store.add_online(online)

    product = store.get_product(user['product_id'])
    if not product or product['product_policy'] not in (FEE_BUYOUT, FEE_MONTH,
                                                        FEE_TIMES):
        return

    if product['product_policy'] == FEE_TIMES:
        # PrePay fee times policy
        user_balance = store.get_user_balance(user['account_number'])
        sessiontime = decimal.Decimal(req.get_acct_sessiontime())
        billing_times = decimal.Decimal(online['billing_times'])
        acct_length = sessiontime - billing_times
        fee_price = decimal.Decimal(product['fee_price'])
        usedfee = acct_length / decimal.Decimal(3600) * fee_price
        usedfee = actual_fee = int(usedfee.to_integral_value())
        balance = user_balance - usedfee
        if balance < 0:
            balance = 0
            actual_fee = user_balance
        store.update_billing(
            utils.Storage(account_number=online['account_number'],
                          nas_addr=online['nas_addr'],
                          acct_session_id=online['acct_session_id'],
                          acct_start_time=online['acct_start_time'],
                          acct_session_time=req.get_acct_sessiontime(),
                          acct_length=int(acct_length.to_integral_value()),
                          acct_fee=usedfee,
                          actual_fee=actual_fee,
                          balance=balance,
                          is_deduct=1,
                          create_time=datetime.datetime.now().strftime(
                              "%Y-%m-%d %H:%M:%S")), True)
def process(req=None,user=None,runstat=None):
    if not req.get_acct_status_type() == STATUS_TYPE_UPDATE:
        return   

    if not user:
        return log.err("[Acct] Received an accounting update request but user[%s] not exists"%req.get_user_name())      

    runstat.acct_update += 1  
    online = store.get_online(req.get_nas_addr(),req.get_acct_sessionid())  

    if not online:         
        sessiontime = req.get_acct_sessiontime()
        updatetime = datetime.datetime.now()
        _starttime = updatetime - datetime.timedelta(seconds=sessiontime)       

        online = utils.Storage(
            account_number = user['account_number'],
            nas_addr = req.get_nas_addr(),
            acct_session_id = req.get_acct_sessionid(),
            acct_start_time = _starttime.strftime( "%Y-%m-%d %H:%M:%S"),
            framed_ipaddr = req.get_framed_ipaddr(),
            mac_addr = req.get_mac_addr(),
            nas_port_id = req.get_nas_portid(),
            billing_times = 0,
            start_source = STATUS_TYPE_UPDATE
        )
        store.add_online(online)    

    product = store.get_product(user['product_id'])
    if not product or product['product_policy'] not in (FEE_BUYOUT,FEE_MONTH,FEE_TIMES):
        return

    if product['product_policy'] == FEE_TIMES:
        # PrePay fee times policy
        user_balance = store.get_user_balance(user['account_number'])
        sessiontime = decimal.Decimal(req.get_acct_sessiontime())
        billing_times = decimal.Decimal(online['billing_times'])
        acct_length = sessiontime-billing_times
        fee_price = decimal.Decimal(product['fee_price'])
        usedfee = acct_length/decimal.Decimal(3600) * fee_price
        usedfee = actual_fee = int(usedfee.to_integral_value())
        balance = user_balance - usedfee
        if balance < 0 :  
            balance = 0
            actual_fee = user_balance
        store.update_billing(utils.Storage(
            account_number = online['account_number'],
            nas_addr = online['nas_addr'],
            acct_session_id = online['acct_session_id'],
            acct_start_time = online['acct_start_time'],
            acct_session_time = req.get_acct_sessiontime(),
            acct_length = int(acct_length.to_integral_value()),
            acct_fee = usedfee,
            actual_fee = actual_fee,
            balance = balance,
            is_deduct = 1,
            create_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S")
        ),True)
示例#3
0
def process(req=None, resp=None, user=None):
    product = store.get_product(user['product_id'])
    if not product: return resp
    input_limit = product['input_max_limit']
    output_limit = product['output_max_limit']
    if input_limit == 0 and output_limit == 0:
        return std_rate(resp, 0, 0)

    _vendor = req.vendor_id or '0'
    return rate_funcs[_vendor](resp, input_limit, output_limit)
示例#4
0
def process(req=None,resp=None,user=None):
    product = store.get_product(user['product_id']) 
    if not product:return resp
    input_limit = product['input_max_limit']
    output_limit = product['output_max_limit']
    if input_limit == 0 and output_limit == 0:
        return std_rate(resp,0,0)

    _vendor = req.vendor_id or '0'
    return rate_funcs[_vendor](resp,input_limit,output_limit)
示例#5
0
def process(req=None, resp=None, user=None):
    product = store.get_product(user['product_id'])
    session_timeout = int(store.get_param("max_session_timeout"))
    acct_policy = user['product_policy'] or FEE_BUYOUT
    if acct_policy in (FEE_BUYOUT, FEE_MONTH):
        expire_date = user.get('expire_date')
        _expire_datetime = datetime.datetime.strptime(
            expire_date + ' 23:59:59', "%Y-%m-%d %H:%M:%S")
        _datetime = datetime.datetime.now()
        if _datetime > _expire_datetime:
            session_timeout += (_expire_datetime - _datetime).seconds
    elif acct_policy == FEE_TIMES:
        balance = int(user.get("balance", 0))
        if balance == 0:
            session_timeout = 0
        else:
            time_len = balance * 3600 / product['fee_price']
            session_timeout = time_len

    if "Framed-Pool" in resp:
        if store.get_param("9_expire_addrpool") in resp['Framed-Pool']:
            session_timeout = 120

    resp['Session-Timeout'] = session_timeout

    input_limit = str(product['input_max_limit'])
    output_limit = str(product['output_max_limit'])
    _class = input_limit.zfill(8) + input_limit.zfill(8) + output_limit.zfill(
        8) + output_limit.zfill(8)
    resp['Class'] = _class

    if user['ip_address']:
        resp['Framed-IP-Address'] = user['ip_address']

    for attr in store.get_product_attrs(user['product_id']):
        try:
            _type = resp.dict[attr['attr_name']].type
            print _type
            resp[str(attr['attr_name'])] = get_type_val(
                _type, attr['attr_value'])
        except:
            import traceback
            traceback.print_exc()

    # for attr in store.get_user_attrs(user['account_number']):
    #     try:resp[attr.attr_name] = attr.attr_value
    #     except:pass

    return resp
示例#6
0
def process(req=None,resp=None,user=None):
    product = store.get_product(user['product_id'])
    session_timeout = int(store.get_param("max_session_timeout"))
    acct_policy = user['product_policy'] or FEE_BUYOUT
    if acct_policy in (FEE_BUYOUT,FEE_MONTH):
        expire_date = user.get('expire_date')
        _expire_datetime = datetime.datetime.strptime(expire_date+' 23:59:59',"%Y-%m-%d %H:%M:%S")
        _datetime = datetime.datetime.now()
        if _datetime > _expire_datetime:
            session_timeout += (_expire_datetime - _datetime).seconds 
    elif acct_policy == FEE_TIMES:
        balance = int(user.get("balance",0))
        if balance == 0:
            session_timeout = 0
        else:
            time_len = balance * 3600 / product['fee_price']
            session_timeout = time_len
    
    if "Framed-Pool" in resp:
        if store.get_param("9_expire_addrpool") in resp['Framed-Pool']:
            session_timeout = 120
    
    resp['Session-Timeout'] = session_timeout

    input_limit = str(product['input_max_limit'])
    output_limit = str(product['output_max_limit'])
    _class = input_limit.zfill(8) + input_limit.zfill(8) + output_limit.zfill(8) + output_limit.zfill(8)
    resp['Class'] = _class

    if user['ip_address']:
        resp['Framed-IP-Address'] = user['ip_address']

    for attr in store.get_product_attrs(user['product_id']):
        try:
            _type = resp.dict[attr['attr_name']].type
            print _type
            resp[str(attr['attr_name'])] = get_type_val(_type,attr['attr_value'])
        except:
            import traceback
            traceback.print_exc()

    
    # for attr in store.get_user_attrs(user['account_number']):
    #     try:resp[attr.attr_name] = attr.attr_value
    #     except:pass

    return resp
示例#7
0
def process(req=None,user=None,runstat=None):
    if not req.get_acct_status_type() == STATUS_TYPE_STOP:
        return  
    runstat.acct_stop += 1   
    ticket = req.get_ticket()
    if not ticket.nas_addr:
        ticket.nas_addr = req.source[0]

    _datetime = datetime.datetime.now() 
    online = store.get_online(ticket.nas_addr,ticket.acct_session_id)    
    if not online:
        session_time = ticket.acct_session_time 
        stop_time = _datetime.strftime( "%Y-%m-%d %H:%M:%S")
        start_time = (_datetime - datetime.timedelta(seconds=int(session_time))).strftime( "%Y-%m-%d %H:%M:%S")
        ticket.acct_start_time = start_time
        ticket.acct_stop_time = stop_time
        ticket.start_source= STATUS_TYPE_STOP
        ticket.stop_source = STATUS_TYPE_STOP
        store.add_ticket(ticket)
    else:
        store.del_online(ticket.nas_addr,ticket.acct_session_id)
        ticket.acct_start_time = online['acct_start_time']
        ticket.acct_stop_time= _datetime.strftime( "%Y-%m-%d %H:%M:%S")
        ticket.start_source = online['start_source']
        ticket.stop_source = STATUS_TYPE_STOP
        store.add_ticket(ticket)

        if not user:return 

        product = store.get_product(user['product_id'])
        if product and product['product_policy'] == FEE_TIMES:
            # PrePay fee times policy
            user_balance = store.get_user_balance(user['account_number'])
            sessiontime = decimal.Decimal(req.get_acct_sessiontime())
            billing_times = decimal.Decimal(online['billing_times'])
            acct_length = sessiontime-billing_times
            fee_price = decimal.Decimal(product['fee_price'])
            usedfee = acct_length/decimal.Decimal(3600) * fee_price
            usedfee = actual_fee = int(usedfee.to_integral_value())
            balance = user_balance - usedfee
            if balance < 0 :  
                balance = 0
                actual_fee = user_balance
            store.update_billing(utils.Storage(
                account_number = online['account_number'],
                nas_addr = online['nas_addr'],
                acct_session_id = online['acct_session_id'],
                acct_start_time = online['acct_start_time'],
                acct_session_time = req.get_acct_sessiontime(),
                acct_length = int(acct_length.to_integral_value()),
                acct_fee = usedfee,
                actual_fee = actual_fee,
                balance = balance,
                is_deduct = 1,
                create_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S")
            ),False)


    log.msg('%s Accounting stop request, remove online'%req.get_user_name(),level=logging.INFO)