From c0be486968fe3d375d1f408eae7f954e1f5f82fc Mon Sep 17 00:00:00 2001 From: mjfernez Date: Thu, 10 Mar 2022 18:34:22 -0500 Subject: Fixes RSS issues. Changes time tracking to git This commit primarily changes the code base to base the file last modified time on the output of "git log". This adds dateutil and git itself as a dependency, but those are pretty common... This also fixes some minor issues including: - missing / between mjfer.net and the URI - README up to date with latest changes - linting --- README.md | 26 +++++++++++++++++++------- config.py | 2 +- requirements.txt | 20 ++++++++++---------- rss_generator.py | 18 ++++++++++++------ siteconfig.py | 7 +++++-- templates/base.html | 2 +- templates/site | 2 +- view_functions.py | 17 ++++++++++++++--- views.py | 4 ++-- 9 files changed, 65 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index e376da9..56bc3f2 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ## Requirements Python 3.7+ - +git (for RSS generation) ## Huh/What/Why? @@ -178,7 +178,8 @@ replace the HTML with your own license (or none), by editing ### RSS and Caching This app comes packaged with an auto-RSS generator that makes a feed -based on the files in the site directory. You can omit files by adding +based on the files in the site directory, presuming you use git to keep +track of your files. You can omit files by adding them to the omit file from the perspective of the site dir. For example: @@ -189,11 +190,22 @@ or for files run under 'site' `home.html` -You can also make use of the Flask-Caching module to optimize your -site's perfomance. There are many options available and to be honest, I don't know much about them, but a simple cache of 5 minutes is provided by default. You'll want to read the - [Flask-Caching -docs](https://flask-caching.readthedocs.io/en/latest/index.html) - before experimenting. +You can make your site into a local git repo with: + +```bash +$ cd ./templates/site +$ rm -rf * +$ git init + +$ git add . +$ git commit -m "new update" +``` + +Alternatively, you can make a new git repo elsewhere and add it to the +main project as a submodule. + +See [here](https://git-scm.com/book/en/v2/Git-Tools-Submodules) +for more on submodules ### Other Tips diff --git a/config.py b/config.py index 6f796fe..5e62328 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,6 @@ import os + class config: # BACKGROUND SETTINGS # # These don't need to be changed @@ -30,4 +31,3 @@ class config: DEFAULT_SITE_DIRS.append(x) GENERATED_SECRET_KEY = os.urandom(32) - diff --git a/requirements.txt b/requirements.txt index b3c4209..ca6eda0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,10 @@ -click==8.0.1 -Flask==2.0.1 -itsdangerous==2.0.1 -Jinja2==3.0.1 -MarkupSafe==2.0.1 -pip==20.3.4 -setuptools==44.1.1 -Werkzeug==2.0.1 -Flask-Compress==1.10.1 -Flask-Caching==1.10.1 +click>=8.0.1 +Flask>=2.0.1 +itsdangerous>=2.0.1 +Jinja2>=3.0.1 +MarkupSafe>=2.0.1 +pip>=20.3.4 +setuptools>=44.1.1 +Werkzeug>=2.0.1 +Flask-Compress>=1.10.1 +python-dateutil>=2.8.1 diff --git a/rss_generator.py b/rss_generator.py index bde4645..05a8b39 100644 --- a/rss_generator.py +++ b/rss_generator.py @@ -1,5 +1,6 @@ import os -from time import strftime, strptime +from datetime import datetime +from dateutil import parser from siteconfig import siteconfig from view_functions import is_hidden_path, file_last_modified @@ -12,6 +13,7 @@ class RSS_Item: Item data is generated from a given file path """ + PARAGRAPHS = siteconfig.rss_channel_config['DESCRIPTION_LENGTH'] class NotAFile(Exception): @@ -19,6 +21,7 @@ class RSS_Item: Throws an exception if an RSS_Item is made out of a directory or invalid file """ + def __init__(self, path: str): self.path = path self.message = f"{path} not a file" @@ -32,8 +35,8 @@ class RSS_Item: self.TITLE = path.rsplit('.', 1)[0].split('/')[-1] self.FILE_TYPE = path.rsplit('.', 1)[1] self.DESCRIPTION = self.parse_file() - self.LAST_UPDATE = self.last_updated() self.URI = self.get_uri() + self.LAST_UPDATE = self.last_updated() self.LINK = siteconfig.rss_channel_config['LINK'] + self.URI def __str__(self): @@ -42,7 +45,10 @@ class RSS_Item: ) def short_timestamp(self): - return strftime("%Y-%m-%d %H:%M", strptime(self.LAST_UPDATE)) + if self.LAST_UPDATE is None: + return "" + dt_update = parser.parse(self.LAST_UPDATE) + return datetime.strftime(dt_update, "%Y-%m-%d %H:%M") def parse_file(self): """ @@ -76,11 +82,11 @@ class RSS_Item: return ''.join(description) def last_updated(self): - return file_last_modified(self.FULL_PATH) + return file_last_modified(self.URI) def get_uri(self): - # return everything after "./templates/" - return '/'.join(self.FULL_PATH.split('/')[2:]) + # return everything after "./templates/site" + return '/'.join(self.FULL_PATH.split('/')[3:]) def get_rss_channel(): diff --git a/siteconfig.py b/siteconfig.py index da21d11..0f4f66c 100644 --- a/siteconfig.py +++ b/siteconfig.py @@ -5,6 +5,7 @@ siteconfig.py - user editable configuration file # this file from config import config + class siteconfig(config): # REQUIRED SETTINGS # @@ -28,7 +29,9 @@ class siteconfig(config): # Set a custom secret key. If not set, it will be generated # Most of the time, you don't need to set this! - SECRET_KEY = config.GENERATED_SECRET_KEY # replace with random number. + SECRET_KEY = ( + config.GENERATED_SECRET_KEY + ) # replace with random number. # Options for Flask Compress # see here https://pypi.org/project/Flask-Compress/ @@ -37,7 +40,7 @@ class siteconfig(config): # RSS Settings rss_channel_config = { 'TITLE': "RSS Feed for mjfer.net", - 'LINK': "mjfer.net", + 'LINK': "https://mjfer.net/", 'DESCRIPTION': "", 'LANGUAGE': "en-us", 'PUBDATE': "", diff --git a/templates/base.html b/templates/base.html index b5742c1..5424505 100644 --- a/templates/base.html +++ b/templates/base.html @@ -41,7 +41,7 @@ {% endif %} {% if last_update %} -

Last update: {{ last_update }} UTC

+

Last update: {{ last_update }}

{% endif %}
{% include 'site/license.html' %}
diff --git a/templates/site b/templates/site index 9b0caf3..7c78a2b 160000 --- a/templates/site +++ b/templates/site @@ -1 +1 @@ -Subproject commit 9b0caf3a5a42c16cca2c7a84089f23d2d4d98599 +Subproject commit 7c78a2b48a8394dfc1b8f3ce25ce78055fe9797e diff --git a/view_functions.py b/view_functions.py index 3de8687..4163c2e 100644 --- a/view_functions.py +++ b/view_functions.py @@ -3,8 +3,8 @@ view_functions.py - defines functions called by views to display the correct dat about files and paths. """ import os -from time import ctime -from siteconfig import siteconfig +import subprocess +from siteconfig import siteconfig, config def default_context(): @@ -20,7 +20,18 @@ def default_context(): def file_last_modified(path): - return ctime(os.stat(path).st_mtime) + git_time = f"git log -n1 --pretty=%aD {path}".split() + try: + mod_time = subprocess.check_output( + git_time, cwd=config.BASE_DIR + ) + except: + # File is not in the git log, no biggie, just blank the date + return None + + # Git outputs in RFC2822 format + return mod_time.decode('ascii').strip() + def index_dir(path): """ diff --git a/views.py b/views.py index d69ac51..fda6152 100644 --- a/views.py +++ b/views.py @@ -55,7 +55,7 @@ def render_file(path): { 'title': path.split('.')[0].upper(), 'parent_dir': '/' + '/'.join(path.split('/')[:-1]), - 'last_update': file_last_modified(abs_path) + 'last_update': file_last_modified(path), } ) if abs_path.endswith('.html'): @@ -134,6 +134,6 @@ def render_rss_feed(): feed = render_template("feed.xml", **context) response = make_response(feed) response.headers['Content-Type'] = str( - siteconfig.MIMETYPES.get(".xml") + siteconfig.MIMETYPES.get(".xml") ) return response -- cgit v1.2.3