Commit 4e500606 authored by shadMod's avatar shadMod 💬

split news.py in views.py and utils.py

parent 14d6bf2e
# Source code of the Ubuntu-it website # Source code of the Ubuntu-it website
# Copyright (C) 2023 shadMod # Copyright (C) 2023 shadMod <m.allegro@shadmod.com>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published # it under the terms of the GNU Affero General Public License as published
...@@ -14,22 +14,17 @@ ...@@ -14,22 +14,17 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os import os
import flask
import locale import locale
import markdown import markdown
from datetime import datetime from datetime import datetime
from ... import cache
CACHE_FILE = "news-cache.json"
class News: class GetNews:
""" """
:base_path: path :param base_path: path
:return: [ :return: [
{ {
"title": "Lorem Ipsum", "title": "Lorem Ipsum",
...@@ -51,50 +46,8 @@ class News: ...@@ -51,50 +46,8 @@ class News:
- Aliquam tincidunt mauris. - Aliquam tincidunt mauris.
""" """
def __init__(self): def __init__(self, base_path):
self.base_path = None self.base_path = base_path
def prepare_blueprint(self, app):
"""
Prepare a blueprint with the news pages
"""
bp = flask.Blueprint("news", __name__)
# populate base_path
self.base_path = os.path.join(app.instance_path, "news")
@bp.route("/")
@bp.route("/<year>")
@cache.enable
def index(year: int = None):
news = []
if year is None:
data = {}
for subdir in sorted(os.listdir(self.base_path), reverse=True):
data[subdir] = os.listdir(os.path.join(self.base_path, subdir))
for year, file_list in data.items():
for file in file_list:
news.append(self.get_data(file, int(year)))
else:
dir_path = os.path.join(self.base_path, str(year))
if os.path.exists(dir_path):
file_list = os.listdir(dir_path)
for file in file_list:
news.append(self.get_data(file, year))
return flask.render_template(
"news/index.html",
news=news,
)
@bp.route("/<year>/<filename>")
def detail(year: int, filename: str):
return flask.render_template(
"news/detail.html",
news=self.get_data(f"{filename}.md", year)
)
return bp
def get_data(self, filename: str, year: int = None): def get_data(self, filename: str, year: int = None):
""" """
...@@ -143,3 +96,46 @@ class News: ...@@ -143,3 +96,46 @@ class News:
# what's left I put in html # what's left I put in html
data["html"] = "".join(html_tmp) data["html"] = "".join(html_tmp)
return data return data
@property
def list_files(self) -> list:
data = []
for list_files in self.get_all_files.values():
data.extend(list_files)
return data
@property
def get_all_files(self) -> dict:
data = {}
for subdir in sorted(os.listdir(self.base_path), reverse=True):
data[subdir] = os.listdir(os.path.join(self.base_path, subdir))
return data
def get_all_files_counter(self, counter: int) -> dict:
i = 0
data = {}
for year, list_news in self.get_all_files.items():
if year not in data:
data[year] = []
for filename in list_news:
data[year].append(filename)
i += 1
if i == counter:
break
if i == counter:
break
return data
def list_news(self, year=None):
news = []
if year is None:
for year, file_list in self.get_all_files.items():
for file in file_list:
news.append(self.get_data(file, int(year)))
else:
dir_path = os.path.join(self.base_path, str(year))
if os.path.exists(dir_path):
file_list = os.listdir(dir_path)
for file in file_list:
news.append(self.get_data(file, year))
return news
# Source code of the Ubuntu-it website
# Copyright (C) 2023 shadMod <m.allegro@shadmod.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import flask
from ... import cache
from .utils import GetNews
CACHE_FILE = "news-cache.json"
class News:
@staticmethod
def prepare_blueprint(app):
"""
Prepare a blueprint with the news pages
"""
bp = flask.Blueprint("news", __name__)
nw_cl = GetNews(os.path.join(app.instance_path, "news"))
@bp.route("/")
@bp.route("/<year>")
@cache.enable
def index(year: int = None):
return flask.render_template(
"news/index.html",
news=nw_cl.list_news(year),
)
@bp.route("/<year>/<filename>")
def detail(year: int, filename: str):
return flask.render_template(
"news/detail.html",
news=nw_cl.get_data(f"{filename}.md", year)
)
return bp
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment