From 74af0cba16fcfdbb85fed47b171b13a1058308b7 Mon Sep 17 00:00:00 2001 From: jr-k Date: Mon, 26 Feb 2024 22:17:46 +0100 Subject: [PATCH] fully working slide crud --- data/www/js/manage.js | 4 +++- obscreen.py | 48 +++++++++++++++++++++++++++++++++++------ src/SlideManager.py | 10 +++------ src/model/Slide.py | 16 +++++++++----- src/model/SlideType.py | 2 +- src/utils.py | 6 ++++++ views/manage.jinja.html | 16 ++++++++++---- 7 files changed, 77 insertions(+), 25 deletions(-) create mode 100644 src/utils.py diff --git a/data/www/js/manage.js b/data/www/js/manage.js index b846c17..cf6da78 100644 --- a/data/www/js/manage.js +++ b/data/www/js/manage.js @@ -90,14 +90,16 @@ jQuery(document).ready(function ($) { $(document).on('click', '.slide-add', function () { showModal('modal-slide-add'); + $('.modal-slide-add input:eq(0)').focus().select(); }); $(document).on('click', '.slide-edit', function () { var slide = JSON.parse($(this).parents('tr:eq(0)').attr('data-entity')); - console.log(slide) showModal('modal-slide-edit'); + $('.modal-slide-edit input:visible:eq(0)').focus().select(); $('#slide-edit-name').val(slide.name); $('#slide-edit-type').val(slide.type); + $('#slide-edit-location').val(slide.location); $('#slide-edit-duration').val(slide.duration); $('#slide-edit-id').val(slide.id); }); diff --git a/obscreen.py b/obscreen.py index 3a964e6..f983112 100755 --- a/obscreen.py +++ b/obscreen.py @@ -6,10 +6,16 @@ import shutil import subprocess import sys + from enum import Enum from flask import Flask, render_template, redirect, request, url_for, send_from_directory, jsonify +from werkzeug.utils import secure_filename from config import config from src.SlideManager import SlideManager +from src.model.Slide import Slide +from src.model.SlideType import SlideType +from src.utils import str_to_enum + # @@ -31,9 +37,12 @@ if config['reverse_proxy_mode']: # app = Flask(__name__, template_folder='views', static_folder='data') +app.config['UPLOAD_FOLDER'] = 'data/uploads' +app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB if config['debug']: app.config['TEMPLATES_AUTO_RELOAD'] = True + # # @@ -95,13 +104,34 @@ def manage(): disabled_slides=slide_manager.get_disabled_slides() ) -@app.route('/manage/slide/add', methods=['POST']) +@app.route('/manage/slide/add', methods=['GET', 'POST']) def manage_slide_add(): - name = request.form['name'] - print(name) - print(request.form) - response = {'message': f'Bonjour {name}, votre formulaire a été reçu !'} - return jsonify(response) + slide = Slide( + name=request.form['name'], + type=str_to_enum(request.form['type'], SlideType), + duration=request.form['duration'], + ) + + if slide.has_file(): + if 'object' not in request.files: + return redirect(request.url) + + object = request.files['object'] + + if object.filename == '': + return redirect(request.url) + + if object: + object_name = secure_filename(object.filename) + object_path = os.path.join(app.config['UPLOAD_FOLDER'], object_name) + object.save(object_path) + slide.location = object_path + else: + slide.location = request.form['object'] + + slide_manager.add_form(slide) + + return redirect(url_for('manage')) @app.route('/manage/slide/edit', methods=['POST']) def manage_slide_edit(): @@ -132,5 +162,9 @@ def not_found(e): # if __name__ == '__main__': - app.run(host=config['bind'] if 'bind' in config else '0.0.0.0', port=config['port']) + app.run( + host=config['bind'] if 'bind' in config else '0.0.0.0', + port=config['port'], + debug=config['debug'] + ) diff --git a/src/SlideManager.py b/src/SlideManager.py index c0287bb..562b0a2 100644 --- a/src/SlideManager.py +++ b/src/SlideManager.py @@ -2,6 +2,7 @@ import json from typing import Dict, Optional, List, Tuple from src.model.Slide import Slide +from src.utils import str_to_enum from pysondb import db class SlideManager(): @@ -39,16 +40,11 @@ class SlideManager(): def update_form(self, id: str, name: str, duration: int) -> None: self._db.updateById(id, {"name": name, "duration": duration}) - def reindent(self) -> None: - with open(self.DB_FILE, 'r') as file: - data = json.load(file) - - with open(self.DB_FILE, 'w', encoding='utf-8') as file: - json.dump(data, file, ensure_ascii=False, indent=4) + def add_form(self, slide: Slide) -> None: + self._db.add(slide.to_dict()) def delete(self, id: int) -> None: self._db.deleteById(id) - self.reindent() def to_dict(self, slides: List[Slide]) -> dict: return [slide.to_dict() for slide in slides] diff --git a/src/model/Slide.py b/src/model/Slide.py index 4334390..7b298ff 100644 --- a/src/model/Slide.py +++ b/src/model/Slide.py @@ -1,17 +1,18 @@ import uuid import json -from typing import Optional -from src.model import SlideType +from typing import Optional, Union +from src.model.SlideType import SlideType +from src.utils import str_to_enum class Slide: - def __init__(self, location: str, duration: int, type: SlideType, enabled: bool, name: str, position: int = 999, id: Optional[int] = None): + def __init__(self, location: str = '', duration: int = 3, type: Union[SlideType, str] = SlideType.URL, enabled: bool = False, name: str = 'Untitled', position: int = 999, id: Optional[int] = None): self._id = uuid.uuid4().int if id is None else id self._location = location self._duration = duration - self._type = type + self._type = str_to_enum(type, SlideType) if isinstance(type, str) else type self._enabled = enabled self._name = name self._position = position @@ -88,7 +89,12 @@ class Slide: "id": self.id, "enabled": self.enabled, "position": self.position, - "type": self.type, + "type": self.type.value, "duration": self.duration, "location": self.location, } + + def has_file(self) -> bool: + return (self.type == SlideType.VIDEO + or self.type == SlideType.PICTURE + ) \ No newline at end of file diff --git a/src/model/SlideType.py b/src/model/SlideType.py index e733e3e..7318944 100644 --- a/src/model/SlideType.py +++ b/src/model/SlideType.py @@ -1,6 +1,6 @@ from enum import Enum -class ItemType(Enum): +class SlideType(Enum): PICTURE = 'picture' VIDEO = 'video' URL = 'url' diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..d67cc0c --- /dev/null +++ b/src/utils.py @@ -0,0 +1,6 @@ + +def str_to_enum(strval: str, enum_class): + for enum_item in enum_class: + if enum_item.value == strval: + return enum_item + raise ValueError(f"{strval} is not a valid {enum_class.__name__} item") diff --git a/views/manage.jinja.html b/views/manage.jinja.html index dd82e8b..e5aede6 100644 --- a/views/manage.jinja.html +++ b/views/manage.jinja.html @@ -185,7 +185,7 @@
- +
@@ -210,7 +210,7 @@
- + seconds
@@ -236,7 +236,7 @@
- +
@@ -249,10 +249,18 @@
+ +
+ +
+ +
+
+
- + seconds