forked from Tivix/django-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added new command generate_secret_key to generate SECRET_KEY
- Loading branch information
1 parent
6ef37b6
commit d6681a0
Showing
4 changed files
with
76 additions
and
4 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
*.pyc | ||
*.DS_Store | ||
dist | ||
build | ||
django_common_helpers.egg-info | ||
.idea/ | ||
.idea/ | ||
*.egg-info |
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
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,32 @@ | ||
import string | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.utils.crypto import get_random_string | ||
from django.utils.translation import ugettext as _ | ||
|
||
|
||
class Command(BaseCommand): | ||
help = _('This command generates SECRET_KEY') | ||
|
||
# Default length is 50 | ||
length = 50 | ||
|
||
# Allowed characters | ||
allowed_chars = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation | ||
|
||
def add_arguments(self, parser): | ||
""" | ||
Define optional arguments with default values | ||
""" | ||
parser.add_argument('--length', default=self.length, | ||
type=int, help=_('SECRET_KEY length default=%d' % self.length)) | ||
|
||
parser.add_argument('--alphabet', default=self.allowed_chars, | ||
type=str, help=_('alphabet to use default=%s' % self.allowed_chars)) | ||
|
||
def handle(self, *args, **options): | ||
length = options.get('length') | ||
alphabet = options.get('alphabet') | ||
secret_key = str(get_random_string(length=length, allowed_chars=alphabet)) | ||
|
||
print('SECRET_KEY: %s' % secret_key) |
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 |
---|---|---|
@@ -1,12 +1,38 @@ | ||
from __future__ import print_function, unicode_literals, with_statement, division | ||
|
||
from django.utils import unittest | ||
from django.core.management import call_command | ||
|
||
try: | ||
from cStringIO import StringIO | ||
except ImportError: | ||
from StringIO import StringIO | ||
|
||
import sys | ||
import random | ||
|
||
|
||
class SimpleTestCase(unittest.TestCase): | ||
def setUp(self): | ||
pass | ||
|
||
def test_example(self): | ||
"""Some test example""" | ||
pass | ||
def test_generate_secret_key(self): | ||
""" Test generation of a secret key """ | ||
out = StringIO() | ||
sys.stdout = out | ||
|
||
for i in range(10): | ||
random_number = random.randrange(10, 100) | ||
call_command('generate_secret_key', length=random_number) | ||
secret_key = self._get_secret_key(out.getvalue()) | ||
|
||
out.truncate(0) | ||
out.seek(0) | ||
|
||
assert len(secret_key) == random_number | ||
|
||
def _get_secret_key(self, result): | ||
""" Get only the value of a SECRET_KEY """ | ||
for index, key in enumerate(result): | ||
if key == ':': | ||
return str(result[index + 1:]).strip() |