aboutsummaryrefslogtreecommitdiffstats
path: root/server.py
diff options
context:
space:
mode:
authormjfernez <mjfernez@gmail.com>2021-06-15 23:28:40 -0400
committermjfernez <mjfernez@gmail.com>2021-06-16 00:12:00 -0400
commit945594509e6a19ab0229e9949691e2f7652ea500 (patch)
tree6604218008b8de51fa70d998cadc960ef627efe2 /server.py
parent3ba991bedda7ac3d2e090bfa9bd9d254ee285ae6 (diff)
downloadezcms-945594509e6a19ab0229e9949691e2f7652ea500.tar.gz
Changed / to /site, added views.py, README amended
Previously this program served files in the 'templates/site' dir on '/', but this caused problems when accessing raw files using '/raw/' which is useful for embedding or quickly downloading content. It's also an inconvenience to the user, since they have to add a word rather than double-clicking an replacing it. The main server file and the base and index templates have been changed to reflect this. The codebase has been reorganized to separate views from the main flask app. Blueprints were avoided since only one blueprint would be needed so thus unnecessary. README and docstrings were also expanded for clarity and edited for errors. Some comments
Diffstat (limited to 'server.py')
-rw-r--r--server.py184
1 files changed, 12 insertions, 172 deletions
diff --git a/server.py b/server.py
index e2e93f0..e77485c 100644
--- a/server.py
+++ b/server.py
@@ -1,183 +1,15 @@
import os
from flask import Flask
-from flask import request, send_from_directory, abort
-from flask import render_template, render_template_string
from siteconfig import siteconfig
app = Flask(__name__)
-# bit of a hack.
-# Brackets don't play nicely with Jinja so instead of using .format,
-# we just replace the special charcater $
-CONTENT_BLOCK = (
- "{% extends 'base.html' %}"
- "{% block content %}"
- "$"
- "{% endblock %}"
-)
-
-
-def default_context():
- """
- default_context - returns the minimum info needed to render a template--the
- domain name (for the home directory), and the top site directories which
- make up the navbar
- """
- return {
- 'domain': app.config['DOMAIN'],
- 'navbar': sorted(app.config['MAIN_SITE_DIRS'])
- }
-
-
-def index_dir(path):
- """
- index_dir - Given a directory at `path`, list it's contents,
- and sort each item as a file or a directory.
-
- return - a tuple with the values:
- > a list of directories in `path`,
- > a list of files in `path`,
- > (if present), a list of external links to add to the index.html of the
- directory
- > (if present), a short description (string) of what the directory contains
-
- Lists are sorted alphabetically
- """
- dirs = []
- files = []
- links = []
- description = ""
- contents = os.listdir(path)
- for obj in contents:
- if os.path.isfile(path + '/' + obj):
- if obj == siteconfig.LINKS_FILE:
- with open(path + '/' + obj) as f:
- links = f.readlines()
- elif obj == siteconfig.DESC_FILE:
- with open(path + '/' + obj) as f:
- description = f.read()
- elif obj.startswith('.'):
- continue
- else:
- files.append(obj)
- elif os.path.isdir(path + '/' + obj):
- if obj.startswith('.'):
- continue
- else:
- dirs.append(obj)
-
- return sorted(dirs), sorted(files), sorted(links), description
-
-
-def is_hidden_path(path):
- """
- Tests if last object specified in `path` is hidden.
- Inspired by Unix. On Windows, diretories won't actually be "hidden" but
- they are still not indexed by this program
- """
- return path.split('/')[-1].startswith('.')
-
-
-@app.route("/")
-def home():
- """
- home - renders the template `home.html` as the main index file
-
- If you'd like to customize your home page, that is the file you want to
- edit, though you can optionally change the title here if you wish
- """
- context = default_context()
- context.update(
- {
- 'title': app.config['HOME_TITLE'],
- 'parent_dir': '/'
- }
- )
- return render_template("site/home.html", **context)
-
-
-# from: https://pythonise.com/series/learning-flask/sending-files-with-flask
-@app.route("/<path:path>")
-def render_file(path):
- """
- render_file - renders an HTML document for the given `path`.
-
- If `path` is an HTML file it is rendered within the base template,
- otherwise, the raw file is returned. If `path` points to a directory, this
- function instead creates an index for the directory containing it's files,
- links, and other info.
- """
- if is_hidden_path(path):
- abort(404)
- abs_path = "./templates/site/" + path
- context = default_context()
- context.update(
- {
- 'title': path.split('.')[0].upper(),
- 'parent_dir': '/' + '/'.join(path.split('/')[:-1])
- }
- )
- if os.path.isfile(abs_path):
- if abs_path.endswith('.html'):
- with open(abs_path, 'rb') as f:
- content = f.read().decode("UTF-8")
- return render_template_string(CONTENT_BLOCK.replace('$', content), **context)
- elif abs_path.endswith('.html!'):
- return render_template("site/" + path, **context)
- else:
- # not an html file, so don't render it
- return send_from_directory('templates/site/', path,
- mimetype=siteconfig.MIMETYPES.get(
- f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE
- )
- )
- elif os.path.isdir(abs_path):
- dirs, files, links, description = index_dir(abs_path)
- context.update(
- {
- 'cur_dir': path.split('/')[-1] + '/',
- 'dirs': dirs,
- 'files': files,
- 'links': links,
- 'description': description
- }
- )
- return render_template("index.html", **context)
- else:
- context.update({'errors': "404 File not found"})
- return render_template("base.html", **context)
-
-
-@app.route("/raw/<path:path>")
-def send_file_from_site(path):
- """
- send_file - instead of rendering a file within a template as with
- `render_file`, send the raw file to the user
- """
- return send_from_directory('template/site/', path,
- mimetype=siteconfig.MIMETYPES.get(
- f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE
- )
- )
-
-
-@app.route("/static/<path:path>")
-def send_file_from_static(path):
- """
- send_file - instead of rendering a file within a template as with
- `render_file`, send the raw file to the user
- """
- return send_from_directory('static/', path,
- mimetype=siteconfig.MIMETYPES.get(
- f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE
- )
- )
-
+from views import *
def setup():
"""
setup - sets up the app according to the settings specified (or not
- speified) in `siteconfig`
+ specified) in `siteconfig`
"""
if siteconfig.SECRET_KEY:
app.config['SECRET_KEY'] = siteconfig.SECRET_KEY
@@ -188,12 +20,20 @@ def setup():
if siteconfig.MAIN_SITE_DIRS:
app.config.update({'MAIN_SITE_DIRS': siteconfig.MAIN_SITE_DIRS})
else:
- app.config.update({'MAIN_SITE_DIRS': index_dir('./templates/site')[0]})
+ s = './templates/site/'
+ top_dirs = [
+ x for x in os.listdir(s) if os.path.isdir(s + x)
+ ]
+ app.config.update(
+ {
+ 'MAIN_SITE_DIRS': sorted(top_dirs)
+ }
+ )
app.config.update({'DOMAIN': siteconfig.DOMAIN})
app.config.update({'HOME_TITLE': siteconfig.HOME_TITLE})
-# Need to come first to be compatible with wsgi
+# Setup needs to come first to be compatible with wsgi
setup()
if __name__ == '__main__':
app.run()