Skip to content

Commit 3851768

Browse files
authored
Merge pull request #113 from scieloorg/MarkupAPI-112-order-menu
Reorganiza menu do admin Wagtail (Marcação, Journal Manager, Content Manager e branding SciELO)
2 parents 92e3c5e + ca491f4 commit 3851768

7 files changed

Lines changed: 154 additions & 18 deletions

File tree

.git-hook-commit-msg

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env python3
2+
"""Git commit-msg hook.
3+
4+
Reads the activity (MarkAPI-NNNN) from the branch name -- anywhere in the
5+
name -- and:
6+
7+
1. validates the message format: <type>(<optional scope>): <message>
8+
allowed types: feature, fix, chore, unittest
9+
2. injects the activity prefix into the first line: [MarkAPI-NNNN] ...
10+
11+
Automatic Git commits (merge/revert/squash) are skipped.
12+
Uses the standard library only for portability across Linux and macOS.
13+
"""
14+
15+
import re
16+
import subprocess
17+
import sys
18+
19+
ALLOWED_TYPES = ("feature", "fix", "chore", "unittest")
20+
21+
BRANCH_ACTIVITY_RE = re.compile(r"MarkupAPI-(\d+)", re.IGNORECASE)
22+
EXISTING_PREFIX_RE = re.compile(r"^\s*\[MarkupAPI-\d+\]\s*", re.IGNORECASE)
23+
MESSAGE_FORMAT_RE = re.compile(
24+
r"^(?:%s)(?:\([a-z0-9-]+\))?: .+" % "|".join(ALLOWED_TYPES)
25+
)
26+
27+
# Automatic commits generated by Git that should pass without validation.
28+
MarkAPI_COMMIT_PREFIXES = (
29+
"Merge branch ",
30+
"Merge pull request ",
31+
"Merge remote-tracking ",
32+
'Revert "',
33+
)
34+
SQUASH_MERGE_RE = re.compile(r"^Merge .+ into ")
35+
36+
37+
def fail(message):
38+
print(message, file=sys.stderr)
39+
sys.exit(1)
40+
41+
42+
def is_auto_commit(first_line):
43+
if first_line.startswith(MarkAPI_COMMIT_PREFIXES):
44+
return True
45+
return bool(SQUASH_MERGE_RE.match(first_line))
46+
47+
48+
def current_branch():
49+
# symbolic-ref works even on the first commit (branch with no commits),
50+
# where rev-parse --abbrev-ref HEAD fails. On a detached HEAD,
51+
# symbolic-ref returns a non-zero code and we fall back to rev-parse
52+
# (which returns "HEAD").
53+
for cmd in (
54+
["git", "symbolic-ref", "--short", "HEAD"],
55+
["git", "rev-parse", "--abbrev-ref", "HEAD"],
56+
):
57+
try:
58+
result = subprocess.run(cmd, capture_output=True, text=True)
59+
except OSError:
60+
return None
61+
if result.returncode == 0:
62+
branch = result.stdout.strip()
63+
if branch:
64+
return branch
65+
return None
66+
67+
68+
def main():
69+
if len(sys.argv) < 2:
70+
fail("❌ commit-msg hook: message file path not provided.")
71+
72+
msg_path = sys.argv[1]
73+
with open(msg_path, "r", encoding="utf-8") as handle:
74+
lines = handle.read().splitlines()
75+
76+
if not lines:
77+
fail("❌ Empty commit message.")
78+
79+
first_line = lines[0].rstrip("\r")
80+
81+
# Let automatic Git commits pass through.
82+
if is_auto_commit(first_line):
83+
sys.exit(0)
84+
85+
branch = current_branch()
86+
if not branch or branch == "HEAD":
87+
fail(
88+
"❌ Could not determine the current branch (detached HEAD?).\n"
89+
"The branch must contain the activity MarkAPI-NNNN."
90+
)
91+
92+
match = BRANCH_ACTIVITY_RE.search(branch)
93+
if not match:
94+
fail(
95+
"❌ Branch without an activity.\n"
96+
"The branch name must contain MarkAPI-NNNN "
97+
"(e.g. feature/MarkAPI-1234/my-feature).\n"
98+
"Current branch: %s" % branch
99+
)
100+
101+
activity = "MarkAPI-%s" % match.group(1)
102+
103+
# Strip an existing prefix to avoid duplication (amend/rebase/reword).
104+
body = EXISTING_PREFIX_RE.sub("", first_line).strip()
105+
106+
if not MESSAGE_FORMAT_RE.match(body):
107+
fail(
108+
"❌ Invalid commit message.\n"
109+
"Expected format: <type>(<optional scope>): <message>\n"
110+
"Allowed types: %s\n"
111+
"Examples:\n"
112+
" chore: format readme\n"
113+
" fix(auth): fix authentication when token is invalid"
114+
% ", ".join(ALLOWED_TYPES)
115+
)
116+
117+
lines[0] = "[%s] %s" % (activity, body)
118+
119+
with open(msg_path, "w", encoding="utf-8") as handle:
120+
handle.write("\n".join(lines) + "\n")
121+
122+
sys.exit(0)
123+
124+
125+
if __name__ == "__main__":
126+
main()

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ latest_commit: ## Show last commit ref
3131
build_date: ## Show build date
3232
@echo "Build date: " $(SCMS_BUILD_DATE)
3333

34+
configure_git_hooks: ## Configure git hooks
35+
cp -fa .git-hook-commit-msg .git/hooks/commit-msg
36+
ln -sf ../../.git-hook-commit-msg .git/hooks/commit-msg
37+
3438
############################################
3539
## atalhos docker compose desenvolvimento ##
3640
############################################

config/menu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
WAGTAIL_MENU_APPS_ORDER = [
2-
"sps_package_validation",
32
"markup_doc",
3+
"reference",
44
"scielo",
55
"tracker",
66
"model_ai",

core/wagtail_hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def group_wagtail_cms_menu_items(request, menu_items):
5151
)
5252
menu_items.append(
5353
SubmenuMenuItem(
54-
_("Wagtail CMS"),
54+
_("Content Manager"),
5555
cms_menu,
5656
icon_name="folder-open-inverse",
5757
name="wagtail_cms",

markup_doc/wagtail_hooks.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
)
3333
from markup_doc.sync_api import sync_collection_from_api
3434
from markup_doc.tasks import get_labels, task_sync_journals_from_api, update_xml
35+
<<<<<<< HEAD
36+
from xml_manager.wagtail_hooks import (
37+
SPSPackageValidationSnippetViewSet,
38+
XMLDocumentHTMLSnippetViewSet,
39+
XMLDocumentPDFSnippetViewSet,
40+
)
41+
=======
42+
>>>>>>> 92e3c5e1efc5b93532d4cc264f5c408e416a3ff4
3543

3644

3745
@hooks.register("register_admin_urls")
@@ -101,7 +109,7 @@ def form_valid(self, form):
101109
class UploadDocxViewSet(SnippetViewSet):
102110
model = UploadDocx
103111
add_view_class = ArticleDocxCreateView
104-
menu_label = _("Carregar DOCX")
112+
menu_label = _("DOCX")
105113
menu_icon = "upload"
106114
add_to_admin_menu = False
107115
exclude_from_explorer = False
@@ -115,7 +123,7 @@ class MarkupXMLViewSet(SnippetViewSet):
115123
model = MarkupXML
116124
add_view_class = ArticleDocxMarkupCreateView
117125
edit_view_class = ArticleDocxEditView
118-
menu_label = _("XML SPS marcado")
126+
menu_label = _("XML SPS")
119127
menu_icon = "code"
120128
add_to_admin_menu = False
121129
exclude_from_explorer = False
@@ -139,7 +147,7 @@ def form_valid(self, form):
139147
class CollectionModelViewSet(SnippetViewSet):
140148
model = CollectionModel
141149
add_view_class = CollectionModelCreateView
142-
menu_label = _("Coleções SciELO")
150+
menu_label = _("Coleção")
143151
menu_icon = "folder-inverse"
144152
add_to_admin_menu = False
145153
exclude_from_explorer = False
@@ -203,15 +211,15 @@ class XMLSPSSnippetViewSetGroup(SnippetViewSetGroup):
203211
menu_icon = "code"
204212
items = (
205213
MarkupXMLViewSet,
214+
SPSPackageValidationSnippetViewSet,
206215
XMLDocumentPDFSnippetViewSet,
207216
XMLDocumentHTMLSnippetViewSet,
208-
ReferenceModelViewSet,
209217
)
210218

211219

212220
class ScieloSnippetViewSetGroup(SnippetViewSetGroup):
213221
menu_name = "scielo"
214-
menu_label = _("SciELO")
222+
menu_label = _("Journal Manager")
215223
menu_icon = "folder-open-inverse"
216224
menu_order = get_menu_order("scielo")
217225
items = (

reference/wagtail_hooks.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Third-party imports
22
from django.http import HttpResponseRedirect
33
from django.utils.translation import gettext_lazy as _
4+
from wagtail.snippets.models import register_snippet
45
from wagtail.snippets.views.snippets import CreateView, SnippetViewSet
56

67
# Local application imports
@@ -35,9 +36,12 @@ class ReferenceModelViewSet(SnippetViewSet):
3536
model = Reference
3637
add_view_class = ReferenceCreateView
3738
menu_name = "reference"
38-
menu_label = _("Referências bibliográficas")
39+
menu_label = _("Referências")
3940
menu_icon = "openquote"
4041
menu_order = get_menu_order("reference")
4142
exclude_from_explorer = False
4243
list_per_page = 20
43-
add_to_admin_menu = False
44+
add_to_admin_menu = True
45+
46+
47+
register_snippet(ReferenceModelViewSet)

xml_manager/wagtail_hooks.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from wagtail.snippets.models import register_snippet
1313
from wagtail.snippets.views.snippets import CreateView, EditView, SnippetViewSet
1414

15-
from config.menu import get_menu_order
16-
1715
from . import urls
1816
from .forms import SPSPackageValidationForm
1917
from .models import (
@@ -156,7 +154,7 @@ class XMLDocumentPDFSnippetViewSet(SnippetViewSet):
156154
verbose_name_plural = _("XML Document PDFs")
157155
icon = "doc-full"
158156
menu_name = "xml_manager"
159-
menu_label = _("PDFs derivados")
157+
menu_label = _("PDFs")
160158
menu_icon = "doc-full"
161159
add_to_admin_menu = False
162160

@@ -177,7 +175,7 @@ class XMLDocumentHTMLSnippetViewSet(SnippetViewSet):
177175
verbose_name_plural = _("XML Document HTMLs")
178176
icon = "doc-full"
179177
menu_name = "xml_manager"
180-
menu_label = _("HTMLs derivados")
178+
menu_label = _("HTMLs")
181179
menu_icon = "doc-full-inverse"
182180
add_to_admin_menu = False
183181

@@ -202,8 +200,7 @@ class SPSPackageValidationSnippetViewSet(SnippetViewSet):
202200
menu_name = "sps_package_validation"
203201
menu_label = _("Validar SPS")
204202
menu_icon = "sps-package-validation"
205-
add_to_admin_menu = True
206-
menu_order = get_menu_order("sps_package_validation")
203+
add_to_admin_menu = False
207204

208205
list_display = (
209206
"__str__",
@@ -220,9 +217,6 @@ class SPSPackageValidationSnippetViewSet(SnippetViewSet):
220217
search_fields = ("package_document__title",)
221218

222219

223-
register_snippet(SPSPackageValidationSnippetViewSet)
224-
225-
226220
@hooks.register("register_icons")
227221
def register_xml_manager_icons(icons):
228222
return icons + ["wagtailadmin/icons/sps-package-validation.svg"]

0 commit comments

Comments
 (0)