From b16651dc9394beb51b8a5bd904e44d32b2ff6fb5 Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Sun, 28 Jan 2024 23:42:25 +0100 Subject: [PATCH] Move plugins to dedicated module to be able to import django at toplevel Without that, an AppRegistryNotReady would be thrown when trying to run the wsgi file. --- DjangoPlugin/setup.py | 4 +- DjangoPlugin/tracdjangoplugin/__init__.py | 93 ----------------------- DjangoPlugin/tracdjangoplugin/plugins.py | 93 +++++++++++++++++++++++ 3 files changed, 95 insertions(+), 95 deletions(-) create mode 100644 DjangoPlugin/tracdjangoplugin/plugins.py diff --git a/DjangoPlugin/setup.py b/DjangoPlugin/setup.py index 7df9411..6a3795a 100644 --- a/DjangoPlugin/setup.py +++ b/DjangoPlugin/setup.py @@ -2,8 +2,8 @@ setup( name="Django Plugin", - version="1.1", + version="1.2", packages=["tracdjangoplugin"], include_package_data=True, - entry_points={"trac.plugins": ["tracdjangoplugin = tracdjangoplugin"]}, + entry_points={"trac.plugins": ["tracdjangoplugin = tracdjangoplugin.plugins"]}, ) diff --git a/DjangoPlugin/tracdjangoplugin/__init__.py b/DjangoPlugin/tracdjangoplugin/__init__.py index 52a08f3..e69de29 100644 --- a/DjangoPlugin/tracdjangoplugin/__init__.py +++ b/DjangoPlugin/tracdjangoplugin/__init__.py @@ -1,93 +0,0 @@ -from trac.core import Component, implements -from trac.web.chrome import INavigationContributor -from trac.web.api import IRequestFilter, IRequestHandler -from trac.wiki.web_ui import WikiModule -from trac.util import Markup -from trac.util.html import tag -from tracext.github import GitHubBrowser - - -class CustomTheme(Component): - implements(IRequestFilter) - - def pre_process_request(self, req, handler): - return handler - - def post_process_request(self, req, template, data, metadata): - req.chrome["theme"] = "django_theme.html" - return template, data, metadata - - -class CustomWikiModule(WikiModule): - """Works in combination with the CustomNavigationBar and replaces - the default wiki module. Has a different logic for active item - handling. - """ - - def get_active_navigation_item(self, req): - pagename = req.args.get("page") - if pagename == "Reports": - return "custom_reports" - return "wiki" - - -class CustomNewTicket(Component): - """Hide certain options for the new ticket page""" - - implements(IRequestFilter, IRequestHandler) - hidden_fields = frozenset( - ["stage", "needs_tests", "needs_docs", "needs_better_patch"] - ) - - def match_request(self, req): - return req.path_info == "/simpleticket" - - def process_request(self, req): - req.redirect(req.href.newticket()) - - def pre_process_request(self, req, handler): - return handler - - def post_process_request(self, req, template, data, metadata): - if data is None: - data = {} - if req.path_info == "/newticket" and not data.get("preview_mode", False): - simple_interface = "TICKET_BATCH_MODIFY" not in req.perm - if simple_interface and "fields" in data: - data["fields"] = [ - f for f in data["fields"] if f["name"] not in self.hidden_fields - ] - data["simple_interface"] = simple_interface - template = "custom_ticket.html" - return template, data, metadata - - -class CustomNavigationBar(Component): - """Implements some more items for the navigation bar.""" - - implements(INavigationContributor) - - def get_active_navigation_item(self, req): - return "custom_reports" - - def get_navigation_items(self, req): - return [ - ( - "mainnav", - "custom_reports", - Markup('Reports' % req.href.wiki("Reports")), - ), - ] - - -class GitHubBrowserWithSVNChangesets(GitHubBrowser): - def _format_changeset_link(self, formatter, ns, chgset, label, fullmatch=None): - # Dead-simple version for SVN changesets. - if chgset.isnumeric(): - href = formatter.href.changeset(chgset, None, "/") - return tag.a(label, class_="changeset", href=href) - - # Fallback to the default implementation. - return super(GitHubBrowserWithSVNChangesets, self)._format_changeset_link( - formatter, ns, chgset, label, fullmatch - ) diff --git a/DjangoPlugin/tracdjangoplugin/plugins.py b/DjangoPlugin/tracdjangoplugin/plugins.py new file mode 100644 index 0000000..52a08f3 --- /dev/null +++ b/DjangoPlugin/tracdjangoplugin/plugins.py @@ -0,0 +1,93 @@ +from trac.core import Component, implements +from trac.web.chrome import INavigationContributor +from trac.web.api import IRequestFilter, IRequestHandler +from trac.wiki.web_ui import WikiModule +from trac.util import Markup +from trac.util.html import tag +from tracext.github import GitHubBrowser + + +class CustomTheme(Component): + implements(IRequestFilter) + + def pre_process_request(self, req, handler): + return handler + + def post_process_request(self, req, template, data, metadata): + req.chrome["theme"] = "django_theme.html" + return template, data, metadata + + +class CustomWikiModule(WikiModule): + """Works in combination with the CustomNavigationBar and replaces + the default wiki module. Has a different logic for active item + handling. + """ + + def get_active_navigation_item(self, req): + pagename = req.args.get("page") + if pagename == "Reports": + return "custom_reports" + return "wiki" + + +class CustomNewTicket(Component): + """Hide certain options for the new ticket page""" + + implements(IRequestFilter, IRequestHandler) + hidden_fields = frozenset( + ["stage", "needs_tests", "needs_docs", "needs_better_patch"] + ) + + def match_request(self, req): + return req.path_info == "/simpleticket" + + def process_request(self, req): + req.redirect(req.href.newticket()) + + def pre_process_request(self, req, handler): + return handler + + def post_process_request(self, req, template, data, metadata): + if data is None: + data = {} + if req.path_info == "/newticket" and not data.get("preview_mode", False): + simple_interface = "TICKET_BATCH_MODIFY" not in req.perm + if simple_interface and "fields" in data: + data["fields"] = [ + f for f in data["fields"] if f["name"] not in self.hidden_fields + ] + data["simple_interface"] = simple_interface + template = "custom_ticket.html" + return template, data, metadata + + +class CustomNavigationBar(Component): + """Implements some more items for the navigation bar.""" + + implements(INavigationContributor) + + def get_active_navigation_item(self, req): + return "custom_reports" + + def get_navigation_items(self, req): + return [ + ( + "mainnav", + "custom_reports", + Markup('Reports' % req.href.wiki("Reports")), + ), + ] + + +class GitHubBrowserWithSVNChangesets(GitHubBrowser): + def _format_changeset_link(self, formatter, ns, chgset, label, fullmatch=None): + # Dead-simple version for SVN changesets. + if chgset.isnumeric(): + href = formatter.href.changeset(chgset, None, "/") + return tag.a(label, class_="changeset", href=href) + + # Fallback to the default implementation. + return super(GitHubBrowserWithSVNChangesets, self)._format_changeset_link( + formatter, ns, chgset, label, fullmatch + )