Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/moin/apps/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ def convert_item(item_name):
meta[MTIME] = int(time.time())
meta[REV_NUMBER] = meta.get(REV_NUMBER, 0) + 1
meta[COMMENT] = form["comment"].value
del meta["dataid"]
del meta[DATAID]
out.seek(0)
backend = flaskg.storage
storage_item = backend.get_item(**item.fqname.query)
Expand All @@ -918,7 +918,7 @@ def convert_item(item_name):
)
item_modified.send(
app,
fqname=meta["name"][0],
fqname=meta[NAME][0],
action=ACTION_CONVERT,
data=BytesIO(content),
meta=item.meta,
Expand Down Expand Up @@ -1559,7 +1559,7 @@ def name_initial(files, uppercase=False, lowercase=False):
if fqname.value == NAMESPACE_ALL:
fqname = CompositeName(NAMESPACE_ALL, NAME_EXACT, "")
item_names = item_name.split("/")
ns_len = len(item.meta["namespace"]) + 1 if item.meta["namespace"] else 0
ns_len = len(item.meta[NAMESPACE]) + 1 if item.meta[NAMESPACE] else 0

# detect orphan subitems and make a list of their missing parents
used_dirs = set()
Expand All @@ -1583,8 +1583,8 @@ def name_initial(files, uppercase=False, lowercase=False):
what = ""
if item.fqname.value == NAMESPACE_ALL:
title = _("Global Index of All Namespaces")
elif item.meta["namespace"]:
what = _("Namespace '{name}' ").format(name=item.meta["namespace"])
elif item.meta[NAMESPACE]:
what = _("Namespace '{name}' ").format(name=item.meta[NAMESPACE])
subitem = item_name[ns_len:]
if subitem:
what = what + _("subitems '{item_name}'").format(item_name=subitem)
Expand Down Expand Up @@ -1818,7 +1818,7 @@ def history(item_name):
entry[FQNAMES] = gen_fqnames(meta)
history.append(entry)
close_file(item.rev.data)
trash = item.meta["trash"] if "trash" in item.meta else False
trash = item.meta[TRASH] if TRASH in item.meta else False

# avoid repeated IO to get user profile when same user edits this item multiple times
editor_infos = {} # userid: user_info
Expand Down Expand Up @@ -2868,7 +2868,7 @@ def _diff(item, revid1, revid2, fqname, rev_ids):
newrev = item[revid2]
except KeyError:
abort(404)
if oldrev.meta["mtime"] > newrev.meta["mtime"]:
if oldrev.meta[MTIME] > newrev.meta[MTIME]:
# within diff, always place oldest on left, newest on right
oldrev, newrev = newrev, oldrev
revid1, revid2 = revid2, revid1
Expand Down Expand Up @@ -2911,7 +2911,7 @@ def _diff(item, revid1, revid2, fqname, rev_ids):
def _diff_raw(item, revid1, revid2):
oldrev = item[revid1]
newrev = item[revid2]
if oldrev.meta["mtime"] > newrev.meta["mtime"]:
if oldrev.meta[MTIME] > newrev.meta[MTIME]:
oldrev, newrev = newrev, oldrev
revid1, revid2 = revid2, revid1
commonmt = _common_type(oldrev.meta[CONTENTTYPE], newrev.meta[CONTENTTYPE])
Expand Down
10 changes: 5 additions & 5 deletions src/moin/cli/_tests/test_modify_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ def test_item_rev(index_create2):
assert f.read() == "MyPage version 2\r\n"
with open("MyPage-v2.meta") as f:
v2_meta = json.load(f)
assert v2_meta["size"] == 18 # newline at end is 2 chars \r\n
assert v2_meta[SIZE] == 18 # newline at end is 2 chars \r\n
with open(data_dir / "MyPage-v1.meta") as f:
v1_meta = json.load(f)
v1_revid = v1_meta["revid"]
assert v1_meta["size"] == 16
v1_revid = v1_meta[REVID]
assert v1_meta[SIZE] == 16
item_get1 = run(
["moin", "item-get", "-n", "MyPage", "-m", "MyPage-v1.meta", "-d", "MyPage-v1.data", "-r", v1_revid, "--crlf"]
)
Expand All @@ -164,8 +164,8 @@ def test_item_rev(index_create2):
assert f.read() == "MyPage version 1\r\n"
with open("MyPage-v1_1.meta") as f:
v1_1_meta = json.load(f)
assert v1_1_meta["revid"] != v1_revid # validate absence of -o option
assert v1_1_meta["size"] == 16 # validate no newline at end in storage
assert v1_1_meta[REVID] != v1_revid # validate absence of -o option
assert v1_1_meta[SIZE] == 16 # validate no newline at end in storage


def test_validate_metadata(index_create2):
Expand Down
4 changes: 2 additions & 2 deletions src/moin/cli/maint/dump_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

from moin.app import create_app, before_wiki, setup_user_anon
from moin.apps.frontend.views import show_item
from moin.constants.keys import CURRENT, NAME_EXACT, THEME_NAME, LATEST_REVS
from moin.constants.keys import CONTENTTYPE, CURRENT, NAME_EXACT, THEME_NAME, LATEST_REVS
from moin.constants.contenttypes import (
CONTENTTYPE_MEDIA,
CONTENTTYPE_MEDIA_SUFFIX,
Expand Down Expand Up @@ -235,7 +235,7 @@ def Dump(directory="HTML", theme="topside_cms", exclude_ns="userprofiles", user=
shutil.copyfileobj(rev.data, f)

# save rendered items or raw data to dump directory root
contenttype = item.meta["contenttype"].split(";")[0]
contenttype = item.meta[CONTENTTYPE].split(";")[0]
os.makedirs(os.path.dirname(filename), exist_ok=True)
if contenttype in (CONTENTTYPE_MEDIA + CONTENTTYPE_OTHER) and filename.endswith(
CONTENTTYPE_MEDIA_SUFFIX + CONTENTTYPE_OTHER_SUFFIX
Expand Down
26 changes: 19 additions & 7 deletions src/moin/cli/maint/modify_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@
from moin.cli._util import get_backends
from moin.storage.middleware.serialization import get_rev_str, correcting_rev_iter
from moin.constants.namespaces import NAMESPACE_USERPROFILES
from moin.constants.keys import CURRENT, ITEMID, DATAID, NAMESPACE, REVID, PARENTID, REV_NUMBER, MTIME, NAME
from moin.constants.keys import (
CONTENTTYPE,
CURRENT,
ITEMID,
DATAID,
NAMESPACE,
REVID,
SIZE,
PARENTID,
REV_NUMBER,
MTIME,
NAME,
)
Comment on lines +25 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rather use 2 lines with 2 import statements than this vertical formatting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that also already existed at some other places, so maybe ignore it for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the result of auto-formatting by black.

from moin.utils.interwiki import split_fqname
from moin.items import Item

Expand Down Expand Up @@ -74,11 +86,11 @@ def GetItem(name, meta_file, data_file, revid, newline="\n"):
meta = json.dumps(dict(rev.meta), sort_keys=True, indent=2, ensure_ascii=False)
with open(meta_file, "w", encoding="utf-8", newline=newline) as mf:
mf.write(meta + "\n")
if "charset" in rev.meta["contenttype"]:
if "charset" in rev.meta[CONTENTTYPE]:
# input data will have \r\n line endings, output will have specified endings
# those running on windows with git autocrlf=true will want --crlf
# those running on linux or with autocrlf=input will want --no-crlf
charset = rev.meta["contenttype"].split("charset=")[1]
charset = rev.meta[CONTENTTYPE].split("charset=")[1]
data = rev.data.read().decode(charset)
lines = data.splitlines()
# add trailing line ending which may have been removed by splitlines,
Expand Down Expand Up @@ -129,15 +141,15 @@ def PutItem(meta_file, data_file, overwrite):
item = app.storage.get_item(**query)

# we want \r\n line endings in data out because \r\n is required in form textareas
if "charset" in meta["contenttype"]:
charset = meta["contenttype"].split("charset=")[1]
if "charset" in meta[CONTENTTYPE]:
charset = meta[CONTENTTYPE].split("charset=")[1]
with open(data_file, "rb") as df:
data = df.read().decode(charset)
if "\r\n" not in data and "\n" in data:
data = data.replace("\n", "\r\n")
data = data.encode(charset)
if 0 < len(data) - meta["size"] <= 2:
data = data[0 : meta["size"]] # potentially truncate trailing newline added by _GetItem
if 0 < len(data) - meta[SIZE] <= 2:
data = data[0 : meta[SIZE]] # potentially truncate trailing newline added by _GetItem
buffer = io.BytesIO()
buffer.write(data)
buffer.seek(0)
Expand Down
4 changes: 2 additions & 2 deletions src/moin/converters/everything.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Convert any item to a DOM Tree (we just create a link to download it).
"""


from moin.constants.keys import NAME
from moin.utils.iri import Iri
from moin.utils.tree import moin_page, xlink
from moin.utils.mime import Type, type_moin_document
Expand All @@ -27,7 +27,7 @@ def _factory(cls, input, output, **kw):

def __call__(self, rev, contenttype=None, arguments=None):
try:
item_name = rev.item.fqname.fullname or rev.meta["name"][0]
item_name = rev.item.fqname.fullname or rev.meta[NAME][0]
except IndexError:
# item is deleted
message = _(
Expand Down
2 changes: 1 addition & 1 deletion src/moin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def u(self):
value = child.u.split(" ", 1)[0]
item = flaskg.storage.document(**{ITEMID: value.split(":")[1]})
try:
name_ = item.meta["name"][0]
name_ = item.meta[NAME][0]
except IndexError:
name_ = _("This item doesn't exist.")
except AttributeError:
Expand Down
36 changes: 19 additions & 17 deletions src/moin/items/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from moin.utils import diff3
from moin.forms import RequiredText, OptionalText, Tags, Names, validate_name, NameNotValidError, OptionalMultilineText
from moin.constants.keys import (
ACL,
NAME,
NAMES,
NAMENGRAM,
Expand Down Expand Up @@ -89,6 +90,7 @@
USERGROUP,
WIKIDICT,
LANGUAGE,
SUMMARY,
)
from moin.constants.chartypes import CHARS_UPPER, CHARS_LOWER
from moin.constants.namespaces import NAMESPACE_ALL, NAMESPACE_USERPROFILES
Expand Down Expand Up @@ -959,7 +961,7 @@ def destroy(self, comment="", destroy_item=False, subitem_names=[], ajax=False):
self.rev.item.destroy_revision(self.rev.revid)
flash(
L_('Rev Number {rev_number} of the item "{name}" was destroyed.').format(
rev_number=self.meta["rev_number"], name=old_name
rev_number=self.meta[REV_NUMBER], name=old_name
),
"info",
)
Expand Down Expand Up @@ -1005,8 +1007,8 @@ def _load(self, item):
# 'strict', which causes KeyError to be thrown when meta contains
# meta keys that are not present in self['meta_form']. Setting
# policy to 'duck' suppresses this behavior.
if "acl" not in meta:
meta["acl"] = "None"
if ACL not in meta:
meta[ACL] = "None"
self["meta_form"].set(meta, policy="duck")
if meta[NAME][0].endswith("Dict"):
try:
Expand Down Expand Up @@ -1088,13 +1090,13 @@ def _save(
else:
meta[LANGUAGE] = app.cfg.language_default

if "acl" in meta:
if ACL in meta:
# we treat this as nothing specified, so fallback to default
if meta["acl"] == "None":
meta.pop("acl")
if meta[ACL] == "None":
meta.pop(ACL)
# this is treated as a rule which matches nothing
elif meta["acl"] == "Empty":
meta["acl"] = ""
elif meta[ACL] == "Empty":
meta[ACL] = ""
# we store the previous (if different) and current item names into revision metadata
# this is useful for deletes, rename history and backends that use item uids internally
if self.fqname.field == NAME_EXACT:
Expand Down Expand Up @@ -1144,7 +1146,7 @@ def _save(

if isinstance(data, str):
data = self.handle_variables(data, meta)
charset = meta["contenttype"].split("charset=")[1]
charset = meta[CONTENTTYPE].split("charset=")[1]
data = data.encode(charset)

if isinstance(data, bytes):
Expand Down Expand Up @@ -1193,7 +1195,7 @@ def handle_variables(self, data, meta):
return data
if not request.path.startswith("/+modify"):
return data
if TEMPLATE in meta["tags"]:
if TEMPLATE in meta[TAGS]:
return data

logging.debug(f"handle_variable data: {data!r}") # log only if necessary
Expand Down Expand Up @@ -1445,10 +1447,10 @@ def do_show(self, revid, item_is_deleted=False, item_may=None):
rev_navigation_ids_dates = rev_navigation.prior_next_revs(revid, self.fqname)
# create extra meta tags for use by web crawlers
html_head_meta = {}
if "tags" in self.meta and self.meta["tags"]:
html_head_meta["keywords"] = ", ".join(self.meta["tags"])
if "summary" in self.meta and self.meta["summary"]:
html_head_meta["description"] = self.meta["summary"]
if TAGS in self.meta and self.meta[TAGS]:
html_head_meta["keywords"] = ", ".join(self.meta[TAGS])
if SUMMARY in self.meta and self.meta[SUMMARY]:
html_head_meta["description"] = self.meta[SUMMARY]
return render_template(
"show.html",
item=self,
Expand Down Expand Up @@ -1484,9 +1486,9 @@ def meta_changed(self, meta):
"""
if request.values.get(COMMENT):
return True
if request.values.get("meta_form_acl") != meta.get("acl", "None"):
if request.values.get("meta_form_acl") != meta.get(ACL, "None"):
return True
if request.values.get("meta_form_summary") != meta.get("summary", None):
if request.values.get("meta_form_summary") != meta.get(SUMMARY, None):
return True
if meta[NAME][0].endswith("Group"):
try:
Expand All @@ -1505,7 +1507,7 @@ def meta_changed(self, meta):
new_tags = request.values.get("meta_form_tags").replace(" ", "").split(",")
if new_tags == [""]:
new_tags = []
if new_tags != meta.get("tags", None):
if new_tags != meta.get(TAGS, None):
return True
return False

Expand Down
4 changes: 2 additions & 2 deletions src/moin/items/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
CONTENTTYPE_NONEXISTENT,
CHARSET,
)
from moin.constants.keys import NAME_EXACT, CONTENTTYPE, TAGS, TEMPLATE, HASH_ALGORITHM, ACTION_SAVE, NAMESPACE
from moin.constants.keys import NAME_EXACT, CONTENTTYPE, TAGS, TEMPLATE, HASH_ALGORITHM, ACTION_SAVE, NAMESPACE, REVID

from moin import log

Expand Down Expand Up @@ -988,7 +988,7 @@ def _render_data_diff_html(self, oldrev, newrev, template, rev_links={}, fqname=
from moin.items import Item # XXX causes import error if placed near top

diffs = self._get_data_diff_html(oldrev.data, newrev.data)
item = Item.create(fqname.fullname, rev_id=newrev.meta["revid"])
item = Item.create(fqname.fullname, rev_id=newrev.meta[REVID])
rendered = Markup(item.content._render_data())
return render_template(
template,
Expand Down
8 changes: 5 additions & 3 deletions src/moin/items/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
from moin.storage.middleware.protecting import AccessDenied
from moin.constants.keys import (
ITEMTYPE,
CLOSED,
CONTENTTYPE,
ITEMID,
CURRENT,
Expand All @@ -75,6 +76,7 @@
ELEMENT,
NAMESPACE,
REFERS_TO,
REPLY_TO,
CONTENT,
ACTION_TRASH,
)
Expand Down Expand Up @@ -214,7 +216,7 @@ def _dump(self, item):
meta.update(self["meta"].value)

if self["submit"].value == "update_negate_status":
meta["closed"] = not meta.get("closed")
meta[CLOSED] = not meta.get(CLOSED)

data = item.content.data_storage_to_internal(item.content.data)
message = self["message"].value
Expand Down Expand Up @@ -312,10 +314,10 @@ def get_comments(self):
lookup[rev.meta[ITEMID]] = rev
comments[rev] = []
for rev in revs:
if not rev.meta["reply_to"]:
if not rev.meta[REPLY_TO]:
roots.append(rev)
else:
parent = lookup[rev.meta["reply_to"]]
parent = lookup[rev.meta[REPLY_TO]]
comments[parent] = comments.get(parent, []) + [rev]
return comments, roots

Expand Down
4 changes: 2 additions & 2 deletions src/moin/utils/edit_locking.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from moin.i18n import L_
from moin.utils.mime import Type
from moin.constants.misc import ANON, NO_LOCK, LOCKED, LOCK
from moin.constants.keys import ITEMID, REVID, REV_NUMBER, NAME
from moin.constants.keys import CONTENTTYPE, ITEMID, REVID, REV_NUMBER, NAME
from moin.utils import show_time

from moin import log
Expand Down Expand Up @@ -73,7 +73,7 @@ def __init__(self, item):
self.item_id = item.meta.get(ITEMID, item.meta.get(NAME)[0])

self.coding = "utf-8"
contenttype = self.item.meta.get("contenttype", None)
contenttype = self.item.meta.get(CONTENTTYPE, None)
if contenttype is not None:
ct = Type(contenttype)
self.coding = ct.parameters.get("charset", self.coding)
Expand Down
5 changes: 2 additions & 3 deletions src/moin/utils/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
ACTION_SAVE,
ACTION_TRASH,
CONTENTTYPE,
REVID,
)
from moin.i18n import _, L_
from moin.i18n import force_locale
Expand Down Expand Up @@ -152,9 +153,7 @@ def generate_diff_url(self, domain):
"""
if self.new_data is None or self.data is None:
return ""
diff_rel_url = url_for(
"frontend.diff", item_name=self.fqname, rev1=self.meta["revid"], rev2=self.new_meta["revid"]
)
diff_rel_url = url_for("frontend.diff", item_name=self.fqname, rev1=self.meta[REVID], rev2=self.new_meta[REVID])
return urljoin(domain, diff_rel_url)

def render_templates(self, content_diff, meta_diff):
Expand Down
Loading