Login

djangoskel

Author:
ickewa
Posted:
March 19, 2008
Language:
Python
Version:
.96
Tags:
project skeleton
Score:
2 (after 2 ratings)

Sometimes i like to try things out in a blank django project, so i created a python script which creates an out of the box working project skeleton for me:

$ djangoskel.py project_name

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#!/usr/bin/python
import os
import sys
import pprint
from django.core.management import setup_environ

# personal data
USER = 'vyvyan'
EMAIL = 'Vyvyan Basterd@bbc.com'
PASSWORD = 'SPG'


# preparing environment
PROJECT = sys.argv[1]
CWD = os.getcwd()

PROJECT_ROOT = '%s/%s' % (CWD, PROJECT)
PYTHON_PATH = '%s/%s/src' % (CWD, PROJECT)
SRC_PATH = '%s/%s/src/%s' % (CWD, PROJECT, PROJECT)

sys.path.append(PYTHON_PATH)


# replacement values for settings 
my_settings = dict(
    ADMINS = ((USER, EMAIL),),
    TIME_ZONE = 'Europe/London',
    DATABASE_ENGINE = 'sqlite3',
    DATABASE_NAME = '%s/%s.db' % (PROJECT_ROOT, PROJECT),
    MEDIA_ROOT = '%s/media_root' % PROJECT_ROOT,
    MEDIA_URL = '/media_root',
    TEMPLATE_DIRS = ('%s/templates' % PROJECT_ROOT,),
    )


# creating project dirctories
os.mkdir(PROJECT_ROOT)
sub_dir = ('src', 'site_media', 'templates', 'media_root',
    'site_media/images', 'site_media/css', 'site_media/js')
for d in sub_dir:
    os.mkdir('%s/%s' % (PROJECT_ROOT, d))


# run startproject
os.chdir(PYTHON_PATH)
os.system('django-admin.py startproject %s' % PROJECT)


# rewrite settings.py
settings = __import__('%s/settings' % PROJECT)

for k, v in my_settings.items():
    setattr(settings, k, v)
# enabling admin app 
setattr(settings, 'INSTALLED_APPS', settings.INSTALLED_APPS + ('django.contrib.admin',))


settings_file = '%s/settings.py' % SRC_PATH
pp = pprint.PrettyPrinter(indent=4, width=50)
f = file(settings_file, 'w')
for attr in dir(settings):
    if not attr.startswith('__'):
        f.write('%s = ' % attr)
        f.write(pp.pformat(getattr(settings, attr)))
        f.write('\n\n')
f.close()


# running snycdb
os.chdir(SRC_PATH)
os.system('python manage.py syncdb --noinput')


#setting up django env
setup_environ(settings)


# creating superuser
from django.contrib.auth.models import User
u = User.objects.create_user(USER, EMAIL, PASSWORD)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save()
print "Superuser created successfully."


# enabling admin in urls.py
urls = '%s/urls.py' % SRC_PATH
content = ''
f = file(urls)
for line in f:
    if line.find('admin/') >=0:
        content += line[2:]
    else:
        content += line
f.close()
f = file(urls, 'w')
f.write(content)
f.close()

More like this

  1. EsayExtJS4: A simple manner to use Ext JS 4 with your Django project by tofplay 3 years, 9 months ago
  2. pyserver -- runserver alias by presclark 7 years, 3 months ago
  3. Remove a clause from a queryset by wahuneke 1 month, 2 weeks ago
  4. Google Charts Templatetags (Python) by justquick 7 years, 2 months ago
  5. Automatically set up a project by madprops 3 years, 1 month ago

Comments

Please login first before commenting.