From e59fd6568cf1c2b1fbe7167fae23b0671a44076d Mon Sep 17 00:00:00 2001 From: carlos-a-g-h Date: Sun, 13 Oct 2024 15:17:31 +0000 Subject: [PATCH] Add files via upload --- control_assets.py | 8 +- dbi_assets.py | 219 +++++++++++++++-------------------------- frontend_assets.py | 238 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 264 insertions(+), 201 deletions(-) diff --git a/control_assets.py b/control_assets.py index 165e705..1ed4696 100644 --- a/control_assets.py +++ b/control_assets.py @@ -69,7 +69,7 @@ _CACHE_ASSETS="cache_assets" -_ERR_TITLE_record_MOD={ +_ERR_TITLE_RECORD_MOD={ _LANG_EN:"History modification error", _LANG_ES:"Error al modificar el historial" } @@ -977,7 +977,7 @@ async def route_api_add_record( ) if not isinstance(asset_id,str): return response_errormsg( - _ERR_TITLE_record_MOD[lang], + _ERR_TITLE_RECORD_MOD[lang], { _LANG_EN:"Asset Id not valid", _LANG_ES:"Id de activo no válido" @@ -990,7 +990,7 @@ async def route_api_add_record( ) if not isinstance(the_sign,str): return response_errormsg( - _ERR_TITLE_record_MOD[lang], + _ERR_TITLE_RECORD_MOD[lang], { _LANG_EN:"Check the 'sign' field", _LANG_ES:"Revisa el campo 'sign' (firma)" @@ -1004,7 +1004,7 @@ async def route_api_add_record( ) if the_mod==0: return response_errormsg( - _ERR_TITLE_record_MOD[lang], + _ERR_TITLE_RECORD_MOD[lang], { _LANG_EN:( "Check the 'mod' field (increase/decrease)" "
" diff --git a/dbi_assets.py b/dbi_assets.py index 2600947..3192d72 100644 --- a/dbi_assets.py +++ b/dbi_assets.py @@ -268,13 +268,13 @@ async def dbi_assets_DropAsset( return result -async def dbi_assets_ModEv_Add( +async def dbi_assets_History_AddRecord( rdbc:AsyncIOMotorClient,name_db:str, asset_id:str, - modev_sign:str, - modev_mod:int, - modev_tag:Optional[str]=None, - modev_comment:Optional[str]=None, + record_sign:str, + record_mod:int, + record_tag:Optional[str]=None, + record_comment:Optional[str]=None, outverb:int=2 )->Mapping: @@ -282,20 +282,20 @@ async def dbi_assets_ModEv_Add( if outverb not in range(0,3): v=2 - modev_date=util_rnow() - modev_uid=secrets.token_hex(8) + record_date=util_rnow() + record_uid=secrets.token_hex(8) - modev_object={ - "date":modev_date, - "mod":modev_mod, - "sign":modev_sign, + record_object={ + "date":record_date, + "mod":record_mod, + "sign":record_sign, } - if isinstance(modev_tag,str): - modev_object.update({"tag":modev_tag}) + if isinstance(record_tag,str): + record_object.update({"tag":record_tag}) - if isinstance(modev_comment,str): - modev_object.update({"comment":modev_comment}) + if isinstance(record_comment,str): + record_object.update({"comment":record_comment}) res:Optional[UpdateResult]=None @@ -305,7 +305,7 @@ async def dbi_assets_ModEv_Add( {"_id":asset_id}, { "$set":{ - f"history.{modev_uid}":modev_object + f"history.{record_uid}":record_object } } ) @@ -321,121 +321,64 @@ async def dbi_assets_ModEv_Add( if v==1: return { - "uid":modev_uid, - "date":modev_date + "uid":record_uid, + "date":record_date } - modev_object.update({"uid":modev_uid}) + record_object.update({"uid":record_uid}) - return modev_object - -# NOTE: Use in the future -# def util_modev_filter( -# modev_uid:str, -# data:Mapping, -# filter_uid:Optional[str]=None, -# filter_date:Optional[str]=None, -# filter_sign:Optional[str]=None, -# filter_tag:Optional[str]=None -# )->list: - -# modev_date:Optional[str]=data.get("date") -# modev_sign:Optional[str]=data.get("sign") -# modev_mod:Optional[int]=data.get("mod") - -# if not isinstance(modev_date,str): -# return [] - -# if not isinstance(modev_sign,str): -# return [] - -# if not isinstance(modev_mod,int): -# return [] - -# if isinstance(filter_uid,str): -# if not filter_uid==modev_uid: -# return [] - -# if isinstance(filter_date,str): -# if not modev_date.startswith(filter_date): -# return [] - -# if isinstance(filter_sign,str): -# if not modev_sign==filter_sign: -# return [] - -# modev_tag:Optional[str]=data.get("tag") -# if isinstance(filter_tag,str): -# if not modev_tag==filter_tag: -# return [] - -# data_ok={ -# "uid":modev_uid, -# "date":modev_date, -# "sign":modev_sign, -# "mod":modev_mod -# } - -# if isinstance(modev_tag,str): -# data_ok.update( -# {"tag":modev_tag} -# ) - -# modev_comment:Optional[str]=data.get("comment") -# if isinstance(modev_comment,str): -# data_ok.update( -# {"comment":modev_comment} -# ) - -# return [data_ok] - -# NOTE: Use in the future -# async def dbi_assets_ModEv_Filter( -# rdbc:AsyncIOMotorClient,name_db:str, -# asset_id:str, -# modev_sign:Optional[str]=None, -# modev_uid:Optional[str]=None, -# modev_tag:Optional[str]=None, -# modev_date:Optional[str]=None, -# )->list: - -# results=None - -# # TODO: Learn how to optimize with MQL - -# try: -# tgtcol:AsyncIOMotorCollection=rdbc[name_db][_COL_ASSETS] -# results=await tgtcol.find_one( -# {"_id":asset_id}, -# {"history":1,"_id":0} -# ) - -# except Exception as exc: -# print(exc) -# return [] - -# if "history" not in results.keys(): -# return [] - -# if not isinstance(results["history"],list): -# return [] - -# results_ok=[] - -# for key in results["history"]: - -# results_ok.extend( -# util_modev_filter( -# key, -# results["history"][key], -# filter_uid=modev_uid, -# filter_sign=modev_sign, -# filter_date=modev_date, -# filter_tag=modev_tag -# ) -# ) - -# return results_ok + return record_object + +async def dbi_assets_History_GetSingleRecord( + rdbc:AsyncIOMotorClient,name_db:str, + asset_id:str,record_uid:str, + )->Mapping: + + aggr_pipeline=[ + { + "$match":{ + "_id":asset_id + } + }, + { + "$project":{ + f"history.{record_uid}":1 + } + }, + { + "$set":{ + "id":"$_id", + "_id":"$$REMOVE" + } + } + ] + + result={} + + try: + col:AsyncIOMotorCollection=rdbc[name_db][_COL_ASSETS] + cursor:AsyncIOMotorCursor=col.aggregate(aggr_pipeline) + async for doc in cursor: + result.update(doc) + break + + except Exception as exc: + return {"err":f"{exc}"} + + if len(result)==0: + return {"err":"Nothing was found"} + + if not isinstance(result.get("history"),Mapping): + return {"err":"No history/records found in the asset"} + + if not isinstance(result["history"].get(record_uid),Mapping): + return {"err":"The specified record was not found in the history"} + + the_record=result["history"][record_uid] + + the_record.update({"uid":record_uid}) + + return the_record if __name__=="__main__": @@ -502,15 +445,15 @@ async def dbi_assets_ModEv_Add( col_idx=col_idx+1 - modev_mod=util_valid_int( + record_mod=util_valid_int( asset["history"][uid].get("mod") ) - modev_comment=util_valid_str( + record_comment=util_valid_str( asset["history"][uid].get("comment") ) - modev_date=util_valid_date( + record_date=util_valid_date( asset["history"][uid].get("date") ) @@ -519,30 +462,30 @@ async def dbi_assets_ModEv_Add( f"{uid}" ) - if modev_date is not None: + if record_date is not None: cell_comment=( f"{cell_comment}\n" f"DATE:\n" - f"{modev_date}" + f"{record_date}" ) - if modev_comment is not None: + if record_comment is not None: cell_comment=( f"{cell_comment}" "\n\n" - f"{modev_comment}" + f"{record_comment}" ) - if modev_mod is None: + if record_mod is None: cell_comment=( f"{cell_comment}\n\n(WARNING)" ) tgt_cell:Cell=ws[f"{util_excel_dectocol(col_start+col_idx)}{row}"] - tgt_cell.value=modev_mod + tgt_cell.value=record_mod tgt_cell.comment=Comment( cell_comment, "?", height=160,width=160 ) - wb.save(test_file.name) \ No newline at end of file + wb.save(test_file.name) diff --git a/frontend_assets.py b/frontend_assets.py index a059039..4cb191b 100644 --- a/frontend_assets.py +++ b/frontend_assets.py @@ -302,7 +302,7 @@ def write_form_delete_asset( _LANG_ES:"¿Está seguro de que quiere eliminar este activo?" }[lang] html_text=( - "" ) -def write_form_add_modev(lang:str,asset_id:str)->str: +def write_form_add_record(lang:str,asset_id:str)->str: tl={ _LANG_EN:"Add or remove", @@ -458,7 +458,7 @@ def write_form_add_modev(lang:str,asset_id:str)->str: ">\n" # WARN: HIDDEN INPUT - """str: "\n" ) -def write_html_modev(lang:str,data:Mapping)->str: - print("MODEV:",data) +def write_button_record_details( + lang:str, + asset_id:str, + record_uid:str + )->str: - modev_uid=util_valid_str(data.get("uid")) + tl={ + _LANG_EN:"Details", + _LANG_ES:"Detalles" + }[lang] - modev_sign=util_valid_str(data.get("sign")) - if not isinstance(modev_sign,str): - return write_div_display_error(lang) + return ( + f"""" + ) + +def write_html_record( + lang:str, + asset_id:str, + data:Mapping, + record_uid:Optional[str]=None, + detailed:bool=False + )->str: + + record_uid_ok:Optional[str]=record_uid + if not isinstance(record_uid,str): - modev_date=util_valid_date( + record_uid_ok=util_valid_str(data.get("uid"),True) + if not isinstance(record_uid_ok,str): + return write_div_display_error(lang) + + record_date=util_valid_date( util_valid_str(data.get("date")) ) + if not isinstance(record_date,str): + return write_div_display_error(lang) - if not isinstance(modev_date,str): + record_mod=util_valid_int(data.get("mod")) + if not isinstance(record_mod,int): return write_div_display_error(lang) - modev_mod=util_valid_int(data.get("mod")) - if not isinstance(modev_mod,int): + record_sign=util_valid_str(data.get("sign"),True) + if not isinstance(record_sign,str): return write_div_display_error(lang) - tl_sign={ - _LANG_EN:"Sign", - _LANG_ES:"Firma" - }[lang] + record_tag=util_valid_str(data.get("tag"),True) - tl_date={ - _LANG_EN:"Date", - _LANG_ES:"Fecha" - }[lang] + html_text="" - tl_mod={ - _LANG_EN:"Adjustment", - _LANG_ES:"Ajuste" - }[lang] + if not detailed: - html_text=f"""
""" + tl={ + _LANG_EN:"Adjustment", + _LANG_ES:"Ajuste", + }[lang] + html_text=( + f"{html_text}\n" + f"
{record_date}
\n" + "
\n" + f"""
""" + f"{tl}: {record_mod}" + "
\n" + ) - if isinstance(modev_uid,str): + tl={ + _LANG_EN:"Signed by", + _LANG_ES:"Firmado por" + }[lang] html_text=( f"{html_text}\n" - f"
UID: {modev_uid}
" + f"""
""" + f"{tl}: {record_sign}" + "
\n" + "
" ) - html_text=( - f"{html_text}\n" - f"
{tl_mod}: {modev_mod}
\n" - f"
{tl_sign}: {modev_sign}
\n" - f"
{tl_date}: {modev_date}
" - ) + if isinstance(record_tag,str): + tl={ + _LANG_EN:"Tag", + _LANG_ES:"Etiqueta" + }[lang] + html_text=( + f"{html_text}\n" + f"
{tl}: {record_tag}
" + ) - modev_tag=util_valid_str( - data.get("tag") - ) - modev_comment=util_valid_str( - data.get("comment") - ) + html_text=( + f"""
""" "\n" + f"{html_text}\n" + f"{write_button_record_details(lang,asset_id,record_uid_ok)}\n" + "
" + ) + + if detailed: - if isinstance(modev_tag,str): tl={ - _LANG_EN:"Tag", - _LANG_ES:"Etiqueta" + _LANG_EN:"Record details", + _LANG_ES:"Detalles del registro" }[lang] html_text=( f"{html_text}\n" - f"
{tl}: {modev_tag}
\n" + """
""" "\n" + f"{tl}\n" + "
\n" + """""" "\n" + "\n" + "\n" + "\n" + f"\n" + "\n" ) - if isinstance(modev_comment,str): tl={ - _LANG_EN:"Comment", - _LANG_ES:"Comentario" + _LANG_EN:"Date", + _LANG_ES:"Fecha" }[lang] html_text=( f"{html_text}\n" - f"
{tl}: {modev_comment}
\n" + "\n" + f"\n" + f"\n" + "\n" ) - return ( + tl={ + _LANG_EN:"From asset", + _LANG_ES:"Del activo" + }[lang] + html_text=( f"{html_text}\n" - "\n" - ) + "\n" + f"\n" + f"\n" + "\n" + ) -def write_html_modev_history( - lang:str, + tl={ + _LANG_EN:"Adjustment", + _LANG_ES:"Ajuste", + }[lang] + html_text=( + f"{html_text}\n" + "\n" + f"\n" + f"\n" + "\n" + ) + + tl={ + _LANG_EN:"Signed by", + _LANG_ES:"Firmado por" + }[lang] + html_text=( + f"{html_text}\n" + "\n" + f"\n" + f"\n" + "\n" + ) + + if record_tag is not None: + + tl={ + _LANG_EN:"Tag", + _LANG_ES:"Etiqueta" + }[lang] + html_text=( + f"{html_text}\n" + "\n" + f"\n" + f"\n" + "\n" + ) + + record_comment=util_valid_str(data.get("comment"),True) + if record_comment is not None: + + html_text=( + f"{html_text}\n" + """""" "\n" + "\n" + "\n" + ) + + html_text=( + f"{html_text}\n" + "\n" + "
UID{record_uid_ok}
{tl}{record_date}
{tl}{asset_id}
{tl}{record_mod}
{tl}{record_sign}
{tl}{record_tag}
\n" + """
""" "\n" + f"{record_comment}" + "
" + "
" + ) + + return html_text + +def write_html_record_history( + lang:str,asset_id:str, history:Optional[Mapping] ): @@ -648,10 +767,10 @@ def write_html_modev_history( history_keys=list(history.keys()) history_keys.reverse() - for modev_id in history_keys: + for record_uid in history_keys: html_text=( f"{html_text}\n" - f"{write_html_modev(lang,history[modev_id])}" + f"{write_html_record(lang,asset_id,history[record_uid],record_uid=record_uid)}" ) return html_text @@ -783,17 +902,18 @@ def write_html_asset( _LANG_EN:"History", _LANG_ES:"Historial" }[lang] - html_modev_history=write_html_modev_history( - lang,data.get("history") + html_record_history=write_html_record_history( + lang,asset_id, + data.get("history") ) html_text=( f"{html_text}\n" f"

{tl}

\n" f"""
""" "\n" - f"{write_form_add_modev(lang,asset_id)}\n" + f"{write_form_add_record(lang,asset_id)}\n" "
\n" f"""
""" "\n" - f"{html_modev_history}\n" + f"{html_record_history}\n" "
" )