|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
|
|
def to_dict(model_instance) -> dict[str, Any]:
|
|
"""
|
|
Returns a dictionary representation of a model instance.
|
|
|
|
Includes all attributes defined on the model class.
|
|
|
|
Args:
|
|
model_instance: The model instance to convert to a dictionary.
|
|
|
|
Returns
|
|
-------
|
|
A dictionary containing all attributes and their values.
|
|
"""
|
|
return {c.name: getattr(model_instance, c.name) for c in model_instance.__table__.columns}
|
|
|
|
|
|
def get_current_datetime():
|
|
|
|
now = datetime.now()
|
|
|
|
|
|
return now.strftime("%Y-%m-%d %H:%M:%S.%f")
|
|
|
|
|
|
def update_table(db, model, data_dict):
|
|
for field, value in data_dict.items():
|
|
|
|
if field == "userId":
|
|
continue
|
|
|
|
if isinstance(value, bool):
|
|
value = str(int(value))
|
|
setattr(model, field, value)
|
|
|
|
db.add(model)
|
|
db.commit()
|
|
return
|
|
|
|
|
|
def create_swagger_response(success_data, error_response_list=None):
|
|
"""
|
|
Create a Swagger response example for both success and error responses.
|
|
|
|
- **success_data**: Example data to include in the success response.
|
|
- **error_response_list**: List of error responses to include in the Swagger documentation.
|
|
|
|
Returns a dictionary representing the success and error responses.
|
|
"""
|
|
|
|
responses = {200: {"content": {"application/json": {"example": success_data}}}}
|
|
|
|
|
|
if error_response_list:
|
|
for error_response in error_response_list:
|
|
responses.update(error_response)
|
|
|
|
return responses
|
|
|