optimizations
This commit is contained in:
parent
d9a9477ee2
commit
81dab72efb
1
data/www/js/dragdrop.js
vendored
1
data/www/js/dragdrop.js
vendored
@ -12,6 +12,7 @@ jQuery(function ($) {
|
||||
url: $(this).attr('data-route'),
|
||||
dropZone: $('body'),
|
||||
formData: {},
|
||||
singleFileUploads: false,
|
||||
dataType: 'json',
|
||||
add: function (e, data) {
|
||||
const $alert = $('.alert-danger');
|
||||
|
||||
@ -78,17 +78,20 @@ class ContentController(ObController):
|
||||
def slideshow_content_upload_bulk(self):
|
||||
working_folder_path, working_folder = self.get_working_folder()
|
||||
|
||||
file = request.files['object']
|
||||
type = ContentType.guess_content_type_file(file)
|
||||
name = file.filename.rsplit('.', 1)[0]
|
||||
for key in request.files:
|
||||
files = request.files.getlist(key)
|
||||
for file in files:
|
||||
type = ContentType.guess_content_type_file(file)
|
||||
name = file.filename.rsplit('.', 1)[0]
|
||||
|
||||
self._model_store.content().add_form_raw(
|
||||
name=name,
|
||||
type=type,
|
||||
request_files=request.files,
|
||||
upload_dir=self._app.config['UPLOAD_FOLDER'],
|
||||
folder_id=working_folder.id if working_folder else None
|
||||
)
|
||||
if type:
|
||||
self._model_store.content().add_form_raw(
|
||||
name=name,
|
||||
type=type,
|
||||
request_files=file,
|
||||
upload_dir=self._app.config['UPLOAD_FOLDER'],
|
||||
folder_id=working_folder.id if working_folder else None
|
||||
)
|
||||
|
||||
return redirect(url_for('slideshow_content_list', path=working_folder_path))
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import os
|
||||
|
||||
from typing import Dict, Optional, List, Tuple, Union
|
||||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
from src.model.entity.Content import Content
|
||||
from src.model.entity.Playlist import Playlist
|
||||
@ -165,15 +166,19 @@ class ContentManager(ModelManager):
|
||||
)
|
||||
|
||||
if content.has_file():
|
||||
if 'object' not in request_files:
|
||||
return redirect(request.url)
|
||||
object = None
|
||||
|
||||
object = request_files['object']
|
||||
if 'object' in request_files:
|
||||
object = request_files['object']
|
||||
|
||||
if object.filename == '':
|
||||
if isinstance(request_files, FileStorage):
|
||||
object = request_files
|
||||
|
||||
if not object or object.filename == '':
|
||||
return None
|
||||
|
||||
if object:
|
||||
object.seek(0)
|
||||
object_name = randomize_filename(object.filename)
|
||||
object_path = os.path.join(upload_dir, object_name)
|
||||
object.save(object_path)
|
||||
|
||||
@ -5,10 +5,8 @@ import sqlite3
|
||||
import logging
|
||||
|
||||
from sqlite3 import Cursor
|
||||
from src.util.utils import wrap_if, is_wrapped_by
|
||||
from typing import Optional, Dict
|
||||
|
||||
|
||||
class DatabaseManager:
|
||||
|
||||
DB_FILE: str = "data/db/obscreen.db"
|
||||
@ -48,7 +46,8 @@ class DatabaseManager:
|
||||
return self
|
||||
|
||||
def close(self) -> None:
|
||||
self._conn.close()
|
||||
if self._conn:
|
||||
self._conn.close()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
@ -61,48 +60,32 @@ class DatabaseManager:
|
||||
|
||||
def execute_write_query(self, query, params=(), silent_errors=False) -> None:
|
||||
logging.debug(query)
|
||||
cur = None
|
||||
sanitized_params = []
|
||||
|
||||
for param in params:
|
||||
if isinstance(param, bool):
|
||||
sanitized_params.append(int(param))
|
||||
elif isinstance(param, dict) or isinstance(param, list):
|
||||
sanitized_params.append(json.dumps(param))
|
||||
else:
|
||||
sanitized_params.append(param)
|
||||
sanitized_params = self._sanitize_params(params)
|
||||
|
||||
try:
|
||||
with self._conn:
|
||||
cur = self._conn.cursor()
|
||||
cur.execute(query, tuple(sanitized_params))
|
||||
cur.close()
|
||||
except sqlite3.Error as e:
|
||||
if not silent_errors:
|
||||
logging.error("SQL query execution error while writing '{}': {}".format(query, e))
|
||||
self._conn.rollback()
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
finally:
|
||||
if cur is not None:
|
||||
cur.close()
|
||||
|
||||
def execute_read_query(self, query, params=()) -> list:
|
||||
logging.debug(query)
|
||||
cur = None
|
||||
|
||||
result = []
|
||||
try:
|
||||
with self._conn:
|
||||
cur = self._conn.cursor()
|
||||
cur.execute(query, params)
|
||||
rows = cur.fetchall()
|
||||
result = [dict(row) for row in rows]
|
||||
cur.close()
|
||||
except sqlite3.Error as e:
|
||||
logging.error("SQL query execution error while reading '{}': {}".format(query, e))
|
||||
result = []
|
||||
finally:
|
||||
if cur is not None:
|
||||
cur.close()
|
||||
|
||||
return result
|
||||
|
||||
def get_all(self, table_name: str, sort: Optional[str] = None, ascending=True) -> list:
|
||||
@ -131,7 +114,7 @@ class DatabaseManager:
|
||||
count = len(lines)
|
||||
|
||||
if count > 1:
|
||||
raise Error("More than one line returned by query '{}'".format(query))
|
||||
raise ValueError("More than one line returned by query '{}'".format(query))
|
||||
|
||||
return lines[0] if count == 1 else None
|
||||
|
||||
@ -231,3 +214,14 @@ class DatabaseManager:
|
||||
|
||||
for query in queries:
|
||||
self.execute_write_query(query=query, silent_errors=True)
|
||||
|
||||
def _sanitize_params(self, params):
|
||||
sanitized_params = []
|
||||
for param in params:
|
||||
if isinstance(param, bool):
|
||||
sanitized_params.append(int(param))
|
||||
elif isinstance(param, dict) or isinstance(param, list):
|
||||
sanitized_params.append(json.dumps(param))
|
||||
else:
|
||||
sanitized_params.append(param)
|
||||
return sanitized_params
|
||||
|
||||
@ -62,6 +62,9 @@ class UserManager:
|
||||
return [self.hydrate_object(raw_user) for raw_user in raw_users]
|
||||
|
||||
def get(self, id: int) -> Optional[User]:
|
||||
if id in self._user_map:
|
||||
return self._user_map[id]
|
||||
|
||||
object = self._db.get_by_id(self.TABLE_NAME, id)
|
||||
return self.hydrate_object(object, id) if object else None
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user