From 94f71d6f8fc7301dfd470ccf2fee27e078321a78 Mon Sep 17 00:00:00 2001 From: mjfernez Date: Sun, 13 Mar 2022 15:21:11 -0400 Subject: Pull changes from mjfer.net This commit adds the changes from the mjfer.net which enable using git as the file last update tracker --- config.py | 2 +- rss_generator.py | 18 ++++++++++++------ siteconfig.py | 17 ++++++++++------- templates/base.html | 18 ++++++++++++++---- view_functions.py | 17 ++++++++++++++--- views.py | 4 ++-- 6 files changed, 53 insertions(+), 23 deletions(-) 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/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 4a653d4..0f4f66c 100644 --- a/siteconfig.py +++ b/siteconfig.py @@ -5,10 +5,11 @@ siteconfig.py - user editable configuration file # this file from config import config + class siteconfig(config): # REQUIRED SETTINGS # - DOMAIN = "example.net" # Your site here! + DOMAIN = "mjfer.net" # Your site here! HOME_TITLE = "WELCOME" LINKS_FILE = ".links" # ".lnx" if you like DESC_FILE = ".description" # ".desc" @@ -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/ @@ -36,16 +39,16 @@ class siteconfig(config): # RSS Settings rss_channel_config = { - 'TITLE': "RSS Feed for example.net", - 'LINK': "http://127.0.0.1:5000/", - 'DESCRIPTION': "My example feed", + 'TITLE': "RSS Feed for mjfer.net", + 'LINK': "https://mjfer.net/", + 'DESCRIPTION': "", 'LANGUAGE': "en-us", 'PUBDATE': "", 'LASTBUILDDATE': "", 'DOCS': "https://git.mjfer.net/ezcms.git/", 'GENERATOR': "EZCMS", - 'AUTHOR': "editor@example.net", - 'WEBMASTER': "webmaster@example.net", + 'AUTHOR': "mjf@mjfer.net", + 'WEBMASTER': "mjf@mjfer.net", # Max amount of paragraphs to print in each description 'DESCRIPTION_LENGTH': 3, # File extensions to include in RSS updates diff --git a/templates/base.html b/templates/base.html index c280184..5424505 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,9 +3,19 @@ {{ domain }}/{{ title }} + + + {% block css%} + {% endblock %}
@@ -17,16 +27,16 @@ {{ dir }}/ | {% endfor %} - - - + +   +

{{ title }}

{% block content %} {% endblock %}

{{ errors }}

- {% if parent_dir %} + {% if parent_dir %}

Go up to parent folder ({{ parent_dir }})

{% endif %}
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