aboutsummaryrefslogtreecommitdiffstats
path: root/view_functions.py
diff options
context:
space:
mode:
authormjfernez <mjfernez@gmail.com>2021-10-10 22:15:07 -0400
committermjfernez <mjfernez@gmail.com>2021-10-10 22:15:07 -0400
commit02a37ad308fbcc27a04787c84a76de7c2936a6d5 (patch)
treedf25089ab76cc0c3faa94e2990b600ea7568ab63 /view_functions.py
parenta800163aeae0998fa49f011664f32a6c348db886 (diff)
downloadezcms-02a37ad308fbcc27a04787c84a76de7c2936a6d5.tar.gz
Caching support. Separate views.py in 2 files
This commit adds the Flask-Caching module to the software stack and enables the caching of views in a wide variety of ways, but implemented here to be simple to understand to someone new to the concept of caching (aka me). Various documentation and formatting was applied to all files. views.py internal functions (mostly related to filesystem operations of the server). have been moved into view_functions.py
Diffstat (limited to 'view_functions.py')
-rw-r--r--view_functions.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/view_functions.py b/view_functions.py
new file mode 100644
index 0000000..64dcfe0
--- /dev/null
+++ b/view_functions.py
@@ -0,0 +1,71 @@
+"""
+view_functions.py - defines functions called by views to display the correct data
+about files and paths.
+"""
+import os
+from siteconfig import siteconfig
+from server import app
+
+
+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('.')