Changing hosted_file_link field names.

This commit is contained in:
Scott Idem
2021-06-18 09:58:31 -04:00
parent 6d4463e57e
commit bb2f14b67c
4 changed files with 46 additions and 36 deletions

View File

@@ -930,6 +930,16 @@ def lookup_id_random_pop(obj_data:dict):
log.warn('for_id_random was passed without for_type')
obj_data.pop('for_id_random')
if 'link_to_type' in obj_data and 'link_to_id_random' in obj_data:
obj_data['link_to_id'] = redis_lookup_id_random(record_id_random=obj_data.get('link_to_id_random', None), table_name=obj_data.get('link_to_type', None))
obj_data.pop('link_to_id_random')
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(obj_data)
elif 'link_to_id_random' in obj_data:
# In case link_to_id_random was passed without link_to_type
log.warn('link_to_id_random was passed without link_to_type')
obj_data.pop('link_to_id_random')
if 'object_type' in obj_data and 'object_id_random' in obj_data:
obj_data['object_id'] = redis_lookup_id_random(record_id_random=obj_data.get('object_id_random', None), table_name=obj_data.get('object_type', None))
obj_data.pop('object_id_random')

View File

@@ -119,9 +119,9 @@ async def save_file(
file: UploadFile,
account_id: int,
account_id_random: str,
for_object_type: str,
for_object_id: int,
for_object_id_random: str,
link_to_type: str,
link_to_id: int,
link_to_id_random: str,
check_allowed_extension: bool = False,
):
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
@@ -136,9 +136,9 @@ async def save_file(
file_info: dict = {}
file_info['saved'] = None
file_info['for_object_type'] = for_object_type
file_info['for_object_id'] = for_object_id
file_info['for_object_id_random'] = for_object_id_random
file_info['link_to_type'] = link_to_type
file_info['link_to_id'] = link_to_id
file_info['link_to_id_random'] = link_to_id_random
file_info['filename'] = file.filename
file_info['extension'] = guess_file_extension(filename=file.filename)
@@ -213,8 +213,8 @@ async def save_file(
def create_hosted_file_link(
account_id: int|str,
hosted_file_id: int|str,
link_to_obj_type: str,
link_to_obj_id: int|str,
link_to_type: str,
link_to_id: int|str,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -223,14 +223,14 @@ def create_hosted_file_link(
else: return False
if hosted_file_id := redis_lookup_id_random(record_id_random=hosted_file_id, table_name='hosted_file'): pass
else: return False
if link_to_obj_id := redis_lookup_id_random(record_id_random=link_to_obj_id, table_name=link_to_obj_type): pass
if link_to_id := redis_lookup_id_random(record_id_random=link_to_id, table_name=link_to_type): pass
else: return False
hosted_file_link_data: dict = {}
hosted_file_link_data['account_id'] = account_id
hosted_file_link_data['hosted_file_id'] = hosted_file_id
hosted_file_link_data['link_to_type'] = link_to_obj_type # Should this be renamed to "link_to_obj_type" for clarity?
hosted_file_link_data['link_to_id'] = link_to_obj_id # Should this be renamed to "link_to_obj_id" for clarity?
hosted_file_link_data['link_to_type'] = link_to_type # Should this be renamed to "link_to_type" for clarity?
hosted_file_link_data['link_to_id'] = link_to_id # Should this be renamed to "link_to_id" for clarity?
# NOTE: Currently sql_insert does not handel all successful inserts correctly. If there is not an autonum ID then it will return 0 as the ID.
if hosted_file_link_data_in_result := sql_insert(data=hosted_file_link_data, table_name='hosted_file_link', id_random_length=0): pass # This should be improved

View File

@@ -69,17 +69,17 @@ class Hosted_File_Link_Base(BaseModel):
return redis_lookup_id_random(record_id_random=values['account_id_random'], table_name='account')
return None
@validator('object_id', always=True)
def object_id_lookup(cls, v, values, **kwargs):
@validator('link_to_id', always=True)
def link_to_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values['object_id_random'] and values['object_type']:
return redis_lookup_id_random(record_id_random=values['object_id_random'], table_name=values['object_type'])
if values['link_to_id_random'] and values['link_to_type']:
return redis_lookup_id_random(record_id_random=values['link_to_id_random'], table_name=values['link_to_type'])
return None
class Config:
underscore_attrs_are_private = True
fields = base_fields
Hosted_File_Base.update_forward_refs()
Hosted_File_Link_Base.update_forward_refs()

View File

@@ -29,8 +29,8 @@ async def upload_files(
file_list: List[UploadFile] = File(...),
account_id: str = Form(..., min_length=1, max_length=22),
# filename: Optional[str] = Form(...),
for_object_type: str = Form(...),
for_object_id: str = Form(..., min_length=1, max_length=22),
link_to_type: str = Form(...),
link_to_id: str = Form(..., min_length=1, max_length=22),
check_allowed_extension: bool = False,
# create_hosted_file_link: bool = True,
x_account_id: str = Header(..., ),
@@ -46,9 +46,9 @@ async def upload_files(
else:
return mk_resp(data=None, status_code=400)
for_object_type = for_object_type
for_object_id_random = for_object_id # This is for the object random str ID
if for_object_id := redis_lookup_id_random(record_id_random=for_object_id, table_name=for_object_type): pass
link_to_type = link_to_type
link_to_id_random = link_to_id # This is for the object random str ID
if link_to_id := redis_lookup_id_random(record_id_random=link_to_id, table_name=link_to_type): pass
else:
return mk_resp(data=None, status_code=400)
@@ -58,9 +58,9 @@ async def upload_files(
file = file_obj,
account_id = account_id,
account_id_random = account_id_random,
for_object_type = for_object_type,
for_object_id = for_object_id,
for_object_id_random = for_object_id_random,
link_to_type = link_to_type,
link_to_id = link_to_id,
link_to_id_random = link_to_id_random,
check_allowed_extension = check_allowed_extension,
)
if file_info['saved']:
@@ -134,8 +134,8 @@ async def upload_files(
if create_hosted_file_link(
account_id=account_id,
hosted_file_id=hosted_file_id,
for_object_type=for_object_type,
for_object_id=for_object_id,
link_to_type=link_to_type,
link_to_id=link_to_id,
): pass # This if statement should be improved
else:
# This if statement should be improved
@@ -157,8 +157,8 @@ async def upload_files_fake(
file_info_li: list,
account_id: str,
# filename: Optional[str] = Form(...),
for_object_type: str,
for_object_id: str,
link_to_type: str,
link_to_id: str,
check_allowed_extension: bool = False,
# create_hosted_file_link: bool = True,
x_account_id: str = Header(..., ),
@@ -176,9 +176,9 @@ async def upload_files_fake(
else:
return mk_resp(data=None, status_code=400)
for_object_type = for_object_type
for_object_id_random = for_object_id # This is for the object random str ID
if for_object_id := redis_lookup_id_random(record_id_random=for_object_id, table_name=for_object_type): pass
link_to_type = link_to_type
link_to_id_random = link_to_id # This is for the object random str ID
if link_to_id := redis_lookup_id_random(record_id_random=link_to_id, table_name=link_to_type): pass
else:
return mk_resp(data=None, status_code=400)
@@ -262,8 +262,8 @@ async def upload_files_fake(
if create_hosted_file_link(
account_id=account_id,
hosted_file_id=hosted_file_id,
for_object_type=for_object_type,
for_object_id=for_object_id,
link_to_type=link_to_type,
link_to_id=link_to_id,
): pass # This if statement should be improved
else:
# This if statement should be improved
@@ -294,9 +294,9 @@ async def test_upload_files(
file = file_obj,
account_id = account_id,
account_id_random = account_id_random,
for_object_type = for_object_type,
for_object_id = for_object_id,
for_object_id_random = for_object_id_random,
link_to_type = link_to_type,
link_to_id = link_to_id,
link_to_id_random = link_to_id_random,
check_allowed_extension = check_allowed_extension,
)
log.debug(file_info)