From ba4ec31a04a7a3b7d3dd6590e12ae7a62b088e27 Mon Sep 17 00:00:00 2001 From: ditaajipratama Date: Wed, 31 Jul 2024 10:25:33 +0700 Subject: [PATCH] Better staticdir system for the templates --- app/config/directory.py | 30 ++++++++++++------------------ app/core/staticdir.py | 7 +++++++ app/core/template.py | 8 ++++++++ app/costa.py | 5 ++--- 4 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 app/core/staticdir.py create mode 100644 app/core/template.py diff --git a/app/config/directory.py b/app/config/directory.py index 3228804..1d82c5b 100644 --- a/app/config/directory.py +++ b/app/config/directory.py @@ -1,24 +1,18 @@ -from bottle import Bottle, get, static_file +from core import template page = { 'public' :'pages/public', 'email' :'pages/email' } -app = Bottle() - -# Default staticdir - -@app.get("/css/") -def static(filepath): - return static_file(filepath, root="./static/css") - -@app.get("/js/") -def static(filepath): - return static_file(filepath, root="./static/js") - -# Template staticdir: plain - -@app.get("/templates/plain/css/") -def static(filepath): - return static_file(filepath, root="./templates/plain/static/css") +static = [ + { + "route" :"/css/", + "root" :"./static/css" + }, + { + "route" :"/js/", + "root" :"./static/js" + } +] +template.add(static, "templates") diff --git a/app/core/staticdir.py b/app/core/staticdir.py new file mode 100644 index 0000000..02df303 --- /dev/null +++ b/app/core/staticdir.py @@ -0,0 +1,7 @@ +from bottle import Bottle, static_file +from config import directory + +app = Bottle() + +for item in directory.static: + app.route(item['route'], "GET", lambda filepath, root=item['root']: static_file(filepath, root=root) ) diff --git a/app/core/template.py b/app/core/template.py new file mode 100644 index 0000000..9a0663a --- /dev/null +++ b/app/core/template.py @@ -0,0 +1,8 @@ +import os + +def add(dirconfig, template_directory): + template_list = [d for d in os.listdir(template_directory) if os.path.isdir(os.path.join(template_directory, d))] + for template_name in template_list: + template_module = __import__(f"{template_directory}.{template_name}.main", fromlist=["static"]) + for static in getattr(template_module, "static", []): + dirconfig.append(static) diff --git a/app/costa.py b/app/costa.py index cc6b19a..77dbdf3 100644 --- a/app/costa.py +++ b/app/costa.py @@ -5,19 +5,18 @@ # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. -import sys from bottle import Bottle, run from beaker.middleware import SessionMiddleware import handler +from core import staticdir from config import server -from config import directory app = Bottle() app.merge(handler.app) -app.merge(directory.app) +app.merge(staticdir.app) app = SessionMiddleware(app, server.session_opts)