aboutsummaryrefslogtreecommitdiffstats
path: root/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'views.py')
-rw-r--r--views.py94
1 files changed, 21 insertions, 73 deletions
diff --git a/views.py b/views.py
index f7711b8..dd37902 100644
--- a/views.py
+++ b/views.py
@@ -1,81 +1,25 @@
+"""
+views.py - defines the logic that generates views that a user sees when
+browsing to certain pages
+"""
import os
from flask import request, send_from_directory, abort
from flask import render_template, render_template_string
from siteconfig import siteconfig
-from server import app
+from server import app, cache
+from view_functions import default_context, index_dir, is_hidden_path
# bit of a hack.
# Brackets don't play nicely with Jinja so instead of using .format,
# we just replace the special character $
-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 or a special file.
-
- 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.
-
- *Special files include '.links' and '.description' the format of these
- files is unquoted CSV and text/html. Both are displayed on the index.html
- for the given `path`
- """
- dirs = []
- files = []
- links = []
- description = False
- 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:
- description = True
- 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, directories won't actually be "hidden" but
- they are still not indexed by this program
- """
- return path.split('/')[-1].startswith('.')
+CONTENT_BLOCK = (
+ "{% extends 'base.html' %}{% block content %}${% endblock %}"
+)
@app.route("/")
@app.route("/site")
+@cache.cached()
def home():
"""
home - renders the template `home.html` as the main index file
@@ -84,12 +28,15 @@ def home():
edit, though you can optionally change the title here if you wish
"""
context = default_context()
- context.update({'title': app.config['HOME_TITLE'], 'parent_dir': '/site/'})
+ context.update(
+ {'title': app.config['HOME_TITLE'], 'parent_dir': '/site/'}
+ )
return render_template("site/home.html", **context)
# from: https://pythonise.com/series/learning-flask/sending-files-with-flask
@app.route("/site/<path:path>")
+@cache.cached()
def render_file(path):
"""
render_file - renders an HTML document for the given `path`.
@@ -124,7 +71,8 @@ def render_file(path):
'templates/site/',
path,
mimetype=siteconfig.MIMETYPES.get(
- f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE
+ f".{ path.split('.')[-1] }",
+ siteconfig.DEFAULT_MIMETYPE,
),
)
elif os.path.isdir(abs_path):
@@ -146,11 +94,11 @@ def render_file(path):
@app.route("/raw/<path:path>")
+@cache.cached()
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, when site is replace with
- path.
+ send_file_from_site - instead of rendering a file within a template
+ as with `render_file`, send the raw file to the user
"""
return send_from_directory(
'templates/site/',
@@ -162,10 +110,10 @@ def send_file_from_site(path):
@app.route("/static/<path:path>")
+@cache.cached()
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
+ send_file_from_static - send files from the static directory
"""
return send_from_directory(
'static/',