Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Handle exessively long help text for jobs. #1802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion django_extensions/management/commands/runjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ def handle(self, *args, **options):
setup_logger(logger, self.stdout)

if options['list_jobs']:
print_jobs(only_scheduled=False, show_when=True, show_appname=True)
print_jobs(only_scheduled=False, show_when=True, show_appname=True, verbose=options['verbosity'] >= 2)
else:
self.runjob(app_name, job_name, options)
46 changes: 28 additions & 18 deletions django_extensions/management/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import os
import sys
import importlib
import textwrap
from typing import Optional # NOQA
from django.apps import apps
from rich.console import Console
from rich.table import Table, Column

_jobs = None

Expand Down Expand Up @@ -147,34 +150,41 @@ def get_job(app_name, job_name):
raise KeyError("Job not found: %s" % job_name)


def print_jobs(when=None, only_scheduled=False, show_when=True, show_appname=False, show_header=True):
def format_help_text(txt: str, digest: bool=False, maxwidth: int=80) -> str:
"""Return the help text properly formatted."""
paragraphs = txt.split('\n\n')
if digest:
return textwrap.shorten(paragraphs[0], maxwidth)
return '\n\n'.join(textwrap.dedent(p).replace('\n', ' ') for p in paragraphs)


def print_jobs(when=None, only_scheduled=False, show_when=True, show_appname=False, show_header=True, verbose=False):
console = Console()

jobmap = get_jobs(when, only_scheduled=only_scheduled)
print("Job List: %i jobs" % len(jobmap))
console.print("Job List: %i jobs" % len(jobmap))
jlist = sorted(jobmap.keys())
if not jlist:
return

appname_spacer = "%%-%is" % max(len(e[0]) for e in jlist)
name_spacer = "%%-%is" % max(len(e[1]) for e in jlist)
when_spacer = "%%-%is" % max(len(e.when) for e in jobmap.values() if e.when)
table = Table()

if show_header:
line = " "
if show_appname:
line += appname_spacer % "appname" + " - "
line += name_spacer % "jobname"
table.add_column('appname')
table.add_column('jobname')
if show_when:
line += " - " + when_spacer % "when"
line += " - help"
print(line)
print("-" * 80)
table.add_column('when')
table.add_column('help')

for app_name, job_name in jlist:
job = jobmap[(app_name, job_name)]
line = " "
row = []
if show_appname:
line += appname_spacer % app_name + " - "
line += name_spacer % job_name
row.append(app_name)
row.append(job_name)
if show_when:
line += " - " + when_spacer % (job.when and job.when or "")
line += " - " + job.help
print(line)
row.append(job.when and job.when or "")
row.append(format_help_text(job.help, digest=not verbose))
table.add_row(*row)
console.print(table)
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ def fullsplit(path, result=None):
cmdclass=cmdclasses,
package_data=package_data,
python_requires=">=3.6",
install_requires=["Django>=3.2"],
install_requires=[
"Django>=3.2",
"rich"
],
extras_require={},
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
4 changes: 2 additions & 2 deletions tests/management/commands/test_runjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def test_sample_job(self):

def test_list_jobs(self):
call_command('runjob', '-l', verbosity=2)
self.assertRegex(sys.stdout.getvalue(), "tests.testapp +- sample_job +- +- My sample job.\n")
self.assertRegex(sys.stdout.getvalue(), r"tests.testapp[^\w]+sample_job[^\w]+My sample job.")

def test_list_jobs_appconfig(self):
with self.modify_settings(INSTALLED_APPS={
'append': 'tests.testapp.apps.TestAppConfig',
'remove': 'tests.testapp',
}):
call_command('runjob', '-l', verbosity=2)
self.assertRegex(sys.stdout.getvalue(), "tests.testapp +- sample_job +- +- My sample job.\n")
self.assertRegex(sys.stdout.getvalue(), r"tests.testapp[^\w]+sample_job[^\w]+My sample job.")

def test_runs_appconfig(self):
with self.modify_settings(INSTALLED_APPS={
Expand Down