Skip to content

Commit

Permalink
Reformatted code with Black.
Browse files Browse the repository at this point in the history
  • Loading branch information
django-bot authored and felixxm committed Jan 18, 2024
1 parent b644f85 commit de30a83
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 60 deletions.
10 changes: 4 additions & 6 deletions DjangoPlugin/setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from setuptools import setup

setup(
name='Django Plugin',
version='1.1',
packages=['tracdjangoplugin'],
name="Django Plugin",
version="1.1",
packages=["tracdjangoplugin"],
include_package_data=True,
entry_points = {
'trac.plugins': ['tracdjangoplugin = tracdjangoplugin']
}
entry_points={"trac.plugins": ["tracdjangoplugin = tracdjangoplugin"]},
)
51 changes: 29 additions & 22 deletions DjangoPlugin/tracdjangoplugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ class CustomWikiModule(WikiModule):
"""

def get_active_navigation_item(self, req):
pagename = req.args.get('page')
if pagename == 'Reports':
return 'custom_reports'
return 'wiki'
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'])
hidden_fields = frozenset(
["stage", "needs_tests", "needs_docs", "needs_better_patch"]
)

def match_request(self, req):
return req.path_info == '/simpleticket'
return req.path_info == "/simpleticket"

def process_request(self, req):
req.redirect(req.href.newticket())
Expand All @@ -36,26 +38,32 @@ def pre_process_request(self, req, handler):
def post_process_request(self, req, template, data, content_type):
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'
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, content_type


class CustomNavigationBar(Component):
"""Implements some more items for the navigation bar."""

implements(INavigationContributor)

def get_active_navigation_item(self, req):
return ''
return ""

def get_navigation_items(self, req):
return [
('mainnav', 'custom_reports', Markup('<a href="%s">Reports</a>' % req.href.wiki('Reports'))),
(
"mainnav",
"custom_reports",
Markup('<a href="%s">Reports</a>' % req.href.wiki("Reports")),
),
]


Expand All @@ -68,14 +76,13 @@ def get_navigation_items(self, req):
from genshi.builder import tag

class GitHubBrowserWithSVNChangesets(GitHubBrowser):

def _format_changeset_link(self, formatter, ns, chgset, label,
fullmatch=None):
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, '/')
href = formatter.href.changeset(chgset, None, "/")
return tag.a(label, class_="changeset", href=href)

# Fallback to the default implemntation
return (super(GitHubBrowserWithSVNChangesets,self)
._format_changeset_link(formatter, ns, chgset, label, fullmatch))
return super(GitHubBrowserWithSVNChangesets, self)._format_changeset_link(
formatter, ns, chgset, label, fullmatch
)
41 changes: 21 additions & 20 deletions DjangoPlugin/tracdjangoplugin/djangoauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,26 @@


class DjangoAuth:

login_url = getattr(settings, 'BASIC_AUTH_LOGIN_URL', '/login')
message = getattr(settings, 'BASIC_AUTH_MESSAGE', "Authorization required.")
message_type = getattr(settings, 'BASIC_AUTH_MESSAGE_TYPE', 'text/plain')
realm = getattr(settings, 'BASIC_AUTH_REALM', "Authenticate")
login_url = getattr(settings, "BASIC_AUTH_LOGIN_URL", "/login")
message = getattr(settings, "BASIC_AUTH_MESSAGE", "Authorization required.")
message_type = getattr(settings, "BASIC_AUTH_MESSAGE_TYPE", "text/plain")
realm = getattr(settings, "BASIC_AUTH_REALM", "Authenticate")

def __init__(self, application):
self.application = application

def __call__(self, environ, start_response):

try:
if get_path_info(environ) == self.login_url:
username = self.process_authorization(environ)
if username is None:
start_response('401 Unauthorized', [
('Content-Type', self.message_type),
('WWW-Authenticate', 'Basic realm="%s"' % self.realm),
])
start_response(
"401 Unauthorized",
[
("Content-Type", self.message_type),
("WWW-Authenticate", 'Basic realm="%s"' % self.realm),
],
)
return [self.message]
finally:
close_old_connections()
Expand All @@ -66,24 +67,24 @@ def __call__(self, environ, start_response):
@staticmethod
def process_authorization(environ):
# Don't override authentication information set by another component.
remote_user = environ.get('REMOTE_USER')
remote_user = environ.get("REMOTE_USER")
if remote_user is not None:
return

authorization = environ.get('HTTP_AUTHORIZATION')
authorization = environ.get("HTTP_AUTHORIZATION")
if authorization is None:
return

if six.PY3: # because fuck you PEP 3333.
authorization = authorization.encode('iso-8859-1').decode('utf-8')
if six.PY3: # because fuck you PEP 3333.
authorization = authorization.encode("iso-8859-1").decode("utf-8")

method, _, credentials = authorization.partition(' ')
if not method.lower() == 'basic':
method, _, credentials = authorization.partition(" ")
if not method.lower() == "basic":
return

try:
credentials = b64decode(credentials.strip())
username, _, password = credentials.partition(':')
username, _, password = credentials.partition(":")
except Exception:
return

Expand All @@ -92,9 +93,9 @@ def process_authorization(environ):

remote_user = username

if six.PY3: # because fuck you PEP 3333.
remote_user = remote_user.encode('utf-8').decode('iso-8859-1')
if six.PY3: # because fuck you PEP 3333.
remote_user = remote_user.encode("utf-8").decode("iso-8859-1")

environ['REMOTE_USER'] = remote_user
environ["REMOTE_USER"] = remote_user

return username
22 changes: 11 additions & 11 deletions DjangoPlugin/tracdjangoplugin/settings.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import json
import os

with open(os.environ.get('SECRETS_FILE')) as handle:
with open(os.environ.get("SECRETS_FILE")) as handle:
SECRETS = json.load(handle)

DEBUG = False

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'djangoproject',
'USER': 'djangoproject',
'HOST': SECRETS.get('db_host', ''),
'PORT': SECRETS.get('db_port', 5432),
'PASSWORD': SECRETS.get('db_password', ''),
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "djangoproject",
"USER": "djangoproject",
"HOST": SECRETS.get("db_host", ""),
"PORT": SECRETS.get("db_port", 5432),
"PASSWORD": SECRETS.get("db_password", ""),
},
}

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
"django.contrib.auth",
"django.contrib.contenttypes",
]


SECRET_KEY = str(SECRETS['secret_key'])
SECRET_KEY = str(SECRETS["secret_key"])

BASIC_AUTH_REALM = "Django's Trac"
5 changes: 4 additions & 1 deletion DjangoPlugin/tracdjangoplugin/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import os

import trac.web.main

application = trac.web.main.dispatch_request

# Massive hack to make Trac fast, otherwise every git call tries to close ulimit -n (1e6) fds
# Python 3 would perform better here, but we are still on 2.7 for Trac, so leak fds for now.
from tracopt.versioncontrol.git import PyGIT

PyGIT.close_fds = False

from .djangoauth import DjangoAuth

application = DjangoAuth(application)

trac_dsn = os.getenv("SENTRY_DSN")

if trac_dsn:
import sentry_sdk
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware

sentry_sdk.init(
dsn=trac_dsn,

# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
Expand Down

0 comments on commit de30a83

Please sign in to comment.