Page tagging implemented
This commit is contained in:
parent
6e720eb3ac
commit
d81c2df678
@ -6,9 +6,11 @@ from django.forms.utils import ErrorList
|
|||||||
|
|
||||||
class EditForm(forms.Form): # Note that it is not inheriting from forms.ModelForm
|
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_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:
|
def __init__(self, *args, **kwargs) -> None:
|
||||||
page_data = kwargs.pop("page_data")
|
page_data = kwargs.pop("page_data")
|
||||||
|
page_tags = kwargs.pop("page_tags")
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
page_txt = self.fields['page_txt']
|
self.fields['page_txt'].initial = page_data
|
||||||
page_txt.initial = page_data
|
self.fields['page_tags'].initial = page_tags
|
||||||
|
@ -17,6 +17,7 @@ class meta_data(dict):
|
|||||||
KEY_CREATION_TIME = "creation_time"
|
KEY_CREATION_TIME = "creation_time"
|
||||||
KEY_MODIFIED_TIME = "modified_time"
|
KEY_MODIFIED_TIME = "modified_time"
|
||||||
KEY_MODIFIED_USER = "modified_user"
|
KEY_MODIFIED_USER = "modified_user"
|
||||||
|
KEY_TAGS = "tags"
|
||||||
|
|
||||||
def __init__(self, meta_filename, page_exists):
|
def __init__(self, meta_filename, page_exists):
|
||||||
self._meta_filename = meta_filename
|
self._meta_filename = meta_filename
|
||||||
@ -38,11 +39,15 @@ class meta_data(dict):
|
|||||||
if missing_keys and page_exists:
|
if missing_keys and page_exists:
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def update(self, username):
|
def update(self, username, tags):
|
||||||
self[self.KEY_MODIFIED_TIME] = int(time.time())
|
if username:
|
||||||
self[self.KEY_MODIFIED_USER] = username
|
self[self.KEY_MODIFIED_TIME] = int(time.time())
|
||||||
|
self[self.KEY_MODIFIED_USER] = username
|
||||||
|
if tags:
|
||||||
|
self[self.KEY_TAGS] = tags
|
||||||
#
|
#
|
||||||
self.save()
|
if username or tags:
|
||||||
|
self.save()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
with open(self._meta_filename, 'w') as fh:
|
with open(self._meta_filename, 'w') as fh:
|
||||||
@ -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.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)))
|
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
|
from .search import update_item
|
||||||
if page_txt.replace("\r\n", "\n") != self.raw_page_src:
|
if page_txt.replace("\r\n", "\n") != self.raw_page_src:
|
||||||
# Store page history
|
# Store page history
|
||||||
@ -97,12 +102,14 @@ class base_page(object):
|
|||||||
fstools.mkdir(os.path.dirname(self.filename))
|
fstools.mkdir(os.path.dirname(self.filename))
|
||||||
with open(self.filename, 'w') as fh:
|
with open(self.filename, 'w') as fh:
|
||||||
fh.write(page_txt)
|
fh.write(page_txt)
|
||||||
# update search index
|
|
||||||
update_item(self)
|
|
||||||
# update metadata
|
# update metadata
|
||||||
self._update_metadata()
|
page_changed = True
|
||||||
return True
|
else:
|
||||||
return False
|
page_changed = False
|
||||||
|
self._update_metadata(tags)
|
||||||
|
# update search index
|
||||||
|
update_item(self)
|
||||||
|
return page_changed
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def filename(self):
|
def filename(self):
|
||||||
@ -134,7 +141,7 @@ class base_page(object):
|
|||||||
self._load_page_src()
|
self._load_page_src()
|
||||||
return self._raw_page_src
|
return self._raw_page_src
|
||||||
|
|
||||||
def _update_metadata(self):
|
def _update_metadata(self, tags):
|
||||||
username = None
|
username = None
|
||||||
try:
|
try:
|
||||||
if self._request.user.is_authenticated:
|
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!")
|
logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
logger.exception("Page edit without having a request object. Check programming!")
|
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):
|
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')
|
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')
|
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)
|
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'|{_("Created")}:|{ctime}|\n'
|
||||||
meta += f'|{_("Modified")}:|{mtime}|\n'
|
meta += f'|{_("Modified")}:|{mtime}|\n'
|
||||||
meta += f'|{_("Editor")}|{user}|\n'
|
meta += f'|{_("Editor")}|{user}|\n'
|
||||||
|
meta += f'|{_("Tags")}|{tags}|\n'
|
||||||
meta += f'=== {_("Page content")}\n'
|
meta += f'=== {_("Page content")}\n'
|
||||||
meta += '{{{\n%s\n}}}\n' % self.raw_page_src
|
meta += '{{{\n%s\n}}}\n' % self.raw_page_src
|
||||||
#
|
#
|
||||||
|
@ -18,6 +18,7 @@ SCHEMA = Schema(
|
|||||||
# Page
|
# Page
|
||||||
title=TEXT,
|
title=TEXT,
|
||||||
page_src=TEXT,
|
page_src=TEXT,
|
||||||
|
page_tags=TEXT,
|
||||||
# metadata
|
# metadata
|
||||||
creation_time=DATETIME,
|
creation_time=DATETIME,
|
||||||
modified_time=DATETIME,
|
modified_time=DATETIME,
|
||||||
@ -63,6 +64,7 @@ def add_item(ix, bp: base_page):
|
|||||||
#
|
#
|
||||||
title=bp.title,
|
title=bp.title,
|
||||||
page_src=bp.raw_page_src,
|
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)),
|
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)),
|
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):
|
def whoosh_search(search_txt):
|
||||||
ix = load_index()
|
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))
|
qp.add_plugin(DateParserPlugin(free=True))
|
||||||
try:
|
try:
|
||||||
q = qp.parse(search_txt)
|
q = qp.parse(search_txt)
|
||||||
|
@ -59,7 +59,7 @@ def edit(request, rel_path):
|
|||||||
p = creole_page(request, rel_path)
|
p = creole_page(request, rel_path)
|
||||||
#
|
#
|
||||||
if not request.POST:
|
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_adaption(
|
||||||
context,
|
context,
|
||||||
@ -73,10 +73,11 @@ def edit(request, rel_path):
|
|||||||
else:
|
else:
|
||||||
save = request.POST.get("save")
|
save = request.POST.get("save")
|
||||||
page_txt = request.POST.get("page_txt")
|
page_txt = request.POST.get("page_txt")
|
||||||
|
tags = request.POST.get("page_tags")
|
||||||
preview = request.POST.get("preview")
|
preview = request.POST.get("preview")
|
||||||
#
|
#
|
||||||
if save is not None:
|
if save is not None:
|
||||||
if p.update_page(page_txt):
|
if p.update_page(page_txt, tags):
|
||||||
messages.edit_success(request)
|
messages.edit_success(request)
|
||||||
else:
|
else:
|
||||||
messages.edit_no_change(request)
|
messages.edit_no_change(request)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user