def get_or_create_user(deki_user, sync_attrs=('is_superuser', 'is_staff', 'is_active', 'email')): """ Grab the User via their UserProfile and deki_user_id. If non exists, create both. NOTE: Changes to this method may require changes to parse_user_info """ try: # Try fetching an existing profile mapped to deki user profile = UserProfile.objects.get(deki_user_id=deki_user.id) user = profile.user except ObjectDoesNotExist: # No existing profile, so try creating a new profile and user # HACK: Usernames in Kuma are limited to 30 characters. There are # around 0.1% of MindTouch users in production (circa 2011) whose # names exceed this length. They're mostly the product of spammers # and security tests but it will still throw MySQL-level errors # during migration. username = deki_user.username[:30] user, created = (User.objects .get_or_create(username=username)) user.username = username user.email = deki_user.email user.set_unusable_password() user.save() profile = UserProfile(deki_user_id=deki_user.id, user=user) profile.save(skip_mindtouch_put=True) user.deki_user = deki_user # Sync these attributes from Deki -> Django (for now) needs_save = False for sa in sync_attrs: deki_val = getattr(deki_user, sa, None) if getattr(user, sa, None) != deki_val: setattr(user, sa, deki_val) needs_save = True if needs_save: user.save() return user
def get_or_create_user(deki_user, sync_attrs=('is_superuser', 'is_staff', 'is_active', 'email')): """ Grab the User via their UserProfile and deki_user_id. If non exists, create both. NOTE: Changes to this method may require changes to parse_user_info """ try: # Try fetching an existing profile mapped to deki user profile = UserProfile.objects.filter(deki_user_id=deki_user.id)[0] user = profile.user except (IndexError, ObjectDoesNotExist): # No existing profile, so try creating a new profile and user # HACK: Usernames in Kuma are limited to 30 characters. There are # around 0.1% of MindTouch users in production (circa 2011) whose # names exceed this length. They're mostly the product of spammers # and security tests but it will still throw MySQL-level errors # during migration. username = deki_user.username[:30] user, created = (User.objects.get_or_create(username=username)) user.username = username user.email = deki_user.email user.set_unusable_password() user.save() profile = UserProfile(deki_user_id=deki_user.id, user=user) profile.save(skip_mindtouch_put=True) user.deki_user = deki_user # Sync these attributes from Deki -> Django (for now) needs_save = False for sa in sync_attrs: deki_val = getattr(deki_user, sa, None) if getattr(user, sa, None) != deki_val: setattr(user, sa, deki_val) needs_save = True if needs_save: user.save() return user
def _create_profile(self): """Create a user, deki_user, and a profile for a test account""" user = User.objects.create_user('tester23', '*****@*****.**', 'trustno1') deki_user = DekiUser(id=0, username='******', fullname='Tester Twentythree', email='*****@*****.**', gravatar='', profile_url=None) profile = UserProfile() profile.user = user profile.fullname = "Tester Twentythree" profile.title = "Spaceship Pilot" profile.organization = "UFO" profile.location = "Outer Space" profile.irc_nickname = "ircuser" profile.bio = "I am a freaky space alien." profile.save() return (user, deki_user, profile)
def get_or_create_user(self, deki_user): """ Grab the User via their UserProfile and deki_user_id. If non exists, create both. NOTE: Changes to this method may require changes to parse_user_info """ try: profile = UserProfile.objects.get(deki_user_id=deki_user.id) user = profile.user log.info("MONITOR Dekiwiki Profile Loaded") log.debug("User account already exists %d", user.id) except UserProfile.DoesNotExist: log.debug( "First time seeing deki user id#%d username=%s, creating account locally", deki_user.id, deki_user.username, ) user, created = User.objects.get_or_create(username=deki_user.username) user.username = deki_user.username # HACK: Deki has fullname Django has First and last... # WACK: but our Dekiwiki instace doesn't let the user edit this data user.first_name = deki_user.fullname user.last_name = "" user.set_unusable_password() user.save() profile = UserProfile(deki_user_id=deki_user.id, user=user) profile.save() log.info("MONITOR Dekiwiki Profile Saved") log.debug("Saved profile %s", str(profile)) user.deki_user = deki_user # Items we don't store in our DB (API read keeps it fresh) user.is_superuser = deki_user.is_superuser user.is_staff = deki_user.is_staff user.is_active = deki_user.is_active return user
def get_or_create_user(self, deki_user): """ Grab the User via their UserProfile and deki_user_id. If non exists, create both. NOTE: Changes to this method may require changes to parse_user_info """ try: # Try fetching an existing profile mapped to deki user profile = UserProfile.objects.get(deki_user_id=deki_user.id) user = profile.user except UserProfile.DoesNotExist: # No existing profile, so try creating a new profile and user user, created = (User.objects.get_or_create( username=deki_user.username)) user.username = deki_user.username user.set_unusable_password() user.save() profile = UserProfile(deki_user_id=deki_user.id, user=user) profile.save() user.deki_user = deki_user # Sync these attributes from Deki -> Django (for now) sync_attrs = ('is_superuser', 'is_staff', 'is_active', 'email') needs_save = False for sa in sync_attrs: deki_val = getattr(deki_user, sa, None) if getattr(user, sa, None) != deki_val: setattr(user, sa, deki_val) needs_save = True if needs_save: user.save() return user
def get_or_create_user(self, deki_user): """ Grab the User via their UserProfile and deki_user_id. If non exists, create both. NOTE: Changes to this method may require changes to parse_user_info """ try: # Try fetching an existing profile mapped to deki user profile = UserProfile.objects.get(deki_user_id=deki_user.id) user = profile.user except UserProfile.DoesNotExist: # No existing profile, so try creating a new profile and user user, created = (User.objects .get_or_create(username=deki_user.username)) user.username = deki_user.username user.set_unusable_password() user.save() profile = UserProfile(deki_user_id=deki_user.id, user=user) profile.save() user.deki_user = deki_user # Sync these attributes from Deki -> Django (for now) sync_attrs = ('is_superuser', 'is_staff', 'is_active', 'email') needs_save = False for sa in sync_attrs: deki_val = getattr(deki_user, sa, None) if getattr(user, sa, None) != deki_val: setattr(user, sa, deki_val) needs_save = True if needs_save: user.save() return user
def create_profile(): """Create a user, deki_user, and a profile for a test account""" user = User.objects.create_user('tester23', '*****@*****.**', 'trustno1') deki_user = DekiUser(id=0, username='******', fullname='Tester Twentythree', email='*****@*****.**', gravatar='', profile_url=None) profile = UserProfile() profile.user = user profile.fullname = "Tester Twentythree" profile.title = "Spaceship Pilot" profile.organization = "UFO" profile.location = "Outer Space" profile.bio = "I am a freaky space alien." profile.irc_nickname = "ircuser" profile.locale = 'en-US' profile.timezone = 'US/Central' profile.save() return (user, deki_user, profile)
def create_profile(): """Create a user and a profile for a test account""" user = User.objects.create_user('tester23', '*****@*****.**', 'trustno1') profile = UserProfile() profile.user = user profile.fullname = "Tester Twentythree" profile.title = "Spaceship Pilot" profile.organization = "UFO" profile.location = "Outer Space" profile.bio = "I am a freaky space alien." profile.irc_nickname = "ircuser" profile.locale = 'en-US' profile.timezone = 'US/Central' profile.save() return (user, profile)
def create_profile(): """Create a user, deki_user, and a profile for a test account""" user = User.objects.create_user("tester23", "*****@*****.**", "trustno1") deki_user = DekiUser( id=0, username="******", fullname="Tester Twentythree", email="*****@*****.**", gravatar="", profile_url=None, ) profile = UserProfile() profile.user = user profile.fullname = "Tester Twentythree" profile.title = "Spaceship Pilot" profile.organization = "UFO" profile.location = "Outer Space" profile.bio = "I am a freaky space alien." profile.irc_nickname = "ircuser" profile.locale = "en-US" profile.timezone = "US/Central" profile.save() return (user, deki_user, profile)