Better staticdir system for the templates

This commit is contained in:
Dita Aji Pratama 2024-07-31 10:25:33 +07:00
parent b09fe65c32
commit ba4ec31a04
4 changed files with 29 additions and 21 deletions

View File

@ -1,24 +1,18 @@
from bottle import Bottle, get, static_file from core import template
page = { page = {
'public' :'pages/public', 'public' :'pages/public',
'email' :'pages/email' 'email' :'pages/email'
} }
app = Bottle() static = [
{
# Default staticdir "route" :"/css/<filepath:re:.*\.(css|sass|css.map)>",
"root" :"./static/css"
@app.get("/css/<filepath:re:.*\.(css|sass|css.map)>") },
def static(filepath): {
return static_file(filepath, root="./static/css") "route" :"/js/<filepath:re:.*\.(js)>",
"root" :"./static/js"
@app.get("/js/<filepath:re:.*\.(js)>") }
def static(filepath): ]
return static_file(filepath, root="./static/js") template.add(static, "templates")
# Template staticdir: plain
@app.get("/templates/plain/css/<filepath:re:.*\.(css|sass|css.map)>")
def static(filepath):
return static_file(filepath, root="./templates/plain/static/css")

7
app/core/staticdir.py Normal file
View File

@ -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) )

8
app/core/template.py Normal file
View File

@ -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)

View File

@ -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. # 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/. # 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 bottle import Bottle, run
from beaker.middleware import SessionMiddleware from beaker.middleware import SessionMiddleware
import handler import handler
from core import staticdir
from config import server from config import server
from config import directory
app = Bottle() app = Bottle()
app.merge(handler.app) app.merge(handler.app)
app.merge(directory.app) app.merge(staticdir.app)
app = SessionMiddleware(app, server.session_opts) app = SessionMiddleware(app, server.session_opts)