Skip to content

Commit

Permalink
Added createuser command
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Jurk committed Mar 8, 2015
1 parent 5094f90 commit c39381b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This repository contains the backend and front-end code for the development rele

1. Check out the source code
2. Create a virtual environment and install all the dependencies using `pip install -r requirements.txt`
3. Rename `ydns/local_settins.example.py` to `ydns/local_settings.py` and adjust the configuration. If you'd like to use the OAuth2 login features, you may have to obtain approprite API credentials
3. Rename `ydns/local_settins.example.py` to `ydns/local_settings.py` and adjust the configuration. If you'd like to use the OAuth2 login features, you may have to obtain appropriate API credentials
4. Setup a database and add the configuration to your local_settings.py
5. Apply database migrations by using `./manage.py migrate` inside your YDNS folder
6. Launch the local server by using `./manage.py runserver`
Expand Down
1 change: 1 addition & 0 deletions accounts/management/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'cjurk'
1 change: 1 addition & 0 deletions accounts/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'cjurk'
101 changes: 101 additions & 0 deletions accounts/management/commands/createuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
##
# YDNS Core
#
# Copyright (c) 2015 Christian Jurk <commx@commx.ws>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
##

from accounts.models import User
from django.core.management.base import BaseCommand
from django.core.validators import validate_email, ValidationError
from getpass import getpass
from optparse import make_option

class Command(BaseCommand):
"""
This management command is used to create user accounts
via command line. Useful if you'd like to create an account
with administrative privileges.
"""
args = '<email>'
help = 'Create user account'
option_list = BaseCommand.option_list + (
make_option('-p',
action='store_true',
dest='password',
default=False,
help='Assign password to account'),
make_option('--admin',
action='store_true',
dest='admin',
default=False,
help='Assign admin privileges to account'),
)

def handle(self, *args, **options):
if not args:
return self.stderr.write('Missing required argument: email')
else:
try:
validate_email(args[0])
except ValidationError:
return self.stderr.write('%r is not a valid email address' % args[0])
else:
try:
user = User.objects.get(email__iexact=args[0])
except User.DoesNotExist:
pass
else:
return self.stderr.write('Email address %r is already taken by user account #%d' % (
user.email, user.id))

email = args[0]
password = None
alias = None

if options['password']:
while True:
s = getpass()
if s:
s2 = getpass('Repeat Password: ')
if s != s2:
self.stderr.write('Passwords do not match')
else:
password = s
break

# Generate alias
while True:
alias = User.objects.make_random_password(16)

try:
User.objects.get(alias=alias)
except User.DoesNotExist:
break

# Create the account
kwargs = {'is_active': True,
'alias': alias}

if options['admin']:
kwargs['is_admin'] = True

user = User.objects.create_user(email, password, **kwargs)
self.stdout.write('User account #%d created.' % user.id)
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
</ul>
</li>
<li><a href="{% url 'accounts:login' %}">{% trans "Login" %}</a></li>
<li><a href="{% url 'accounts:signup' %}" class="btn btn-primary">{% trans "Sign up" %}</a></li>
<li><a href="{% url 'accounts:signup' %}"><strong>{% trans "Sign up" %}</strong></a></li>
{% else %}
<li class="dropdown">
<button type="button" class="btn btn-link dropdown-toggle gravatar-28" data-toggle="dropdown" role="button" aria-expanded="false">
Expand Down

0 comments on commit c39381b

Please sign in to comment.