Skip to content

Commit 67fcd2c

Browse files
committed
Refs #8 - Add the beginnings of a management command, based on the API used in constance. So far just does list, and will only work under argparse enabled Django, rather than the old optparse one.
1 parent 4d5886f commit 67fcd2c

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

demo_project.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,24 @@
77
import os
88
import sys
99
sys.dont_write_bytecode = True
10-
MISSING_DEPENDENCY = False
10+
MISSING_DEPENDENCIES = []
1111
try:
1212
from django.conf import settings
1313
except ImportError:
14-
sys.stdout.write("You'll need to `pip install Django>=1.7` to run "
15-
"this demo\n")
16-
MISSING_DEPENDENCY = True
14+
MISSING_DEPENDENCIES.append("Django>=1.7")
1715

1816
try:
1917
import debug_toolbar
2018
except ImportError:
21-
sys.stdout.write("You'll need to `pip install django-debug-toolbar` to "
22-
"run this demo\n")
23-
MISSING_DEPENDENCY = True
19+
MISSING_DEPENDENCIES.append("django-debug-toolbar")
2420
try:
2521
import rest_framework
2622
except ImportError:
27-
sys.stdout.write("You'll need to `pip install djangorestframework>=3.2` "
28-
"to run this demo\n")
29-
MISSING_DEPENDENCY = True
23+
MISSING_DEPENDENCIES.append("djangorestframework>=3.2")
3024

31-
if MISSING_DEPENDENCY:
25+
if MISSING_DEPENDENCIES:
26+
deps = " ".join(MISSING_DEPENDENCIES)
27+
sys.stdout.write("You'll need to `pip install {}` to run this demo\n".format(deps))
3228
sys.exit(1)
3329

3430

@@ -59,6 +55,7 @@ def user_qs():
5955
'django.contrib.messages.middleware.MessageMiddleware',
6056
'django.contrib.sessions.middleware.SessionMiddleware',
6157
'django.contrib.auth.middleware.AuthenticationMiddleware',
58+
'debug_toolbar.middleware.DebugToolbarMiddleware',
6259
'stagesetting.middleware.ApplyRuntimeSettings',
6360
),
6461
DATABASES={

stagesetting/management/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals, absolute_import
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals, absolute_import
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals, absolute_import
3+
4+
from django.core.management import BaseCommand
5+
from stagesetting.models import RuntimeSetting, RuntimeSettingWrapper
6+
7+
8+
class Command(BaseCommand):
9+
help = ""
10+
11+
def get_model(self):
12+
return RuntimeSetting
13+
14+
def get_wrapper(self):
15+
return RuntimeSettingWrapper(model=self.get_model())
16+
17+
def add_arguments(self, parser):
18+
subparsers = parser.add_subparsers(dest='command')
19+
parser_list = subparsers.add_parser('list', cmd=self, help='list all')
20+
21+
22+
def handle(self, command, key=None, value=None, *args, **options):
23+
if command == "list":
24+
wrapped = self.get_wrapper()
25+
for key, value in sorted(wrapped):
26+
self.stdout.write(
27+
self.style.HTTP_REDIRECT(key),
28+
)
29+
data = sorted(value.items())
30+
maxlength = max(len(x[0]) for x in data)
31+
tmpl = "{{k: <{}}}".format(maxlength)
32+
for form_key, form_value in data:
33+
key = tmpl.format(k=form_key)
34+
self.stdout.write("{k}: {v!r}".format(k=self.style.HTTP_INFO(key), v=form_value))
35+
self.stdout.write("\n")

0 commit comments

Comments
 (0)