further working on describer and fixed found errors
This commit is contained in:
parent
62428e84ae
commit
03792eadeb
68
verse/distribusikan/add_files_to_describer.py
Normal file
68
verse/distribusikan/add_files_to_describer.py
Normal file
@ -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)
|
@ -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",
|
||||
|
@ -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)
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user