|
| 1 | +import os |
| 2 | + |
| 3 | +def _get_django_admin(bin_env): |
| 4 | + if not bin_env: |
| 5 | + da = 'django-admin.py' |
| 6 | + else: |
| 7 | + # try to get pip bin from env |
| 8 | + if os.path.exists(os.path.join(bin_env, 'bin', 'django-admin.py')): |
| 9 | + da = os.path.join(bin_env, 'bin', 'django-admin.py') |
| 10 | + else: |
| 11 | + da = bin_env |
| 12 | + return da |
| 13 | + |
| 14 | + |
| 15 | +def command(settings_module, |
| 16 | + command, |
| 17 | + bin_env=None, |
| 18 | + pythonpath=None, |
| 19 | + *args, **kwargs): |
| 20 | + """ |
| 21 | + run arbitrary django management command |
| 22 | + """ |
| 23 | + da = _get_django_admin(bin_env) |
| 24 | + cmd = "{0} {1} --settings={2}".format(da, command, settings_module) |
| 25 | + |
| 26 | + if pythonpath: |
| 27 | + cmd = "{0} --pythonpath={1}".format(cmd, pythonpath) |
| 28 | + |
| 29 | + for arg in args: |
| 30 | + cmd = "{0} --{1}".format(cmd, arg) |
| 31 | + |
| 32 | + for key, value in kwargs.iteritems(): |
| 33 | + cmd = '{0} --{1}={2}'.format(cmd, key, value) |
| 34 | + |
| 35 | + return __salt__['cmd.run'](cmd) |
| 36 | + |
| 37 | + |
| 38 | +def syncdb(settings_module, |
| 39 | + bin_env=None, |
| 40 | + migrate=False, |
| 41 | + database=None, |
| 42 | + pythonpath=None): |
| 43 | + """ |
| 44 | + run syncdb |
| 45 | +
|
| 46 | + if you have south installed, you can pass in the optional |
| 47 | + ``migrate`` kwarg and run the migrations after the syncdb |
| 48 | + finishes. |
| 49 | + """ |
| 50 | + da = _get_django_admin(bin_env) |
| 51 | + cmd = "{0} syncdb --settings={1}".format(da, settings_module) |
| 52 | + if migrate: |
| 53 | + cmd = "{0} --migrate".format(cmd) |
| 54 | + if database: |
| 55 | + cmd = "{0} --database={1}".format(cmd, database) |
| 56 | + if pythonpath: |
| 57 | + cmd = "{0} --pythonpath={1}".format(cmd, pythonpath) |
| 58 | + return __salt__['cmd.run'](cmd) |
| 59 | + |
| 60 | + |
| 61 | +def createsuperuser(settings_module, |
| 62 | + username, |
| 63 | + email, |
| 64 | + bin_env=None, |
| 65 | + database=None, |
| 66 | + pythonpath=None): |
| 67 | + """ |
| 68 | + create a super user for the database. |
| 69 | + this defaults to use the ``--noinput`` flag which will |
| 70 | + not create a password for the superuser. |
| 71 | + """ |
| 72 | + da = _get_django_admin(bin_env) |
| 73 | + cmd = "{0} createsuperuser --settings={1} --noinput --email='{2}' --username={3}".format( |
| 74 | + da, settings_module, email, username) |
| 75 | + if database: |
| 76 | + cmd = "{0} --database={1}".format(cmd, database) |
| 77 | + if pythonpath: |
| 78 | + cmd = "{0} --pythonpath={1}".format(cmd, pythonpath) |
| 79 | + return __salt__['cmd.run'](cmd) |
| 80 | + |
| 81 | + |
| 82 | +def loaddata(settings_module, |
| 83 | + fixtures, |
| 84 | + bin_env=None, |
| 85 | + database=None, |
| 86 | + pythonpath=None): |
| 87 | + """ |
| 88 | + load fixture data |
| 89 | +
|
| 90 | + fixtures: |
| 91 | + comma separated list of fixtures to load |
| 92 | +
|
| 93 | + """ |
| 94 | + da = _get_django_admin(bin_env) |
| 95 | + cmd = "{0} loaddata --settings={1} {2}".format( |
| 96 | + da, settings_module, " ".join(fixtures.split(","))) |
| 97 | + if database: |
| 98 | + cmd = "{0} --database={1}".format(cmd, database) |
| 99 | + if pythonpath: |
| 100 | + cmd = "{0} --pythonpath={1}".format(cmd, pythonpath) |
| 101 | + return __salt__['cmd.run'](cmd) |
| 102 | + |
| 103 | + |
| 104 | +def collectstatic(settings_module, |
| 105 | + bin_env=None, |
| 106 | + no_post_process=False, |
| 107 | + ignore=None, |
| 108 | + dry_run=False, |
| 109 | + clear=False, |
| 110 | + link=False, |
| 111 | + no_default_ignore=False, |
| 112 | + pythonpath=None): |
| 113 | + |
| 114 | + da = _get_django_admin(bin_env) |
| 115 | + cmd = "{0} collectstatic --settings={1} --noinput".format( |
| 116 | + da, settings_module) |
| 117 | + if no_post_process: |
| 118 | + cmd = "{0} --no-post-process".format(cmd) |
| 119 | + if ignore: |
| 120 | + cmd = "{0} --ignore=".format(cmd, ignore) |
| 121 | + if dry_run: |
| 122 | + cmd = "{0} --dry-run".format(cmd) |
| 123 | + if clear: |
| 124 | + cmd = "{0} --clear".format(cmd) |
| 125 | + if link: |
| 126 | + cmd = "{0} --link".format(cmd) |
| 127 | + if no_default_ignore: |
| 128 | + cmd = "{0} --no-default-ignore".format(cmd) |
| 129 | + if pythonpath: |
| 130 | + cmd = "{0} --pythonpath={1}".format(cmd, pythonpath) |
| 131 | + |
| 132 | + return __salt__['cmd.run'](cmd) |
0 commit comments