Example #1
0
def edit(request, slug):
    profile = get_object_or_404(Profile, slug=slug)
    user = profile.user
    if request.user.pk != user.pk:
        return HttpResponseRedirect(reverse('users.views.edit', 
            args=[request.user.profile.slug]))
    if request.method == 'POST':
        form = UserEditForm(request.POST, instance=user)
        if form.is_valid():
            username = request.POST.get('username')
            password = request.POST.get('password1')
            if password:
                user.set_password(password)
                user.save()
            user = form.save()
            if username:
                profile.slug = slugify(username)
                profile.save()
            messages.success(request, 'User updated')
            return HttpResponseRedirect(reverse('root_path'))
    form = UserEditForm(instance=user)
    d = {
        'form' : form,
        'title': 'Edit Profile',
        'user' : user,
    }
    return render_to_response('users/edit.html', add_csrf(request, d), 
        context_instance=RequestContext(request))
Example #2
0
    def post(self, request, *args, **kwargs):
        user = get_object_or_404(User, id=self.request.user.id)
        member = get_object_or_404(Member, user=user)        

        user_edit_form = UserEditForm(data=request.POST, instance=user)
        password_edit_form = ChangePasswordForm(data=request.POST, user=user, initial={})
        member_edit_form = MemberDetailsForm(data=request.POST, instance=member)

        if user_edit_form.is_valid():
            user_edit_form.save()
            
        if password_edit_form.has_changed():
            if password_edit_form.is_valid():
                password_edit_form.save()
                return redirect(reverse('users:login'))
            
        if member_edit_form.is_valid():
            member_edit_form.save()
            
        context = self.get_context_data(**kwargs)
        
        context['form'] = user_edit_form
        context['form2'] = password_edit_form
        context['form3'] = member_edit_form
        
        return render_to_response(self.template_name, context, context_instance=RequestContext(request))
Example #3
0
    def test_photo_modified(self, update_mock):
        dummy = Mock()
        dummy.user = self.user

        data = {"username": self.user_profile.username, "email": self.user_profile.email}
        files = {"photo": get_uploaded_file("transparent.png")}
        form = UserEditForm(data, files=files, instance=self.user_profile, request=dummy)
        assert form.is_valid()
        form.save()
        assert update_mock.called
Example #4
0
def edit(request):
    user = get_object_or_404(User, id=request.user.id)
    form = UserEditForm(instance=user)
    if request.POST:
        if form.is_valid():
            form.save()
        return HttpResponseRedirect(reverse('users:profile'))
    context = {
        'form': form,
    }
    return render(request, 'users/edit.html', context)
Example #5
0
    def test_user_edit_form_valid_data(self):
        """Test user edit form with valid data"""
        form = UserEditForm(
            data={
                "username": faker.first_name(),
                "unconfirmed_email": faker.email(),
                "birthday": faker.date_time(),
                "first_name": faker.first_name(),
                "last_name": faker.last_name(),
            })

        self.assertTrue(form.is_valid())
Example #6
0
    def test_photo_modified(self, update_mock):
        dummy = Mock()
        dummy.user = self.user

        data = {'username': self.user_profile.username,
                'email': self.user_profile.email}
        files = {'photo': get_uploaded_file('transparent.png')}
        form = UserEditForm(data, files=files, instance=self.user_profile,
                            request=dummy)
        assert form.is_valid()
        form.save()
        assert update_mock.called
Example #7
0
    def test_photo_modified(self, update_mock):
        dummy = Mock()
        dummy.user = self.user

        data = {'username': self.user_profile.username,
                'email': self.user_profile.email,
                'lang': 'en-US'}
        files = {'photo': get_uploaded_file('transparent.png')}
        form = UserEditForm(data, files=files, instance=self.user_profile,
                            request=dummy)
        assert form.is_valid()
        form.save()
        assert update_mock.called
Example #8
0
def edit(request):
    context = RequestContext(request)

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserEditForm(data=request.POST, instance=request.user)
        profile_form = UserProfileForm(data=request.POST, instance=request.user.userprofile)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
 
            user = user_form.save(commit=False)

            profile = profile_form.save(commit=False)

            # Did the user provide a profile picture?
            # If so, we need to get it from the input form and put it in the UserProfile model.
            if 'avatar' in request.FILES:
                profile.avatar = request.FILES['avatar']

            # Now we save the UserProfile and User model instance.
            user.save()
            profile.save()
            
            # Update our variable to tell the template registration was successful.
            messages.success(request, _('Your changes have been saved.'))
            return HttpResponseRedirect(reverse('users:profile', args=(user.username,)))
 

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print user_form.errors, profile_form.errors

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = UserProfileForm(instance=request.user.userprofile)

    # Render the template depending on the context.
    return render_to_response( 'users/edit.html', {'user_form': user_form, 'profile_form': profile_form}, context)
Example #9
0
    def test_email_change_with_same_email(self):
        """Test email change checking with the same email"""
        result = is_user_email_changed(
            self.user.email,
            UserEditForm(data=self.get_data_for_user_edit_form()))

        self.assertFalse(result)
Example #10
0
    def test_email_change_with_different_email(self):
        """Test email change checking with different email"""
        different_email = faker.email()

        result = is_user_email_changed(
            different_email,
            UserEditForm(data=self.get_data_for_user_edit_form()))

        self.assertTrue(result)
Example #11
0
def edit_profile(request):
    user_form = UserEditForm(request.POST or None, instance=request.user)
    profile_form = ProfileEditForm(
        request.POST or None,
        files=request.FILES or None,
        instance=request.user.profile)
    if user_form.is_valid() and profile_form.is_valid():
        user_form.save()
        profile_form.save()
        # автоматический вход после редактирования профиля
        user = authenticate(
            username=user_form.cleaned_data['username'],
            password=user_form.cleaned_data['password1'])
        login(request, user)
        return HttpResponseRedirect(reverse('edit_profile'))
    return render(
        request,
        'account/profile_edit.html',
        {'user_form': user_form, 'profile_form': profile_form})
Example #12
0
def base_profile(request):
    f_password = forms.PasswordChangeForm(request.POST)
    f_profile = ProfileForm(instance = request.user.profile)
    f_user = UserEditForm(instance = request.user)
    commit = False
    if request.method == 'POST':
        f_profile = ProfileForm(request.POST,request.FILES,instance = request.user.profile)
        f_user = UserEditForm(request.POST,request.FILES,instance = request.user)
        if f_password.is_valid():
            f_password.save(commit=True)
            #handle_uploaded_file(request.FILES['avatar'])
            f_profile.save(commit=True)
            commit=True
        if f_profile.is_valid():
            f_profile.save(commit=True)
            #handle_uploaded_file(request.FILES['avatar'])
            commit=True
        if f_user.is_valid():
            user = f_user.save(commit=True)
            commit=True
    return render_to_response('user/profile.html',{'profile':f_profile,'f_pw':f_password,'f_user':f_user,'commit':commit},context_instance=RequestContext(request))
Example #13
0
def update_profile(request):
    my_form = UpdateProfile(request.POST or None, instance=request.user)

    if my_form.is_valid():
        user = my_form.save()
        customUser = UserProfileInfo.objects.get(user_id=user.id)
        image = customUser.profilePic
        customUser.delete()
        profile_form = UserProfileInfoForm(data=request.POST)
        edit_form = UserEditForm(data=request.POST)
        profile = profile_form.save(commit=False)
        profile.user_id = user.id
        profile.profilePic = image
        edit = edit_form.save(commit=False)
        edit.user = user
        user.save()
        profile.save()
        edit.save()
        return HttpResponse("your data updated succesfully")
    context = {'form': my_form}
    return render(request, 'users/update_profile.html', context)
Example #14
0
def edit_profile(request):
    if request.method == 'POST':
        form = UserEditForm(data=request.POST,
                            files=request.FILES,
                            instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('profile')
    else:
        form = UserEditForm(instance=request.user)
        context = {"form": form, "title": "Edit Profile"}
        return render(request, 'users/edit_profile.html', context=context)
Example #15
0
    def test_user_edit_view_with_changes(self):
        """Test user edit view with changed fields"""
        self.login_user()

        data_for_form = self.get_data_for_user_edit_form()
        new_first_name = faker.first_name()
        data_for_form["first_name"] = new_first_name
        form = UserEditForm(data=data_for_form)

        url = reverse("profile-edit", args=(self.user.id, ))
        response = self.client.post(url, form=form)

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed("users/user_update_form.html")
Example #16
0
def profile():
    """Update profile for current user."""
    if not g.user:
        flash("Access unauthorized.", "danger")
        return redirect("/")

    form = UserEditForm(obj=g.user)
    # form is valid?
    if form.validate_on_submit():
        # form has valid Pword?
        if User.authenticate(username=g.user.username, password=form.password.data):

            g.user.update_from_serial(request.form)
            db.session.add(g.user)
            try:
                db.session.commit()
                return redirect(url_for("user_routes.users_show", user_id=g.user.id))

            except IntegrityError:
                flash("Username or email already taken", 'danger')
                return render_template('/users/edit.html', form=form)
        else:
            form.password.errors.append("Password is incorrect!")
    return render_template('/users/edit.html', form=form)
Example #17
0
def get_dashboard_profile_edit(request):
    if request.method == "POST":
        user_form = UserEditForm(request.POST, instance=request.user)
        profile_form = ProfileEditForm(request.POST,
                                       request.FILES,
                                       instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            return redirect("profile")

    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user.profile)

    context = {"u_form": user_form, "p_form": profile_form}

    return render(request, "dashboard/profile_edit.html", context)
Example #18
0
def edit_account(request):
    # Edit your profile
    categories = search_categories()
    if request.method == "GET":
        instance = get_object_or_404(User, id=request.user.id)
        form = UserEditForm(instance=instance)
        return render(request, 'courses/edit_account.html', {
            'form': form,
            'categories': categories
        })

    # Method POST
    else:
        instance = get_object_or_404(User, id=request.user.id)
        form = UserEditForm(request.POST or None,
                            request.FILES or None,
                            instance=instance)

        if form.is_valid():
            form.save()

            # Get the new profile_picture (if any)
            profile_picture = request.FILES.get('picture_profile')
            user_picture = UserProfile.objects.filter(user=request.user)

            # If the user already has a profile picture one has been added
            if user_picture and profile_picture:
                user_picture = user_picture[0]
                user_picture.avatar = profile_picture
                user_picture.save()

            # If the user does not have a profile photo and a
            elif profile_picture:
                user_picture = UserProfile(user=request.user,
                                           avatar=profile_picture)
                user_picture.save()

            return redirect(reverse('myaccount'))

        return render(request, 'courses/edit_account.html', {
            'form': form,
            'categories': categories
        })
Example #19
0
def edit_profile_view(request):
    if request.method == 'POST':
        user_form = UserEditForm(data=request.POST or None,
                                 instance=request.user)
        profile_form = ProfileEditForm(data=request.POST or None,
                                       instance=request.user.profile,
                                       files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user.profile)

    context = {
        'user_form': user_form,
        'profile_form': profile_form,
    }
    return render(request, 'users/users_form.html', context)
Example #20
0
 def post(self, request, *args, **kwargs):
     user_form = UserEditForm(instance=request.user, data=request.POST)
     profile_form = ProfileForm(instance=request.user.profile, data=request.POST, files=request.FILES)
     if request.POST['old_password'] and not check_password(request.POST['old_password'], request.user.password):
         messages.error(request, 'Password is wrong')
     elif user_form.is_valid() and profile_form.is_valid():
         cd = user_form.cleaned_data
         request.user.set_password(cd['new_password'])
         user_form.save()
         profile_form.save()
         messages.success(request, 'Profile updated successfully')
     else:
         print(user_form.is_valid())
         print(profile_form.is_valid())
         messages.error(request, 'Error updating your profile')
     return render(request, 'users/edit.html', {'user_form': user_form, 'profile_form': profile_form})
Example #21
0
def edit_view(request):
    if request.method == 'POST':
        user_form = UserEditForm(instance=request.user, data=request.POST)
        profile_form = ProfileEditForm(instance=request.user.profile, data=request.POST, files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            change_user = user_form.save(commit=False)
            change_user.save()
            profile = Profile.objects.get(user=change_user)
            avatar = request.FILES.get('photo')
            if avatar:
                profile.photo = avatar
            profile_form.save()
            return redirect('users:account')
        return request(request, 'edit.html', {'user_form': user_form, 'profile_form': profile_form})
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user.profile)
        return render(request, 'edit.html', {'user_form': user_form,
                                             'profile_form': profile_form})
Example #22
0
def edit_user(request, pk=None):
    instance = None
    if pk:
        try:
            instance = User.objects.get(pk=pk)
        except:
            return redirect(reverse('users:students'))
    if request.method == 'POST':
        form = UserEditForm(data=request.POST,
                            instance=instance,
                            request_user=request.user)
        if form.is_valid():
            form.save()
            if request.user.is_anonymous() or request.user.is_student:
                return redirect('users:login')
            return redirect(reverse('users:students'))
        else:
            pass
    else:
        form = UserEditForm(instance=instance, request_user=request.user)
    return render(request, 'users/edit_student.html', {
        'form': form,
    })
Example #23
0
def profile_edit(request, username):
    userprofile = get_object_or_404(User, username=username)
    if request.method == 'POST':
        user_form = UserEditForm(instance=request.user, data=request.POST)
        profile_form = UserProfileEditForm(instance=request.user.userprofile,
                                           data=request.POST,
                                           files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request,
                             _('The update of the profile was successful'))
        else:
            messages.error(request,
                           _('An error occurred while updating the profile'))
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = UserProfileEditForm(instance=request.user.userprofile)
    return render(
        request, 'user_account/profile_edit.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'userprofile': userprofile
        })
Example #24
0
    def test_user_edit_form_no_data(self):
        """Test user edit form with no data"""
        form = UserEditForm(data={})

        self.assertFalse(form.is_valid())
Example #25
0
 def get(self, request, *args, **kwargs):
     user_form = UserEditForm(instance=request.user)
     profile_form = ProfileForm(instance=request.user.profile)
     return render(request, 'users/edit.html', {'user_form': user_form, 'profile_form': profile_form})