Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-a-g-h authored Oct 13, 2024
1 parent 0127453 commit e59fd65
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 201 deletions.
8 changes: 4 additions & 4 deletions control_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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"
Expand All @@ -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)"
Expand All @@ -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)" "<br>"
Expand Down
219 changes: 81 additions & 138 deletions dbi_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,34 +268,34 @@ 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:

v=outverb
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

Expand All @@ -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
}
}
)
Expand All @@ -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__":

Expand Down Expand Up @@ -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")
)

Expand All @@ -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)
wb.save(test_file.name)
Loading

0 comments on commit e59fd65

Please sign in to comment.