fully working slide crud
This commit is contained in:
parent
bf5a67a05f
commit
74af0cba16
@ -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);
|
||||
});
|
||||
|
||||
48
obscreen.py
48
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
|
||||
|
||||
|
||||
|
||||
# <config>
|
||||
@ -31,9 +37,12 @@ if config['reverse_proxy_mode']:
|
||||
|
||||
# <server>
|
||||
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
|
||||
|
||||
# </server>
|
||||
|
||||
# <xenv>
|
||||
@ -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):
|
||||
# </web>
|
||||
|
||||
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']
|
||||
)
|
||||
|
||||
|
||||
@ -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]
|
||||
|
||||
@ -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
|
||||
)
|
||||
@ -1,6 +1,6 @@
|
||||
from enum import Enum
|
||||
|
||||
class ItemType(Enum):
|
||||
class SlideType(Enum):
|
||||
PICTURE = 'picture'
|
||||
VIDEO = 'video'
|
||||
URL = 'url'
|
||||
|
||||
6
src/utils.py
Normal file
6
src/utils.py
Normal file
@ -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")
|
||||
@ -185,7 +185,7 @@
|
||||
<div class="form-group">
|
||||
<label for="slide-add-name">Name</label>
|
||||
<div class="widget">
|
||||
<input name="name" type="text" id="slide-add-name" />
|
||||
<input name="name" type="text" id="slide-add-name" required="required" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
@ -210,7 +210,7 @@
|
||||
<div class="form-group">
|
||||
<label for="slide-add-duration">Duration</label>
|
||||
<div class="widget">
|
||||
<input type="number" name="duration" id="slide-add-duration" />
|
||||
<input type="number" name="duration" id="slide-add-duration" required="required" />
|
||||
<span>seconds</span>
|
||||
</div>
|
||||
</div>
|
||||
@ -236,7 +236,7 @@
|
||||
<div class="form-group">
|
||||
<label for="slide-edit-name">Name</label>
|
||||
<div class="widget">
|
||||
<input type="text" name="name" id="slide-edit-name" />
|
||||
<input type="text" name="name" id="slide-edit-name" required="required" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
@ -249,10 +249,18 @@
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="slide-edit-location">Location</label>
|
||||
<div class="widget">
|
||||
<input type="text" name="location" id="slide-edit-location" disabled="disabled" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="slide-edit-duration">Duration</label>
|
||||
<div class="widget">
|
||||
<input type="number" name="duration" id="slide-edit-duration" />
|
||||
<input type="number" name="duration" id="slide-edit-duration" required="required" />
|
||||
<span>seconds</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user