restyling
This commit is contained in:
@@ -496,9 +496,10 @@ def assign_playlist(display_id: int):
|
||||
@bp.post("/displays/<int:display_id>")
|
||||
@login_required
|
||||
def update_display(display_id: int):
|
||||
"""Update display metadata (description + assigned playlist).
|
||||
"""Update display metadata.
|
||||
|
||||
Company users should be able to set a short description per display and assign a playlist.
|
||||
Supports both form POST (full update) and JSON/AJAX (partial update).
|
||||
Company users can set a short description per display and assign a playlist.
|
||||
"""
|
||||
|
||||
company_user_required()
|
||||
@@ -507,26 +508,77 @@ def update_display(display_id: int):
|
||||
if not display or display.company_id != current_user.company_id:
|
||||
abort(404)
|
||||
|
||||
wants_json = (
|
||||
(request.headers.get("X-Requested-With") == "XMLHttpRequest")
|
||||
or ("application/json" in (request.headers.get("Accept") or ""))
|
||||
or request.is_json
|
||||
)
|
||||
|
||||
def _json_error(message: str, status: int = 400):
|
||||
return jsonify({"ok": False, "error": message}), status
|
||||
|
||||
# Inputs from either form or JSON
|
||||
payload = request.get_json(silent=True) if request.is_json else None
|
||||
|
||||
# Description (short, optional)
|
||||
desc = (request.form.get("description") or "").strip() or None
|
||||
if desc is not None:
|
||||
desc = desc[:200]
|
||||
display.description = desc
|
||||
if request.is_json:
|
||||
if payload is None:
|
||||
return _json_error("Invalid JSON")
|
||||
if "description" in payload:
|
||||
desc = (payload.get("description") or "").strip() or None
|
||||
if desc is not None:
|
||||
desc = desc[:200]
|
||||
display.description = desc
|
||||
else:
|
||||
# form POST implies full update
|
||||
desc = (request.form.get("description") or "").strip() or None
|
||||
if desc is not None:
|
||||
desc = desc[:200]
|
||||
display.description = desc
|
||||
|
||||
# Playlist assignment
|
||||
playlist_id = (request.form.get("playlist_id") or "").strip()
|
||||
if not playlist_id:
|
||||
display.assigned_playlist_id = None
|
||||
if request.is_json:
|
||||
if "playlist_id" in payload:
|
||||
playlist_id_val = payload.get("playlist_id")
|
||||
if playlist_id_val in (None, ""):
|
||||
display.assigned_playlist_id = None
|
||||
else:
|
||||
try:
|
||||
playlist_id_int = int(playlist_id_val)
|
||||
except (TypeError, ValueError):
|
||||
return _json_error("Invalid playlist_id")
|
||||
playlist = db.session.get(Playlist, playlist_id_int)
|
||||
if not playlist or playlist.company_id != current_user.company_id:
|
||||
return _json_error("Invalid playlist")
|
||||
display.assigned_playlist_id = playlist.id
|
||||
else:
|
||||
try:
|
||||
playlist_id_int = int(playlist_id)
|
||||
except ValueError:
|
||||
abort(400)
|
||||
playlist = db.session.get(Playlist, playlist_id_int)
|
||||
if not playlist or playlist.company_id != current_user.company_id:
|
||||
abort(400)
|
||||
display.assigned_playlist_id = playlist.id
|
||||
playlist_id = (request.form.get("playlist_id") or "").strip()
|
||||
if not playlist_id:
|
||||
display.assigned_playlist_id = None
|
||||
else:
|
||||
try:
|
||||
playlist_id_int = int(playlist_id)
|
||||
except ValueError:
|
||||
abort(400)
|
||||
playlist = db.session.get(Playlist, playlist_id_int)
|
||||
if not playlist or playlist.company_id != current_user.company_id:
|
||||
abort(400)
|
||||
display.assigned_playlist_id = playlist.id
|
||||
|
||||
db.session.commit()
|
||||
|
||||
if wants_json:
|
||||
return jsonify(
|
||||
{
|
||||
"ok": True,
|
||||
"display": {
|
||||
"id": display.id,
|
||||
"name": display.name,
|
||||
"description": display.description,
|
||||
"assigned_playlist_id": display.assigned_playlist_id,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
flash("Display updated", "success")
|
||||
return redirect(url_for("company.dashboard"))
|
||||
|
||||
Reference in New Issue
Block a user