Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 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 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:
jfile = open(crn, 'r')
except Exception, error_message:
logging.debug('add_users_json load from file error: %s' % error_message)
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):
user = User.objects.create_user(username, email)
user.is_staff = False
user.first_name = first_name
user.last_name = last_name
except IntegrityError:
print username
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment