-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
631 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from django.core.management import CommandError, BaseCommand, get_commands, load_command_class | ||
|
||
|
||
def call_command(name, *args, **options): | ||
""" | ||
Calls the given command, with the given options and args/kwargs. | ||
This is the primary API you should use for calling specific commands. | ||
Some examples: | ||
call_command('migrate') | ||
call_command('shell', plain=True) | ||
call_command('sqlmigrate', 'myapp') | ||
Copy of the function from django.core.management. In addition to the | ||
standard functionality, stores the raw args on the command instance. | ||
""" | ||
# Load the command object. | ||
try: | ||
app_name = get_commands()[name] | ||
except KeyError: | ||
raise CommandError("Unknown command: %r" % name) | ||
|
||
if isinstance(app_name, BaseCommand): | ||
# If the command is already loaded, use it directly. | ||
command = app_name | ||
else: | ||
command = load_command_class(app_name, name) | ||
|
||
# Store the raw args on the command so they can be used later if needed | ||
command.raw_args = args | ||
|
||
# Simulate argument parsing to get the option defaults (see #10080 for details). | ||
parser = command.create_parser('', name) | ||
if command.use_argparse: | ||
# Use the `dest` option name from the parser option | ||
opt_mapping = {sorted(s_opt.option_strings)[0].lstrip('-').replace('-', '_'): s_opt.dest | ||
for s_opt in parser._actions if s_opt.option_strings} | ||
arg_options = {opt_mapping.get(key, key): value for key, value in options.items()} | ||
defaults = parser.parse_args(args=args) | ||
defaults = dict(defaults._get_kwargs(), **arg_options) | ||
# Move positional args out of options to mimic legacy optparse | ||
args = defaults.pop('args', ()) | ||
else: | ||
# Legacy optparse method | ||
defaults, _ = parser.parse_args(args=[]) | ||
defaults = dict(defaults.__dict__, **options) | ||
if 'skip_checks' not in options: | ||
defaults['skip_checks'] = True | ||
|
||
return command.execute(*args, **defaults) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
12 changes: 12 additions & 0 deletions
12
build/lib.linux-x86_64-2.7/django_grepdb/tests/admin_urls.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.conf.urls import include, url | ||
from django.contrib import admin | ||
|
||
from models import TestModel, TestModelTwo | ||
|
||
admin.site.register(TestModel) | ||
admin.site.register(TestModelTwo) | ||
|
||
|
||
urlpatterns = [ | ||
url(r'^admin/', include(admin.site.urls)), | ||
] |
31 changes: 31 additions & 0 deletions
31
build/lib.linux-x86_64-2.7/django_grepdb/tests/migrations/0001_initial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='TestModel', | ||
fields=[ | ||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | ||
('text_field', models.TextField(blank=True)), | ||
('text_field_two', models.TextField(blank=True)), | ||
('char_field', models.CharField(max_length=255, blank=True)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='TestModelTwo', | ||
fields=[ | ||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | ||
('text_field', models.TextField(blank=True)), | ||
('char_field', models.CharField(max_length=255, blank=True)), | ||
('url', models.URLField(blank=True)), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.db import models | ||
|
||
|
||
class TestModel(models.Model): | ||
text_field = models.TextField(blank=True) | ||
text_field_two = models.TextField(blank=True) | ||
char_field = models.CharField(blank=True, max_length=255) | ||
|
||
class Meta: | ||
app_label = 'tests' | ||
|
||
|
||
class TestModelTwo(models.Model): | ||
text_field = models.TextField(blank=True) | ||
char_field = models.CharField(blank=True, max_length=255) | ||
url = models.URLField(blank=True) | ||
|
||
class Meta: | ||
app_label = 'tests' |
114 changes: 114 additions & 0 deletions
114
build/lib.linux-x86_64-2.7/django_grepdb/tests/test_admin_links.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.conf import settings | ||
from django.core.management import call_command, CommandError | ||
from django.test import TestCase, override_settings | ||
from django.utils.six import StringIO | ||
|
||
from models import TestModel | ||
|
||
|
||
class TestWithoutAdminInstalled(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
TestModel.objects.create(text_field="The quick brown fox") | ||
|
||
def test_admin_option_not_used_by_default(self): | ||
out = StringIO() | ||
call_command('grepdb', 'brown', '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" | ||
self.assertEqual(out.getvalue(), expected) | ||
|
||
def test_admin_option_fails(self): | ||
with self.assertRaises(CommandError) as cm: | ||
call_command('grepdb', 'brown', 'tests.TestModel.text_field', '-s', '-l') | ||
self.assertEqual(cm.exception.message, u'Error: unrecognized arguments: -l') | ||
|
||
|
||
@override_settings( | ||
INSTALLED_APPS=[ | ||
'django.contrib.admin', | ||
'django.contrib.contenttypes', | ||
'django_grepdb', | ||
'django_grepdb.tests', | ||
], | ||
ROOT_URLCONF='django_grepdb.tests.admin_urls' | ||
) | ||
class TestWithAdminInstalled(TestCase): | ||
@classmethod | ||
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""" | ||
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[32mlocalhost:8000/admin/tests/testmodel/1/\x1b[0m\n" | ||
self.assertEqual(out.getvalue(), expected) | ||
|
||
def test_option_without_argument_turns_off_link_generation(self): | ||
out = StringIO() | ||
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 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" | ||
self.assertEqual(out.getvalue(), expected) | ||
|
||
def test_option_with_http_hostname(self): | ||
out = StringIO() | ||
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'http://fox.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[32mhttp://fox.example.com/admin/tests/testmodel/1/\x1b[0m\n" | ||
self.assertEqual(out.getvalue(), expected) | ||
|
||
def test_option_with_https_hostname(self): | ||
out = StringIO() | ||
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'https://fox.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://fox.example.com/admin/tests/testmodel/1/\x1b[0m\n" | ||
self.assertEqual(out.getvalue(), expected) | ||
|
||
def test_option_with_schemeless_hostname(self): | ||
with self.assertRaises(CommandError) as cm: | ||
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'fox.example.com') | ||
msg = u'Reference fox.example.com is not recognised as a hostname and was not found in DJANGO_GREPDB_SITES' | ||
self.assertEqual(cm.exception.message, msg) | ||
|
||
def test_option_with_localhost(self): | ||
out = StringIO() | ||
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'localhost:4000', 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[32mlocalhost:4000/admin/tests/testmodel/1/\x1b[0m\n" | ||
self.assertEqual(out.getvalue(), expected) | ||
|
||
def test_option_with_sites_key(self): | ||
out = StringIO() | ||
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'production', 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://example.com/admin/tests/testmodel/1/\x1b[0m\n" | ||
self.assertEqual(out.getvalue(), expected) | ||
|
||
def test_option_with_mixed_arguments(self): | ||
out = StringIO() | ||
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'staging', 'production', | ||
'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://dev.example.com/admin/tests/testmodel/1/\x1b[0m\n" | ||
self.assertEqual(out.getvalue(), expected) | ||
|
||
@override_settings() | ||
def test_option_without_sites_setting(self): | ||
del settings.DJANGO_GREPDB_SITES | ||
with self.assertRaises(CommandError) as cm: | ||
call_command('grepdb', 'quick', 'tests.TestModel.text_field', '-s', '-l', 'fox.example.com') | ||
msg = u'Reference fox.example.com is not recognised as a hostname and DJANGO_GREPDB_SITES is not ' \ | ||
'configured in settings' | ||
self.assertEqual(cm.exception.message, msg) |
Oops, something went wrong.