From bd4e5bec076e1a05ef2687f4c2fbc1ee0dc4038c Mon Sep 17 00:00:00 2001 From: Michael Moese Date: Fri, 1 Jul 2022 11:32:22 +0200 Subject: [PATCH] Move helper functions into own module Helper functions are now inside utils.py, and are just imported from there. Signed-off-by: Michael Moese --- app.py | 37 +++---------------------------------- utils.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 34 deletions(-) create mode 100644 utils.py diff --git a/app.py b/app.py index 518c0d0..733d5ef 100755 --- a/app.py +++ b/app.py @@ -6,42 +6,11 @@ from flask_flatpages import FlatPages from flask_flatpages.utils import pygmented_markdown from flask_fontawesome import FontAwesome from datetime import datetime -from fotogrid import fotogrid -from feed import generate_feed import locale - -# directly support jinja within markdown blogposts -# https://flask-flatpages.readthedocs.io/en/v0.7.1/ -def markdown_with_jinja_renderer(text): - prerendered_body = render_template_string(text) - return pygmented_markdown(prerendered_body) - - -# function to reformat date from blog posts for better -# visualization on the pages -def format_date_string(date_string): - date = datetime.strptime(date_string, "%d.%m.%Y") - return date.strftime("%d. %B %Y") - - -def sort_date_from_string(date_string): - try: - return datetime.strptime(date_string.split('-')[0].strip(), "%d.%m.%Y") - except Exception: - return datetime.now() - - -def is_future_date(date_string): - try: - start_date = datetime.strptime(date_string.split('-')[0].strip(), "%d.%m.%Y") - if start_date < datetime.now(): - return False - except Exception: - pass - - return True - +from fotogrid import fotogrid +from feed import generate_feed +from utils import markdown_with_jinja_renderer, format_date_string, sort_date_from_string, is_future_date # create app app = Flask(__name__) diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..f50206a --- /dev/null +++ b/utils.py @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: GPL-3.0 +# vim: set sw=4 ts=4 ex +from flask import render_template_string +from flask_flatpages.utils import pygmented_markdown +from datetime import datetime + +# directly support jinja within markdown blogposts +# https://flask-flatpages.readthedocs.io/en/v0.7.1/ +def markdown_with_jinja_renderer(text): + prerendered_body = render_template_string(text) + return pygmented_markdown(prerendered_body) + +# function to reformat date from blog posts for better +# visualization on the pages +def format_date_string(date_string): + date = datetime.strptime(date_string, "%d.%m.%Y") + return date.strftime("%d. %B %Y") + + +def sort_date_from_string(date_string): + try: + return datetime.strptime(date_string.split('-')[0].strip(), "%d.%m.%Y") + except Exception: + return datetime.now() + + +def is_future_date(date_string): + try: + start_date = datetime.strptime(date_string.split('-')[0].strip(), "%d.%m.%Y") + if start_date < datetime.now(): + return False + except Exception: + pass + + return True + +