Applied automatic field check also for PUT on times to avoid accidental overwrite
This commit is contained in:
parent
ae2ce859ad
commit
60baf02299
@ -72,7 +72,7 @@ def PutTimes():
|
|||||||
|
|
||||||
# validate the request
|
# validate the request
|
||||||
InputValidationTimes.evaluate_put_data(user_data, loadedModel, content)
|
InputValidationTimes.evaluate_put_data(user_data, loadedModel, content)
|
||||||
return impl.times.PutTimes(loadedModel)
|
return impl.times.PutTimes(loadedModel, content)
|
||||||
|
|
||||||
except ValidationError as ex:
|
except ValidationError as ex:
|
||||||
return create_validation_error_response(ex=ex, status_code=400)
|
return create_validation_error_response(ex=ex, status_code=400)
|
||||||
|
|||||||
@ -115,7 +115,7 @@ def PostTimes(schemaModel):
|
|||||||
pooledConnection.close()
|
pooledConnection.close()
|
||||||
|
|
||||||
|
|
||||||
def PutTimes(schemaModel):
|
def PutTimes(schemaModel, original_payload=None):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
:param schemaModel: The deserialized model of the record to be inserted
|
:param schemaModel: The deserialized model of the record to be inserted
|
||||||
@ -127,24 +127,33 @@ def PutTimes(schemaModel):
|
|||||||
pooledConnection = local_db.getPoolConnection()
|
pooledConnection = local_db.getPoolConnection()
|
||||||
commands = pydapper.using(pooledConnection)
|
commands = pydapper.using(pooledConnection)
|
||||||
|
|
||||||
query = "UPDATE times SET "
|
sentinel = object()
|
||||||
isNotFirst = False
|
existing_times = commands.query_single_or_default("SELECT * FROM times WHERE id = ?id?", sentinel, param={"id": schemaModel["id"]})
|
||||||
|
if existing_times is sentinel:
|
||||||
|
return json.dumps("no such record"), 404, {'Content-Type': 'application/json; charset=utf-8'}
|
||||||
|
|
||||||
|
provided_keys = set(original_payload.keys()) if isinstance(original_payload, dict) else None
|
||||||
|
|
||||||
|
if "shipcall_id" not in schemaModel or (provided_keys is not None and "shipcall_id" not in provided_keys):
|
||||||
|
schemaModel["shipcall_id"] = existing_times["shipcall_id"]
|
||||||
|
|
||||||
|
schemaModel = {k:v.value if isinstance(v, (Enum, Flag)) else v for k,v in schemaModel.items()}
|
||||||
|
|
||||||
|
update_clauses = []
|
||||||
for key in schemaModel.keys():
|
for key in schemaModel.keys():
|
||||||
|
if provided_keys is not None and key not in provided_keys:
|
||||||
|
continue
|
||||||
if key == "id":
|
if key == "id":
|
||||||
continue
|
continue
|
||||||
if key == "created":
|
if key == "created":
|
||||||
continue
|
continue
|
||||||
if key == "modified":
|
if key == "modified":
|
||||||
continue
|
continue
|
||||||
if isNotFirst:
|
update_clauses.append(f"{key} = ?{key}?")
|
||||||
query += ", "
|
|
||||||
isNotFirst = True
|
|
||||||
query += key + " = ?" + key + "? "
|
|
||||||
|
|
||||||
query += "WHERE id = ?id?"
|
if update_clauses:
|
||||||
|
query = "UPDATE times SET " + ", ".join(update_clauses) + " WHERE id = ?id?"
|
||||||
schemaModel = {k:v.value if isinstance(v, (Enum, Flag)) else v for k,v in schemaModel.items()}
|
commands.execute(query, param=schemaModel)
|
||||||
affected_rows = commands.execute(query, param=schemaModel)
|
|
||||||
|
|
||||||
# apply 'Traffic Light' evaluation to obtain 'GREEN', 'YELLOW' or 'RED' evaluation state. The function internally updates the mysql database 'shipcall'
|
# apply 'Traffic Light' evaluation to obtain 'GREEN', 'YELLOW' or 'RED' evaluation state. The function internally updates the mysql database 'shipcall'
|
||||||
evaluate_shipcall_state(mysql_connector_instance=pooledConnection, shipcall_id=schemaModel["shipcall_id"]) # every times data object refers to the 'shipcall_id'
|
evaluate_shipcall_state(mysql_connector_instance=pooledConnection, shipcall_id=schemaModel["shipcall_id"]) # every times data object refers to the 'shipcall_id'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user