Skip to content

Commit

Permalink
Move plugins to dedicated module to be able to import django at toplevel
Browse files Browse the repository at this point in the history
Without that, an AppRegistryNotReady would be thrown when
trying to run the wsgi file.
  • Loading branch information
bmispelon committed Jan 29, 2024
1 parent 9ce5a20 commit b16651d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 95 deletions.
4 changes: 2 additions & 2 deletions DjangoPlugin/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]},
)
93 changes: 0 additions & 93 deletions DjangoPlugin/tracdjangoplugin/__init__.py
Original file line number Diff line number Diff line change
@@ -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('<a href="%s">Reports</a>' % 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
)
93 changes: 93 additions & 0 deletions DjangoPlugin/tracdjangoplugin/plugins.py
Original file line number Diff line number Diff line change
@@ -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('<a href="%s">Reports</a>' % 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
)

0 comments on commit b16651d

Please sign in to comment.