Skip to content

Instantly share code, notes, and snippets.

@hanleybrand
Created December 4, 2018 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanleybrand/25184b46adf6c6d20a597e833287728a to your computer and use it in GitHub Desktop.
Save hanleybrand/25184b46adf6c6d20a597e833287728a to your computer and use it in GitHub Desktop.
create users for mdid3 from a json source - management command, only a start
import requests
import simplejson as json
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from optparse import make_option
from django.core.validators import email_re
from django.db import IntegrityError
from django.conf import settings
import logging
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--file', '-f', action='store_true', dest='file', help='Load json from a file instead'),
)
help = 'Add user accounts based on json from a user database\n' \
'Available commands: serverurl \n' \
'ex: python manage.py add_users_json [crn] \n ' \
'where [crn] is a course reference number like 12345'
args = 'crn'
def handle(self, *args, **options):
return process_users(args)
def process_users(self, crn, options=None):
file_json = options.get('file', None)
if file_json:
try:
jfile = open(crn, 'r')
except Exception, error_message:
logging.debug('add_users_json load from file error: %s' % error_message)
else:
try:
parms = {'crn', crn}
userlist = requests.get(settings.JSON_URL, params=parms)
logging.debug('add_users_json: adding users from %s' % userlist.url)
except Exception, error_message:
logging.debug('add_users_json error: %s' % error_message)
def create_accounts(username, email, first_name, last_name):
try:
user = User.objects.create_user(username, email)
user.is_staff = False
user.first_name = first_name
user.last_name = last_name
user.set_unusable_password()
user.save()
except IntegrityError:
print username
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment