Skip to content

Commit

Permalink
* Added new command generate_secret_key to generate SECRET_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
sultaniman committed May 13, 2015
1 parent 6ef37b6 commit d6681a0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
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
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ At the end you can test new app by runing test::

Happy scaffolding!

Generation of SECRET_KEY
------------------------

Sometimes you need to generate a new ``SECRET_KEY`` so now you can generate it using this command:

$ python manage.py generate_secret_key


1. ``--length`` – is the length of the key ``default=50``
2. ``--alphabet`` – is the alphabet to use to generate the key ``default=ascii letters + punctuation symbols``


This open-source app is brought to you by Tivix, Inc. ( http://tivix.com/ )


Expand Down
32 changes: 32 additions & 0 deletions django_common/management/commands/generate_secret_key.py
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)
32 changes: 29 additions & 3 deletions django_common/tests.py
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()

0 comments on commit d6681a0

Please sign in to comment.