[pricelist] persistent save for visible columns in pricecalculation

This commit is contained in:
Tim Gröger 2021-03-18 22:34:18 +01:00
parent 2b35eec0fc
commit 8dc2defe02
1 changed files with 36 additions and 1 deletions

View File

@ -5,10 +5,13 @@ from http.client import NO_CONTENT
from flaschengeist.plugins import Plugin from flaschengeist.plugins import Plugin
from flaschengeist.utils.decorators import login_required from flaschengeist.utils.decorators import login_required
from werkzeug.exceptions import BadRequest from werkzeug.exceptions import BadRequest, Forbidden
from . import models from . import models
from . import pricelist_controller, permissions from . import pricelist_controller, permissions
from ...controller import userController
from ...models.session import Session
from ...utils.HTTP import no_content
pricelist_bp = Blueprint("pricelist", __name__, url_prefix="/pricelist") pricelist_bp = Blueprint("pricelist", __name__, url_prefix="/pricelist")
@ -220,3 +223,35 @@ def update_extra_ingredient(identifier):
def delete_extra_ingredient(identifier): def delete_extra_ingredient(identifier):
pricelist_controller.delete_extra_ingredient(identifier) pricelist_controller.delete_extra_ingredient(identifier)
return "", NO_CONTENT return "", NO_CONTENT
@pricelist_bp.route("/users/<userid>/pricecalc_columns", methods=["GET", "PUT"])
@login_required()
def get_columns(userid, current_session: Session):
"""Get pricecalc_columns of an user
Route: ``/users/<userid>/pricelist/pricecac_columns`` | Method: ``GET`` or ``PUT``
POST-data: On ``PUT`` json encoded array of floats
Args:
userid: Userid identifying the user
current_session: Session sent with Authorization Header
Returns:
GET: JSON object containing the shortcuts as float array or HTTP error
PUT: HTTP-created or HTTP error
"""
if userid != current_session._user.userid:
raise Forbidden
user = userController.get_user(userid)
if request.method == "GET":
return jsonify(user.get_attribute("pricecalc_columns", []))
else:
data = request.get_json()
if not isinstance(data, list) or not all(isinstance(n, str) for n in data):
raise BadRequest
data.sort(reverse=True)
user.set_attribute("pricecalc_columns", data)
userController.persist()
return no_content()