fix: improve parameter handling and logging in various functions

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