Beispiel #1
0
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)
Beispiel #2
0
    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
Beispiel #3
0
    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