Page tagging implemented

This commit is contained in:
Dirk Alders 2024-10-11 14:23:50 +02:00
parent 6e720eb3ac
commit d81c2df678
4 changed files with 35 additions and 17 deletions

View File

@ -6,9 +6,11 @@ from django.forms.utils import ErrorList
class EditForm(forms.Form): # Note that it is not inheriting from forms.ModelForm
page_txt = forms.CharField(max_length=20000, label="Page source text", widget=forms.Textarea(attrs={"rows": "20"}))
page_tags = forms.CharField(max_length=20000, label="Tags (words separated by spaces)")
def __init__(self, *args, **kwargs) -> None:
page_data = kwargs.pop("page_data")
page_tags = kwargs.pop("page_tags")
super().__init__(*args, **kwargs)
page_txt = self.fields['page_txt']
page_txt.initial = page_data
self.fields['page_txt'].initial = page_data
self.fields['page_tags'].initial = page_tags

View File

@ -17,6 +17,7 @@ class meta_data(dict):
KEY_CREATION_TIME = "creation_time"
KEY_MODIFIED_TIME = "modified_time"
KEY_MODIFIED_USER = "modified_user"
KEY_TAGS = "tags"
def __init__(self, meta_filename, page_exists):
self._meta_filename = meta_filename
@ -38,10 +39,14 @@ class meta_data(dict):
if missing_keys and page_exists:
self.save()
def update(self, username):
def update(self, username, tags):
if username:
self[self.KEY_MODIFIED_TIME] = int(time.time())
self[self.KEY_MODIFIED_USER] = username
if tags:
self[self.KEY_TAGS] = tags
#
if username or tags:
self.save()
def save(self):
@ -87,7 +92,7 @@ class base_page(object):
shutil.copy(self.filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.PAGE_FILE_NAME)))
shutil.copy(self._meta_filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.META_FILE_NAME)))
def update_page(self, page_txt):
def update_page(self, page_txt, tags):
from .search import update_item
if page_txt.replace("\r\n", "\n") != self.raw_page_src:
# Store page history
@ -97,12 +102,14 @@ class base_page(object):
fstools.mkdir(os.path.dirname(self.filename))
with open(self.filename, 'w') as fh:
fh.write(page_txt)
# update metadata
page_changed = True
else:
page_changed = False
self._update_metadata(tags)
# update search index
update_item(self)
# update metadata
self._update_metadata()
return True
return False
return page_changed
@property
def filename(self):
@ -134,7 +141,7 @@ class base_page(object):
self._load_page_src()
return self._raw_page_src
def _update_metadata(self):
def _update_metadata(self, tags):
username = None
try:
if self._request.user.is_authenticated:
@ -143,7 +150,11 @@ class base_page(object):
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)
self._meta_data.update(username, tags)
@property
def page_tags(self):
return self._meta_data.get(self._meta_data.KEY_TAGS)
class creole_page(base_page):
@ -168,10 +179,12 @@ class creole_page(base_page):
ctime = timestamp_to_datetime(self._request, self._meta_data.get(self._meta_data.KEY_CREATION_TIME)).strftime('%Y-%m-%d %H:%M')
mtime = timestamp_to_datetime(self._request, self._meta_data.get(self._meta_data.KEY_MODIFIED_TIME)).strftime('%Y-%m-%d %H:%M')
user = self._meta_data.get(self._meta_data.KEY_MODIFIED_USER)
tags = self._meta_data.get(self._meta_data.KEY_TAGS, "-")
#
meta = f'|{_("Created")}:|{ctime}|\n'
meta += f'|{_("Modified")}:|{mtime}|\n'
meta += f'|{_("Editor")}|{user}|\n'
meta += f'|{_("Tags")}|{tags}|\n'
meta += f'=== {_("Page content")}\n'
meta += '{{{\n%s\n}}}\n' % self.raw_page_src
#

View File

@ -18,6 +18,7 @@ SCHEMA = Schema(
# Page
title=TEXT,
page_src=TEXT,
page_tags=TEXT,
# metadata
creation_time=DATETIME,
modified_time=DATETIME,
@ -63,6 +64,7 @@ def add_item(ix, bp: base_page):
#
title=bp.title,
page_src=bp.raw_page_src,
page_tags=bp.page_tags,
#
creation_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_CREATION_TIME)),
modified_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_MODIFIED_TIME)),
@ -77,7 +79,7 @@ def add_item(ix, bp: base_page):
def whoosh_search(search_txt):
ix = load_index()
qp = qparser.MultifieldParser(['title', 'page_src'], ix.schema)
qp = qparser.MultifieldParser(['title', 'page_src', 'page_tags'], ix.schema)
qp.add_plugin(DateParserPlugin(free=True))
try:
q = qp.parse(search_txt)

View File

@ -59,7 +59,7 @@ def edit(request, rel_path):
p = creole_page(request, rel_path)
#
if not request.POST:
form = EditForm(page_data=p.raw_page_src)
form = EditForm(page_data=p.raw_page_src, page_tags=p.page_tags)
#
context_adaption(
context,
@ -73,10 +73,11 @@ def edit(request, rel_path):
else:
save = request.POST.get("save")
page_txt = request.POST.get("page_txt")
tags = request.POST.get("page_tags")
preview = request.POST.get("preview")
#
if save is not None:
if p.update_page(page_txt):
if p.update_page(page_txt, tags):
messages.edit_success(request)
else:
messages.edit_no_change(request)