Lots and lots of logic.
This commit is contained in:
parent
4c8a8d4ac7
commit
2845d340da
2 changed files with 215 additions and 46 deletions
|
|
@ -60,11 +60,8 @@ def generate_crud_blueprint(model, service, *, base_prefix: str | None = None, r
|
|||
DELETE /api/<model>/delete?id=123[&hard=1]
|
||||
"""
|
||||
model_name = model.__name__.lower()
|
||||
# bikeshed if you want pluralization; this is the least-annoying default
|
||||
collection = (base_prefix or model_name).lower()
|
||||
plural = collection if collection.endswith('s') else f"{collection}s"
|
||||
|
||||
bp = Blueprint(plural, __name__, url_prefix=f"/api/{plural}")
|
||||
bp = Blueprint(model_name, __name__, url_prefix=f"/api/{model_name}")
|
||||
|
||||
@bp.errorhandler(Exception)
|
||||
def _handle_any(e: Exception):
|
||||
|
|
@ -105,7 +102,7 @@ def generate_crud_blueprint(model, service, *, base_prefix: str | None = None, r
|
|||
obj = service.create(payload)
|
||||
resp = jsonify(obj.as_dict())
|
||||
resp.status_code = 201
|
||||
resp.headers["Location"] = url_for(f"{plural}.rest_get", obj_id=obj.id, _external=False)
|
||||
resp.headers["Location"] = url_for(f"{bp.name}.rest_get", obj_id=obj.id, _external=False)
|
||||
return resp
|
||||
except Exception as e:
|
||||
return _json_error(e)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue