From 03792eadebf5aefa2e95e5f43c14a728f6d73a2b Mon Sep 17 00:00:00 2001 From: crunk Date: Mon, 17 Jun 2024 23:06:51 +0200 Subject: [PATCH] further working on describer and fixed found errors --- verse/distribusikan/add_files_to_describer.py | 68 +++++++++++++++++++ verse/distribusikan/distribusi_selector.py | 10 +-- verse/distribusikan/distribusi_workflow.py | 6 +- verse/distribusikan/upload_page.py | 4 +- verse/models/distribusi_file_model.py | 2 +- 5 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 verse/distribusikan/add_files_to_describer.py diff --git a/verse/distribusikan/add_files_to_describer.py b/verse/distribusikan/add_files_to_describer.py new file mode 100644 index 0000000..3ce1eb5 --- /dev/null +++ b/verse/distribusikan/add_files_to_describer.py @@ -0,0 +1,68 @@ +import os + +import magic +from distribusi.mappings import FILE_TYPES +from models.distribusi_model import Distribusis +from models.distribusi_file_model import DistribusiFiles +from app import create_app, get_app, db +from sqlalchemy.exc import ( + DatabaseError, + DataError, + IntegrityError, + InterfaceError, + InvalidRequestError, +) + +MIME_TYPE = magic.Magic(mime=True) + + +def _distribusi_file_with_type(distribusi, full_path): + mime = MIME_TYPE.from_file(full_path) + type_, subtype = mime.split("/") + if type_ in FILE_TYPES: + _add_distribusi_file_to_db(distribusi, full_path, type_) + + +def _get_distribusi_from_path(path): + distribusi = Distribusis.query.filter_by(distribusiname=path).first() + return distribusi + + +def _add_distribusi_file_to_db(distribusi, full_path, type): + app = get_app() + app.logger.info(f"adding file to database: {full_path} type: {type}") + try: + new_distribusi_file = DistribusiFiles( + path=full_path, + type=type, + distribusi=distribusi.id, + ) + db.session.add(new_distribusi_file) + db.session.commit() + return + except InvalidRequestError: + db.session.rollback() + app.logger.error("Something went wrong!") + except IntegrityError: + db.session.rollback() + app.logger.error("File %s already exists!", full_path) + except DataError: + db.session.rollback() + app.logger.error("%s Invalid Entry", full_path) + except InterfaceError: + db.session.rollback() + app.logger.error("Error connecting to the database") + except DatabaseError: + db.session.rollback() + app.logger.error("Error connecting to the database") + + +def add_distribusi_files_to_db(path): + distribusi = _get_distribusi_from_path(path) + path = os.path.join("stash", path) + for root, dirs, files in os.walk(path, topdown=True): + files = list(filter(lambda f: not f.startswith("."), files)) + files = list(filter(lambda f: not f.endswith(".html"), files)) + for file in files: + full_path = os.path.join(root, file) + _distribusi_file_with_type(distribusi, full_path) diff --git a/verse/distribusikan/distribusi_selector.py b/verse/distribusikan/distribusi_selector.py index 39f46ca..f42bd2e 100644 --- a/verse/distribusikan/distribusi_selector.py +++ b/verse/distribusikan/distribusi_selector.py @@ -127,17 +127,17 @@ def select_current_distribusi(distribusiname): flash("An error occured !", "danger") -def DistribusiSelected(): +def distribusi_selected(): user = User.query.filter_by(email=current_user.email).first() if user.currentdistribusi is None: return False return True -def SelectorVisible(): +def selector_visible(): has_distribusi = UserHelper.has_distribusi() - distribusi_selected = DistribusiSelected() - if distribusi_selected: + is_distribusi_selected = distribusi_selected() + if is_distribusi_selected: return False if not has_distribusi: return False @@ -155,7 +155,7 @@ def render_distribusi_template(selectorform, uploadform, current_distribusi): # because the user has chosen to update his distribusi, we assume # no selected css. css_selected = False - selectorvisible = SelectorVisible() + selectorvisible = selector_visible() limit_reached = UserHelper.distribusi_limit_reached() template = render_template( "distribusi.html", diff --git a/verse/distribusikan/distribusi_workflow.py b/verse/distribusikan/distribusi_workflow.py index 0f91425..43b6666 100644 --- a/verse/distribusikan/distribusi_workflow.py +++ b/verse/distribusikan/distribusi_workflow.py @@ -15,7 +15,8 @@ from sqlalchemy.exc import ( ) from app import db -from distribusikan.distribusi_selector import SelectorVisible +from distribusikan.add_files_to_describer import add_distribusi_files_to_db +from distribusikan.distribusi_selector import selector_visible from distribusikan.distribusis_info import DistribusisInfo from distribusikan.forms.distribusiform import DistribusiForm from distribusikan.forms.publicthemeform import PublicThemeForm @@ -44,6 +45,7 @@ def distribusi_workflow(): cssfile = get_css_file(distribusi) unzip_distribusi_files(distribusi, userfolder) clean_up_distribusi_files(userfolder) + add_distribusi_files_to_db(distribusi.distribusiname) run_distribusi(userfolder, cssfile) set_distribusi_to_visible(distribusi, user) delete_css_file(cssfile) @@ -121,7 +123,7 @@ def render_distribusi_template(distribusiform, current_distribusi): publicthemeform.publicthemes.choices = DistribusisInfo.public_themes() selectorform = SelectorForm() selectorform.distribusis.choices = DistribusisInfo.user_distribusinames() - selectorvisible = SelectorVisible() + selectorvisible = selector_visible() files_uploaded = UserHelper.is_zip_uploaded(current_distribusi) distribusi_live = UserHelper.is_distribusi_live(current_distribusi) diff --git a/verse/distribusikan/upload_page.py b/verse/distribusikan/upload_page.py index fc3b7d1..06070f3 100644 --- a/verse/distribusikan/upload_page.py +++ b/verse/distribusikan/upload_page.py @@ -1,7 +1,7 @@ from flask import render_template from app import APP -from distribusikan.distribusi_selector import SelectorVisible +from distribusikan.distribusi_selector import selector_visible from distribusikan.distribusis_info import DistribusisInfo from distribusikan.upload import upload_new_distribusi, upload_updates_files from distribusikan.forms.distribusiform import DistribusiForm @@ -22,7 +22,7 @@ def upload_page(): publicthemeform.publicthemes.choices = DistribusisInfo.public_themes() selectorform = SelectorForm() selectorform.distribusis.choices = DistribusisInfo.user_distribusinames() - selectorvisible = SelectorVisible() + selectorvisible = selector_visible() current_distribusi = UserHelper.current_distribusi() if current_distribusi == "new" or UserHelper.has_distribusi() is False: diff --git a/verse/models/distribusi_file_model.py b/verse/models/distribusi_file_model.py index 23003b1..edddc71 100644 --- a/verse/models/distribusi_file_model.py +++ b/verse/models/distribusi_file_model.py @@ -9,7 +9,7 @@ class DistribusiFiles(db.Model): id = db.Column(db.Integer, primary_key=True) type = db.Column(db.String(100), nullable=True, unique=False) distribusi = db.Column(db.Integer, db.ForeignKey("distribusis.id")) - path = db.Column(db.String(4096), nullable=True, unique=False) + path = db.Column(db.String(4096), nullable=True, unique=True) alttext = db.Column(db.String(255), nullable=True, unique=False) tags = db.Column(db.String(500), nullable=True, unique=False) description = db.Column(db.String(4096), nullable=True, unique=False)