2024-10-09 13:07:38 +02:00
|
|
|
from datetime import datetime
|
2024-10-05 11:55:55 +02:00
|
|
|
from django.conf import settings
|
2024-10-09 13:07:38 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2024-10-05 16:22:40 +02:00
|
|
|
import fstools
|
2024-10-09 11:16:06 +02:00
|
|
|
import json
|
2024-10-09 09:57:05 +02:00
|
|
|
import logging
|
2024-10-07 16:47:15 +02:00
|
|
|
from pages import messages, url_page
|
2024-10-05 11:55:55 +02:00
|
|
|
import mycreole
|
|
|
|
import os
|
2024-10-09 11:16:06 +02:00
|
|
|
import time
|
2024-10-09 13:07:38 +02:00
|
|
|
from users.models import get_userprofile
|
|
|
|
import zoneinfo
|
2024-10-05 11:55:55 +02:00
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
|
2024-10-05 11:55:55 +02:00
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
|
2024-10-09 11:16:06 +02:00
|
|
|
class meta_data(dict):
|
|
|
|
KEY_CREATION_TIME = "creation_time"
|
|
|
|
KEY_MODIFIED_TIME = "modified_time"
|
|
|
|
KEY_MODIFIED_USER = "modified_user"
|
|
|
|
|
|
|
|
def __init__(self, meta_filename, page_exists):
|
|
|
|
self._meta_filename = meta_filename
|
|
|
|
|
|
|
|
# Load data from disk
|
|
|
|
try:
|
|
|
|
with open(meta_filename, 'r') as fh:
|
|
|
|
super().__init__(json.load(fh))
|
|
|
|
except (FileNotFoundError, json.decoder.JSONDecodeError) as e:
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
# Add missing information to meta_data
|
|
|
|
missing_keys = False
|
|
|
|
if self.KEY_CREATION_TIME not in self:
|
|
|
|
missing_keys = True
|
|
|
|
self[self.KEY_CREATION_TIME] = int(time.time())
|
|
|
|
if self.KEY_MODIFIED_TIME not in self:
|
|
|
|
self[self.KEY_MODIFIED_TIME] = self[self.KEY_CREATION_TIME]
|
|
|
|
if missing_keys and page_exists:
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def update(self, username):
|
|
|
|
self[self.KEY_MODIFIED_TIME] = int(time.time())
|
|
|
|
self[self.KEY_MODIFIED_USER] = username
|
|
|
|
#
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
with open(self._meta_filename, 'w') as fh:
|
|
|
|
json.dump(self, fh, indent=4)
|
|
|
|
|
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
class base_page(object):
|
2024-10-09 11:16:06 +02:00
|
|
|
PAGE_FILE_NAME = 'page'
|
|
|
|
META_FILE_NAME = 'meta.json'
|
2024-10-09 09:57:05 +02:00
|
|
|
SPLITCHAR = ":"
|
2024-10-05 11:55:55 +02:00
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
def __init__(self, path):
|
|
|
|
if path.startswith(settings.PAGES_ROOT):
|
|
|
|
self._path = path
|
|
|
|
else:
|
|
|
|
self._path = os.path.join(settings.PAGES_ROOT, path.replace("/", 2*self.SPLITCHAR))
|
|
|
|
self._raw_page_src = None
|
2024-10-09 11:16:06 +02:00
|
|
|
#
|
|
|
|
self._meta_data = meta_data(self._meta_filename, self.is_available())
|
2024-10-05 11:55:55 +02:00
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
def _load_page_src(self):
|
|
|
|
if self._raw_page_src is None:
|
|
|
|
try:
|
|
|
|
with open(self.filename, 'r') as fh:
|
|
|
|
self._raw_page_src = fh.read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
self._raw_page_src = ""
|
2024-10-05 11:55:55 +02:00
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
def update_page(self, page_txt):
|
|
|
|
from .search import update_item
|
|
|
|
#
|
|
|
|
folder = os.path.dirname(self.filename)
|
|
|
|
if not os.path.exists(folder):
|
|
|
|
fstools.mkdir(folder)
|
|
|
|
with open(self.filename, 'w') as fh:
|
|
|
|
fh.write(page_txt)
|
|
|
|
update_item(self)
|
2024-10-09 11:16:06 +02:00
|
|
|
#
|
|
|
|
self._update_metadata()
|
2024-10-05 11:55:55 +02:00
|
|
|
|
|
|
|
@property
|
2024-10-09 09:57:05 +02:00
|
|
|
def filename(self):
|
2024-10-09 11:16:06 +02:00
|
|
|
return os.path.join(self._path, self.PAGE_FILE_NAME)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _meta_filename(self):
|
|
|
|
return os.path.join(self._path, self.META_FILE_NAME)
|
2024-10-05 11:55:55 +02:00
|
|
|
|
|
|
|
@property
|
2024-10-09 09:57:05 +02:00
|
|
|
def rel_path(self):
|
|
|
|
return os.path.basename(self._path).replace(2*self.SPLITCHAR, "/")
|
2024-10-05 20:20:28 +02:00
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
def rel_path_is_valid(self):
|
|
|
|
return not self.SPLITCHAR in self.rel_path
|
2024-10-05 20:20:28 +02:00
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
def is_available(self):
|
|
|
|
is_a = os.path.isfile(self.filename)
|
|
|
|
if not is_a:
|
|
|
|
logger.info("page.is_available: Not available - %s", self.filename)
|
|
|
|
return is_a
|
2024-10-05 11:55:55 +02:00
|
|
|
|
|
|
|
@property
|
2024-10-09 09:57:05 +02:00
|
|
|
def title(self):
|
|
|
|
return os.path.basename(self._path).split("::")[-1]
|
2024-10-05 11:55:55 +02:00
|
|
|
|
2024-10-05 16:22:40 +02:00
|
|
|
@property
|
|
|
|
def raw_page_src(self):
|
2024-10-09 09:57:05 +02:00
|
|
|
self._load_page_src()
|
|
|
|
return self._raw_page_src
|
2024-10-05 16:22:40 +02:00
|
|
|
|
2024-10-09 11:16:06 +02:00
|
|
|
def _update_metadata(self):
|
|
|
|
username = None
|
|
|
|
try:
|
|
|
|
if self._request.user.is_authenticated:
|
|
|
|
username = self._request.user.username
|
|
|
|
else:
|
|
|
|
logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
|
|
|
|
except AttributeError:
|
|
|
|
logger.exception("Page edit without having a request object. Check programming!")
|
|
|
|
self._meta_data.update(username)
|
|
|
|
|
2024-10-09 09:57:05 +02:00
|
|
|
|
|
|
|
class creole_page(base_page):
|
|
|
|
FOLDER_ATTACHMENTS = "attachments"
|
|
|
|
|
|
|
|
def __init__(self, request, path) -> None:
|
|
|
|
self._request = request
|
|
|
|
super().__init__(path)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def attachment_path(self):
|
|
|
|
return os.path.join(os.path.basename(self._path), self.FOLDER_ATTACHMENTS)
|
2024-10-05 11:55:55 +02:00
|
|
|
|
2024-10-07 16:47:15 +02:00
|
|
|
def render_to_html(self):
|
2024-10-05 16:22:40 +02:00
|
|
|
if self.is_available():
|
2024-10-07 16:47:15 +02:00
|
|
|
return self.render_text(self._request, self.raw_page_src)
|
2024-10-05 16:22:40 +02:00
|
|
|
else:
|
2024-10-09 09:57:05 +02:00
|
|
|
messages.unavailable_msg_page(self._request, self.rel_path)
|
2024-10-05 16:22:40 +02:00
|
|
|
return ""
|
2024-10-05 20:20:28 +02:00
|
|
|
|
2024-10-09 13:07:38 +02:00
|
|
|
def render_meta(self):
|
|
|
|
def str_date(tm):
|
|
|
|
up = get_userprofile(self._request.user)
|
|
|
|
tz = zoneinfo.ZoneInfo(up.timezone)
|
|
|
|
#
|
|
|
|
return datetime.fromtimestamp(tm, tz).strftime('%Y-%m-%d %H:%M')
|
|
|
|
#
|
|
|
|
ctime = str_date(self._meta_data.get(self._meta_data.KEY_CREATION_TIME))
|
|
|
|
mtime = str_date(self._meta_data.get(self._meta_data.KEY_MODIFIED_TIME))
|
|
|
|
user = self._meta_data.get(self._meta_data.KEY_MODIFIED_USER)
|
|
|
|
#
|
|
|
|
meta = f'|{_("Created")}:|{ctime}|\n'
|
|
|
|
meta += f'|{_("Modified")}:|{mtime}|\n'
|
|
|
|
meta += f'|{_("Editor")}|{user}|\n\n'
|
|
|
|
#
|
|
|
|
return mycreole.render_simple(meta)
|
|
|
|
|
2024-10-05 20:20:28 +02:00
|
|
|
def render_text(self, request, txt):
|
|
|
|
macros = {
|
2024-10-07 17:19:29 +02:00
|
|
|
"subpages": self.macro_subpages,
|
|
|
|
"allpages": self.macro_allpages,
|
2024-10-05 20:20:28 +02:00
|
|
|
}
|
2024-10-07 00:44:22 +02:00
|
|
|
return mycreole.render(request, txt, self.attachment_path, macros=macros)
|
2024-10-05 20:20:28 +02:00
|
|
|
|
2024-10-07 17:19:29 +02:00
|
|
|
def macro_allpages(self, *args, **kwargs):
|
|
|
|
kwargs["allpages"] = True
|
|
|
|
return self.macro_subpages(*args, **kwargs)
|
|
|
|
|
2024-10-05 20:20:28 +02:00
|
|
|
def macro_subpages(self, *args, **kwargs):
|
2024-10-07 17:19:29 +02:00
|
|
|
allpages = kwargs.pop("allpages", False)
|
|
|
|
#
|
|
|
|
|
2024-10-05 20:20:28 +02:00
|
|
|
def parse_depth(s: str):
|
|
|
|
try:
|
|
|
|
return int(s)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2024-10-07 17:19:29 +02:00
|
|
|
params = kwargs.get('', '')
|
|
|
|
startname = ''
|
|
|
|
depth = parse_depth(params)
|
2024-10-05 20:20:28 +02:00
|
|
|
if depth is None:
|
2024-10-07 17:19:29 +02:00
|
|
|
params = params.split(",")
|
|
|
|
depth = parse_depth(params[0])
|
|
|
|
if len(params) == 2:
|
|
|
|
startname = params[1]
|
|
|
|
elif depth is None:
|
|
|
|
startname = params[0]
|
|
|
|
if depth is None:
|
|
|
|
depth = 9999
|
2024-10-05 20:20:28 +02:00
|
|
|
#
|
|
|
|
rv = ""
|
2024-10-07 21:21:47 +02:00
|
|
|
# create a rel_path list
|
2024-10-09 09:57:05 +02:00
|
|
|
pathlist = [base_page(path).rel_path for path in fstools.dirlist(settings.PAGES_ROOT, rekursive=False)]
|
2024-10-07 21:21:47 +02:00
|
|
|
# sort basename
|
|
|
|
pathlist.sort(key=os.path.basename)
|
|
|
|
|
|
|
|
last_char = None
|
|
|
|
for contentname in pathlist:
|
2024-10-05 20:20:28 +02:00
|
|
|
#
|
2024-10-09 09:57:05 +02:00
|
|
|
if (contentname.startswith(self.rel_path) or allpages) and contentname != self.rel_path:
|
2024-10-07 17:19:29 +02:00
|
|
|
if allpages:
|
|
|
|
name = contentname
|
|
|
|
else:
|
2024-10-09 09:57:05 +02:00
|
|
|
name = contentname[len(self.rel_path)+1:]
|
2024-10-07 17:19:29 +02:00
|
|
|
if name.count('/') < depth and name.startswith(startname):
|
2024-10-07 21:21:47 +02:00
|
|
|
if last_char != os.path.basename(name)[0].upper():
|
|
|
|
last_char = os.path.basename(name)[0].upper()
|
|
|
|
if last_char is not None:
|
|
|
|
rv += "</ul>\n"
|
|
|
|
rv += f'<h3>{last_char}</h3>\n<ul>\n'
|
2024-10-07 16:47:15 +02:00
|
|
|
rv += f' <li><a href="{url_page(self._request, contentname)}">{name}</a></li>\n'
|
2024-10-05 20:20:28 +02:00
|
|
|
if len(rv) > 0:
|
2024-10-07 21:21:47 +02:00
|
|
|
rv += "</ul>\n"
|
2024-10-05 20:20:28 +02:00
|
|
|
return rv
|