Ejemplo n.º 1
0
def register(request):
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        if user_form.is_valid():
            user = User()
            user.email = user_form.cleaned_data['email']
            user.username = user.email
            user.set_password(user_form.cleaned_data['password'])
            user.save()
            user_profile = UserProfile()
            user_profile.user = user
            user_profile.website = user_form.cleaned_data['website']
            if 'picture' in request.FILES:
                user_profile.picture = request.FILES['picture']
            user_profile.save()
            login(request, user)
            send_confirmation_email(request, user)
            messages.success(request,'Registration successfully happened. Now confirm your email.')
            return redirect('index')
        else:
            print(user_form.errors)
            messages.error(request,'Check out your data, invalid form!.')
    else:
        user_form = UserForm()
    context_dict ={ 'user_form': user_form,}
    return render(request,'auth/register.html', context_dict)
    def test_create_user_using_view(self):
        """  test user creation functions in views.py (no form used)"""
        # delete user if it exists
        try:
            userKK = User.objects.get(username=username)
            userKK.delete()
        except User.DoesNotExist:
            pass
        # create user self.username with password self.passwd
        # fill user form
        loginDict = {}
        loginDict["username"] = username
        loginDict["email"] = email
        loginDict["password"] = passwd
        userForm = UserForm(data=loginDict)

        # fill profile form
        profileDict = {}
        profileDict['website'] = website
        # create auxiliary black image for profileForm
        filesDict = {'picture': createPicture()}
        userProfileForm = UserProfileForm(data=profileDict, files=filesDict)

        if userForm.is_valid() and userProfileForm.is_valid():
            try:
                # Save the user's form data to the database.
                user = userForm.save()
                # Now we hash the password with the set_password method.
                # Once hashed, we can update the user object.
                user.set_password(user.password)
                user.save()
                # we need to set a user object in userprofile
                userprofile = userProfileForm.save(commit=False)
                userprofile.user = user
                userprofile.save()

            except Exception as e:
                raise Exception("Error processing form: %s" % e.message)

        self.assertTrue(userForm.is_valid())
        self.assertTrue(userProfileForm.is_valid())

        # check user name
        try:
            userKK = User.objects.get(username=username)
        except User.DoesNotExist:
            userKK = User()
        self.assertEqual(userKK.email, email)

        # check file name
        try:
            userprofileKK = UserProfile.objects.get(user=userKK)
        except User.DoesNotExist:
            userprofileKK = UserProfile()
        imageName = join(settings.MEDIA_ROOT, userprofileKK.picture.name)
        with Image.open(imageName) as im:
            width, height = im.size
        self.assertEqual(width, 200)
Ejemplo n.º 3
0
def add_user(user):
    try:
        u = User.objects.get(username=user['email'])
    except User.DoesNotExist:
        u = User()
    u.email = user['email']
    u.username = user['email']
    u.set_password(user['password'])
    u.save()
    try:
        user_profile = UserProfile.objects.get(user__username=user['email'])
    except UserProfile.DoesNotExist:
        user_profile = UserProfile()
    user_profile.user = u
    user_profile.website = user['website']
    user_profile.save()
Ejemplo n.º 4
0
def register_profile(request):
    try:
        user_profile = request.user.userprofile
    except UserProfile.DoesNotExist:
        user_profile = UserProfile(user=request.user)

    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=user_profile)
        print "request.POST: %s \n" % request.POST
        if form.is_valid():
            form.save()
            return redirect('/rango/profile')
    else:
        form = UserProfileForm(instance=user_profile)
        context_dict = {'form': form}
        return render(request, 'rango/profile_registration.html', context_dict)
Ejemplo n.º 5
0
    def register(self, request, **cleaned_data):
        # Create a new User
        username, email, password = cleaned_data['username'], cleaned_data[
            'email'], cleaned_data['password1']
        new_user_object = UserModel().objects.create_user(
            username, email, password)

        # And links that user to a new (empty) UserProfile
        profile = UserProfile(user=new_user_object)
        profile.save()

        new_user = authenticate(username=username, password=password)
        login(request, new_user)
        signals.user_registered.send(sender=self.__class__,
                                     user=new_user,
                                     request=request)
        return new_user
Ejemplo n.º 6
0
def view_profile(request):
    user_profile = UserProfile(user=request.user)

    return render(request, 'rango/profile.html',
                  {'user_profile': user_profile})