def add_user_profile(username, email, password, first_name, last_name, picture,
                     postcode, phone):
    '''Adds a new user profile with the given parameters.'''
    user = User.objects.create_user(username=username,
                                    email=email,
                                    password=password,
                                    first_name=first_name,
                                    last_name=last_name)
    user = RegistrationProfile.objects.create_inactive_user(site=None,
                                                            new_user=user,
                                                            send_email=False)
    RegistrationProfile.objects.activate_user(
        activation_key=RegistrationProfile.objects.get(
            user=user).activation_key,
        site=None)

    user_profile = UserProfile()
    user_profile.user = user
    user_profile.picture = picture

    user_profile.postcode = postcode
    user_profile.phone = phone
    user_profile.rating = 0

    user_profile.save()

    return user_profile
Exemple #2
0
    def register(self, form_class):
        """This function overrides the register function from RegistrationView."""
        user = super(MyRegistrationView, self).register(form_class)
        user.first_name = form_class.cleaned_data['first_name']
        user.last_name = form_class.cleaned_data['last_name']
        user = RegistrationProfile.objects.create_inactive_user(site=None,
                                                                new_user=user)

        # Create our user profile and fill the details.
        user_profile = UserProfile()
        user_profile.user = user

        if not form_class.cleaned_data['picture']:
            # Handle when the user does not upload a profile picture
            image = Image.open(
                path.dirname(path.dirname(path.abspath(__file__))) +
                static('images/profile.jpg'))

            if image.mode != "RGB":
                image = image.convert("RGB")

            output = BytesIO()
            image.save(output, 'JPEG', quality=100)
            output.seek(0)

            file_system = FileSystemStorage(settings.MEDIA_ROOT +
                                            '/profile_images/')
            filename = file_system.save('profile.jpg', output)
            file_url = file_system.url(filename)

            user_profile.picture = file_url.replace('/media', 'profile_images',
                                                    1)
        else:
            user_profile.picture = form_class.cleaned_data['picture']

        # Clean up the postcode
        input_postcode = form_class.cleaned_data['postcode'].replace(
            ' ', '').upper()
        user_profile.postcode = input_postcode[:-3] + ' ' + input_postcode[-3:]
        user_profile.phone = form_class.cleaned_data['phone']

        user_profile.save()
        return user_profile
    def register(self, form_class):
        # Override the register function from RegistrationView
        user = super(MyRegistrationView, self).register(form_class)
        user.first_name = form_class.cleaned_data['first_name']
        user.last_name = form_class.cleaned_data['last_name']
        user = RegistrationProfile.objects.create_inactive_user(site=None,
                                                                new_user=user)
        # Create our user profile and fill the details.
        user_profile = UserProfile()
        user_profile.user = user

        if form_class.cleaned_data['picture'] == None:
            user_profile.picture = 'profile_images/placeholder.jpg'
        else:
            user_profile.picture = form_class.cleaned_data['picture']

        input_postcode = form_class.cleaned_data['postcode'].replace(
            ' ', '').upper()
        user_profile.postcode = input_postcode[:-3] + ' ' + input_postcode[-3:]
        user_profile.phone = form_class.cleaned_data['phone']

        user_profile.save()
        return user_profile