Skip to content

Commit

Permalink
Version 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
exonian committed Nov 7, 2015
1 parent 4062e2d commit 64cdb31
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def add_arguments(self, parser):
parser.add_argument('pattern', type=str, help='Pattern to search for')
parser.add_argument('identifiers', nargs='*', type=str, help='Identifier of a model or field')
parser.add_argument('--show-values', '-s', nargs='?', type=show_values_style, default='l',
help='Turn off showing matching values (default is any line containing a match), ' +
'or provide the mode "a" to show the entire field ' +
help='Turn off showing matching values (default is any line containing a match), ' \
'or provide the mode "a" to show the entire field ' \
'or an integer to show that many characters either side of a match.')
parser.add_argument('--ignore-case', '-i', action='store_true', help='Match case-insensitively')
parser.add_argument('--find-text-fields', '-t', dest='field_type', action='append_const', const='TextField',
Expand All @@ -38,15 +38,16 @@ def add_arguments(self, parser):
help='Search all CharField fields (and subclasses) on a model if no field is specified')
parser.add_argument('--find-fields', '-f', dest='field_type', action='append', type=str,
help='Search all fields of this type (and subclasses) on a model if no field is specified')
parser.add_argument('--preset', '-p', help='The name of a preset configuration in DJANGO_GREPDB_PRESETS. ' +
'DJANGO_GREPDB_PRESETS should be a dict of dicts, with each config dict providing ' +
parser.add_argument('--preset', '-p', help='The name of a preset configuration in DJANGO_GREPDB_PRESETS. ' \
'DJANGO_GREPDB_PRESETS should be a dict of dicts, with each config dict providing ' \
'default values for any number of parser args.')
if apps.is_installed('django.contrib.admin'):
parser.add_argument('--admin-links', '-l', dest='admin_hostname', nargs='*', default=['localhost:8000'],
help='Generate admin links. Defaults to true, using http://localhost:8000/ as hostname. ' +
'Can be passed one or more hostnames to use instead. If DJANGO_GREPDB_SITES is a ' +
'dict defined in settings, keys from it can also be passed to use their values as ' +
'hostnames. Links can be disabled by using this argument without any values.')
parser.add_argument('--admin-links', '-l', dest='admin_hostname', nargs='*', default=['default'],
help='Generate admin links. Defaults to true, using http://localhost:8000/ as hostname. ' \
'Can be passed one or more hostnames to use instead. If DJANGO_GREPDB_SITES is a ' \
'dict defined in settings, the value of the "default" key will be used as default, ' \
'and keys from it can also be passed to use their values as hostnames. ' \
'Links can be disabled by using this argument without any values.')
self.parser = parser

def handle(self, **options):
Expand Down Expand Up @@ -108,6 +109,16 @@ def get_admin_hostname(self, reference):
"""
if 'http' in reference or 'localhost' in reference:
return reference
try:
hostname = self.get_admin_hostname_from_settings(reference)
except CommandError:
if reference == 'default':
hostname = 'localhost:8000'
else:
raise
return hostname

def get_admin_hostname_from_settings(self, reference):
try:
sites = getattr(settings, 'DJANGO_GREPDB_SITES')
except AttributeError:
Expand Down
20 changes: 17 additions & 3 deletions build/lib.linux-x86_64-2.7/django_grepdb/tests/test_admin_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,21 @@ class TestWithAdminInstalled(TestCase):
def setUpTestData(cls):
TestModel.objects.create(text_field="The quick brown fox")

def test_default_link_generation_output(self):
"""Default is to generate admin links, and to use localhost:8000 as the hostname"""
def test_default_link_generation_output_with_defined_default(self):
out = StringIO()
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', stdout=out)
expected = "\x1b[1m\x1b[36m\n<class 'django_grepdb.tests.models.TestModel'> " \
"text_field\x1b[0m\n\x1b[1m\x1b[32mTestModel object " \
"(pk=1)\x1b[0m\n\x1b[32mhttps://local.example.com/admin/tests/testmodel/1/\x1b[0m\n"
self.assertEqual(out.getvalue(), expected)

@override_settings(
DJANGO_GREPDB_SITES = {
'staging': 'https://staging.example.com',
'production': 'https://example.com',
}
)
def test_default_link_generation_output_without_defined_default(self):
out = StringIO()
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', stdout=out)
expected = "\x1b[1m\x1b[36m\n<class 'django_grepdb.tests.models.TestModel'> " \
Expand Down Expand Up @@ -95,12 +108,13 @@ def test_option_with_sites_key(self):

def test_option_with_mixed_arguments(self):
out = StringIO()
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'staging', 'production',
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'staging', 'production', 'default',
'https://dev.example.com', stdout=out)
expected = "\x1b[1m\x1b[36m\n<class 'django_grepdb.tests.models.TestModel'> " \
"text_field\x1b[0m\n\x1b[1m\x1b[32mTestModel object (pk=1)\x1b[0m\n" \
"\x1b[32mhttps://staging.example.com/admin/tests/testmodel/1/\x1b[0m\n" \
"\x1b[32mhttps://example.com/admin/tests/testmodel/1/\x1b[0m\n" \
"\x1b[32mhttps://local.example.com/admin/tests/testmodel/1/\x1b[0m\n" \
"\x1b[32mhttps://dev.example.com/admin/tests/testmodel/1/\x1b[0m\n"
self.assertEqual(out.getvalue(), expected)

Expand Down
2 changes: 1 addition & 1 deletion build/lib.linux-x86_64-2.7/django_grepdb/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '1.0.0'
VERSION = '1.1.0'
Binary file added dist/django-grep-db-1.1.0.tar.gz
Binary file not shown.
Binary file added dist/django_grep_db-1.1.0-py2.py3-none-any.whl
Binary file not shown.
2 changes: 1 addition & 1 deletion django_grep_db.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 1.0
Name: django-grep-db
Version: 1.0.0
Version: 1.1.0
Summary: A simple Django app for command-line searching via the ORM
Home-page: https://github.com/exonian/django-grep-db
Author: Michael Blatherwick
Expand Down
2 changes: 1 addition & 1 deletion django_grep_db.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
django>=1.8
colorama>=0.3.3
termcolor>=1.1.0
termcolor>=1.1.0
2 changes: 1 addition & 1 deletion django_grepdb/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '1.0.0'
VERSION = '1.1.0'

0 comments on commit 64cdb31

Please sign in to comment.