fix: improve parameter handling and logging in various functions

1 parent ce60ddfa
......@@ -139,7 +139,8 @@ def get_params(params, alternate=None):
get_params('devel', False)
"""
settings = get_settings()
result = settings.get(params, alternate).strip() or None
result = settings.get(params, alternate) or None
result = result and result.strip() or None
return result and result or alternate
......@@ -808,7 +809,7 @@ def pbb_biller_split():
if not 'pbb_biller' in settings:
return None, None
biller = re.sub('\D', '', settings['pbb_biller'])
biller = re.sub(r'\D', '', settings['pbb_biller'])
return biller[:2], biller[2:]
......@@ -1019,7 +1020,7 @@ def terbilang(n):
def number_only(value):
return re.sub('\D', "", value)
return re.sub(r'\D', "", value)
def set_user_log(message, request, logobj=None, user_name=None):
......
......@@ -5,6 +5,8 @@ import json
import sys
from datetime import datetime, date
from requests import HTTPError
import requests
from pyramid.i18n import TranslationStringFactory
from pyramid_rpc.jsonrpc import JsonRpcError
......@@ -268,7 +270,7 @@ def get_seconds(current_time=None):
if not current_time:
current_time = datetime.utcnow()
durasi = current_time - begin_unix_time
log.info(f"{current_time} - {begin_unix_time}")
log.debug("%s - %s %s", current_time, begin_unix_time, durasi)
return int(durasi.total_seconds())
......@@ -294,6 +296,7 @@ def json_rpc_header(userid, password, key=None):
password = str(password)
signature = hmac.new(key=str.encode(password), msg=str.encode(value),
digestmod=hashlib.sha256).digest()
log.debug("signature: %s", signature)
if sys.version < '3':
encoded_signature = base64.encodestring(signature).replace('\n', '')
......@@ -319,7 +322,7 @@ def validate_time(request):
lima_menit = int(settings["diff_server_time"])
if not log.parent.level==logging.DEBUG and abs(now - time_stamp) > lima_menit:
log.info(f"req time {time_stamp} server time {now}")
log.error("req time %s server time %s diff %s", time_stamp, now, abs(now - time_stamp))
raise JsonRpcInvalidTimeError
return time_stamp
......@@ -351,21 +354,21 @@ def send_rpc(auth, message):
params = dict(data=message)
data = get_jsonrpc(auth["method"], params)
timeout = 'timeout' in auth and int(auth['timeout']) or 5
log.info("URL:{} timeout:{} detik".format(url, timeout))
log.warning("REQUEST {}".format(data))
log.debug("URL:%s timeout:%s detik", url, timeout)
log.warning("REQUEST %s", data)
try:
results = requests.post(url, json=data, headers=headers,
resp = requests.post(url, json=data, headers=headers,
timeout=timeout) # data=jsondata,
except Exception as e:
except HTTPError as e:
log.warning(str(e))
return
if results.status_code != 200:
log.info(results)
log.info(results.text)
return
rows = results.text and json.loads(results.text) or None
log.warning("RESPONSE {}".format(rows))
return rows
return {}
if resp.status_code != 200:
log.debug(resp)
return {}
result = resp.text and json.loads(resp.text) or None
log.warning("RESPONSE %s", result)
return result
##############################
......
......@@ -250,7 +250,7 @@ def hitung_bulan(jatuh_tempo, tanggal=None, max_denda=24):
jatuh_tempo): # + timedelta(days=1):
return 0
kini = datetime.now()
kini = tanggal
x = (kini.year - jatuh_tempo.year) * 12
y = kini.month - jatuh_tempo.month
bln_tunggakan = x + y + 1
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!