Пример #1
0
def edit():

    form = EditProfileForm()

    if request.method == 'POST':

        if form.validate_on_submit():
            user = User.objects.get(id=current_user.id)
            user.name = form.name.data.strip()
            user.email = form.email.data.strip()
            user.website = form.website.data.strip()
            user.twitter_username = form.twitter.data.strip('@')
            facebook = form.facebook.data.strip().strip('/').split('/')[-1]
            user.facebook_username = facebook
            user.location = form.location.data.strip()
            user.hireable = bool(form.hireable.data)
            user.bio = form.bio.data.strip()
            user.save()
            flash(_('Profile updated successfully'), category='success')
            return redirect(url_for('users.index'))
        else:
            flash(_('Error happened, see below'), category='alert')
            return render_template('users/edit_profile.html', form=form)
    else:
        form.hireable.default = int(bool(current_user.hireable))
        form.bio.default = current_user.bio or ''
        form.process()

    return render_template('users/edit_profile.html', form=form)
Пример #2
0
def edit_profile(request):
    user_obj = request.user
    try:
        profile_obj = get_object_or_404(Profile, user=user_obj)
    except:
        profile_obj = None

    if request.method == 'POST':
        form = EditProfileForm(request.POST)
        if form.is_valid():
            profile_obj.city = form.cleaned_data['city'].title()
            profile_obj.country = form.cleaned_data['country'].title()
            profile_obj.description = form.cleaned_data['description']
            profile_obj.save()
            user_obj.first_name = form.cleaned_data['first_name'].title()
            user_obj.last_name = form.cleaned_data['last_name'].title()
            user_obj.save()
            messages.info(request, 'Your profile information is saved')
    else:
        form = EditProfileForm(initial={
            'first_name': user_obj.first_name,
            'last_name': user_obj.last_name,
            'username': user_obj.username,
            'sex': profile_obj.sex,
            'dob': profile_obj.dob.strftime('%d %b, %Y - %A'),
            'city': profile_obj.city,
            'country': profile_obj.country,
            'description': profile_obj.description,
        })

    return render(request, 'users/edit_profile.html', {
        'form': form,
    })
Пример #3
0
def edit_profile(request, form=None):
    """
        Used to give Dajax(ice) the edit profile page
        Renders in : modal
        Refreshes : right_content
    """
    dajax = Dajax()
    
    errors = False
    userprofile = request.user.get_profile()
    fullname = userprofile.user.get_full_name()
    nickname = userprofile.nickname
    if request.method == 'POST' and form != None:
        form = EditProfileForm(deserialize_form(form), instance=userprofile)
        
        if form.is_valid():
            
            form.save()
            dajax.assign("#edit_profile_nickname", "innerHTML", edit_form.cleaned_data['nickname'])
            dajax.remove_css_class('#profile_edit_form input', 'error')
            dajax.script('modal_hide()') # Hide modal
            show_alert(dajax, 'success', 'Profile was edited and saved')
        else:
            errors = True
            dajax.remove_css_class('#profile_edit_form input', 'error')
            for error in form.errors:
                dajax.add_css_class('#id_%s' % error, 'error')
            #show_alert(dajax, 'error', "There were errors in the form") # as it is in modal, not req
    else:
        form = EditProfileForm ( instance = userprofile )
        html_content = render_to_string("users/edit_profile.html", locals(), RequestContext(request))
        #dajax.remove_css_class('#id_modal', 'hide') # Show modal (already done in do_Dajax)
        dajax.assign("#id_modal", "innerHTML", html_content) # Populate modal
    
    return dajax.json()
Пример #4
0
def edit_profile(request):
    user_obj = request.user
    try:
        profile_obj = get_object_or_404(Profile, user=user_obj)
    except:
        profile_obj = None

    if request.method == 'POST':
        form = EditProfileForm(request.POST)
        if form.is_valid():
            profile_obj.city = form.cleaned_data['city'].title()
            profile_obj.country = form.cleaned_data['country'].title()
            profile_obj.description = form.cleaned_data['description']
            profile_obj.save()
            user_obj.first_name = form.cleaned_data['first_name'].title()
            user_obj.last_name = form.cleaned_data['last_name'].title()
            user_obj.save()
            messages.info(request, 'Your profile information is saved')
    else:
        form = EditProfileForm(
            initial={
                'first_name': user_obj.first_name,
                'last_name': user_obj.last_name,
                'username': user_obj.username,
                'sex': profile_obj.sex,
                'dob': profile_obj.dob.strftime('%d %b, %Y - %A'),
                'city': profile_obj.city,
                'country': profile_obj.country,
                'description': profile_obj.description,
            })

    return render(request, 'users/edit_profile.html', {
        'form': form,
    })
Пример #5
0
def edit():

    form = EditProfileForm()

    if request.method == 'POST':

        if form.validate_on_submit():
            user = User.objects.get(id=current_user.id)
            user.name = form.name.data.strip()
            user.email = form.email.data.strip()
            user.website = form.website.data.strip()
            user.twitter_username = form.twitter.data.strip('@')
            facebook = form.facebook.data.strip().strip('/').split('/')[-1]
            user.facebook_username = facebook
            user.location = form.location.data.strip()
            user.hireable = bool(form.hireable.data)
            user.bio = form.bio.data.strip()
            user.save()
            flash(_('Profile updated successfully'), category='success')
            return redirect(url_for('users.index'))
        else:
            flash(_('Error happened, see below'), category='alert')
            return render_template('users/edit_profile.html', form=form)
    else:
        form.hireable.default = int(bool(current_user.hireable))
        form.bio.default = current_user.bio or ''
        form.process()

    return render_template('users/edit_profile.html', form=form)
Пример #6
0
def edit_profile(request):
    title = 'Editer le profile'

    if request.method == "POST":
        # request.user.username is the original username
        form = EditProfileForm(request.user.username, request.POST)
        if form.is_valid():
            username = form.cleaned_data["username"]
            about_me = form.cleaned_data["about_me"]
            user = get_object_or_404(User, id=request.user.id)

            # We use a transaction so that if one of the requests below fails all previous ones are canceled
            with transaction.atomic():
                edit = EditProfile.objects.filter(user=user)
                if edit.exists():
                    edit.update(about_me=about_me, user=user)
                    user.username = username
                    user.save()
                else:
                    edit = EditProfile(about_me=about_me, user=user)
                    user.username = username
                    edit.save()
                    user.save()
                messages.add_message(request, messages.SUCCESS,
                                        'Vos changements ont été enregistrés.')
                return redirect(reverse('users:profile'))
    else:
        edit = EditProfile.objects.filter(user=request.user)
        about_me = edit[0].about_me if edit.exists() else 'Décrivez vous en quelques mots'

        form = EditProfileForm(request.user.username,
                               {'username': request.user.username,
                                'about_me': about_me})
    return render(request, 'users/edit_profile.html', {'form': form ,'title': title})
Пример #7
0
def user_profile_edit(request):
    if request.method == 'POST':
        form = EditProfileForm(request.user, request.POST, request.FILES)
        if form.is_valid():
            data = form.cleaned_data
            user = request.user
            profile = user.get_profile()
            if data['avatar']:
                profile.avatar = data['avatar']
            profile.city = data['city']
            profile.show_signatures = data['show_signatures']
            profile.main_char = data['main_char']
            profile.country = data['country']
            profile.birthday = data['birthday']
            profile.signature = data['signature']
            profile.save()
            user.first_name = data['first_name']
            user.last_name = data['last_name']
            user.save()
            return HttpResponseRedirect("/accounts/profile/")
    else:
        form = EditProfileForm(request.user, instance=request.user.get_profile())
    return render_to_response(
        "account/profile_edit.html",
        {
            'form': form,
        },
        context_instance=RequestContext(request)
    )
Пример #8
0
 def post(self, request):
     user_form = EditUserForm(request.POST, instance=request.user)
     profile_form = EditProfileForm(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('users:view_profile')
     return HttpResponse("Something went wrong")
Пример #9
0
    def test_username_already_taken(self):
        self.jacob = User.objects.create_user(username='******',
                                              email='*****@*****.**',
                                              password='******')

        form = EditProfileForm(data={
            'username': '******',
            'about_me': 'somthing about me'
        },
                               original_username='******')
        self.assertFalse(form.is_valid())
Пример #10
0
 def test_username_already_taken(self):
     form = EditProfileForm(
         data={
             'username': '******',
         },
         original_username='******'
     )
     self.assertFalse(form.is_valid())
     # Check that the user got the message 'This username is already taken'
     for field in form:
         for error in field.errors:
             self.assertEqual(error, 'This username is already taken.')
Пример #11
0
def edit_profile(request):
    title = 'Edit profile'

    if request.method == "POST":
        # request.user.username is the original username
        form = EditProfileForm(request.user.username, request.POST,
                               request.FILES)
        if form.is_valid():
            username = form.cleaned_data["username"]
            phone_number = form.cleaned_data["phone_number"]
            location = form.cleaned_data["location"]
            profile_image = form.cleaned_data["profile_image"]
            user = get_object_or_404(User, id=request.user.id)

            # We use a transaction so that if one of the requests below fails all previous ones are canceled
            try:
                with transaction.atomic():
                    edit = Profile.objects.filter(user=user)
                    if edit.exists():
                        edit = Profile.objects.get(user=user)
                        edit.phone_number = phone_number
                        edit.location = location
                        if profile_image:
                            edit.profile_image = request.FILES['profile_image']
                        edit.save()
                        user.username = username
                        user.save()
                    else:
                        edit = Profile(user=user)
                        edit.phone_number = phone_number
                        edit.location = location
                        edit.profile_image = profile_image
                        user.username = username
                        edit.save()
                        user.save()
                    messages.add_message(request, messages.SUCCESS,
                                         'Your changes have been saved.')
                    return redirect('users:profile', username=user.username)
            except IntegrityError:
                form.errors[
                    'internal'] = "An internal error has occurred. Please try your request again."
    else:
        edit = Profile.objects.filter(user=request.user)
        if edit.exists():
            form = EditProfileForm(
                request.user.username, {
                    'username': request.user.username,
                    'phone_number': edit[0].phone_number,
                    'location': edit[0].location
                }, request.FILES)
        else:
            form = EditProfileForm(request.user.username,
                                   {'username': request.user.username},
                                   request.FILES)

    return render(request, 'users/edit_profile.html', {
        'title': title,
        'form': form
    })
Пример #12
0
    def post(self, request, *args, **kwargs):
        form = EditProfileForm(request.POST, request.FILES)
        if form.is_valid():
            user            = request.user
            user.first_name = form.cleaned_data['first_name']                                                           
            user.last_name  = form.cleaned_data['last_name']                                                                      
            user.region     = form.cleaned_data['region']
            user.about      = form.cleaned_data['about']

            if 'avatar' in self.request.FILES:
                save_avatar(user, request)
            user.save()
            
            return redirect('users:account', username = user.username)
        else:
            return render(request, self.template_name, {'form': form})
Пример #13
0
def edit_user_profile(request, user_id=0):
    # if own profile, allows edit profile option
    if user_id==0:
        user = User.objects.get(pk=request.user.id)
        profile = user.userprofile

        # brings up EditPrfileForm option and saves data
        if request.method == 'POST':
            form = EditProfileForm(data=request.POST, instance=request.user, profile=profile)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('/users/profile')
        else:
            form = EditProfileForm(profile=profile)

        return render(request, 'users/edit_profile.html', {'form': form})
Пример #14
0
def EditProfileView(request):
    pass
    template = 'user/EditProfile2.html'
    self = request.user
    profile = UserProfile.objects.get(user=self)
    img = None

    if request.method == "POST":
         pf = EditProfileForm(request.POST, request.FILES, instance=profile)
         if pf.is_valid():
             pf.save()
             # resize and save image under same filename
             imfn = join(settings.MEDIA_ROOT, profile.avatar.name)
             im = Image.open(imfn)
             im.thumbnail((160, 160), Image.ANTIALIAS)
             im.save(imfn, "JPEG")
    else:
        pf = EditProfileForm(instance=profile)
        if profile.avatar:
            img = "/media/" + profile.avatar.name

    return render_to_response(template, add_csrf(request, pf=pf, img=img))
Пример #15
0
def edit_profile(request, id):
    # Checks if the current logged in user id doesn't match the requested id
    # If the current user id and the request id dont match the current user gets logged out and redirected to log in again
    if request.user.id != id:
        logout(request)
        return redirect('login')

    user = User.objects.get(id=id)
    if request.method == "GET":
        context = {
            'user': user,
            'form': EditProfileForm()
        }

        return render(request, 'profile/edit-profile.html', context)
    form = EditProfileForm(request.POST, request.FILES)
    if form.is_valid():
        print(form.errors)

        # checks if the profile didnt have an old picture
        if user.userprofile.profile_picture:
            old_pic = user.userprofile.profile_picture

            clean_up(old_pic.path)

        # in case a field is empty it doesnt leave the field in the db empty
        if form.cleaned_data['profile_picture']:
            user.userprofile.profile_picture = form.cleaned_data['profile_picture']

        if form.cleaned_data['username']:
            user.username = form.cleaned_data['username']

        if form.cleaned_data['email']:
            user.email = form.cleaned_data['email']

        user.save()
        user.userprofile.save()

        return redirect('profile page', id)
Пример #16
0
def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)

        if form.is_valid():
            form.save()
            return redirect(
                '.')  # back to the current page: 8000/users/profile
    else:
        form = EditProfileForm(instance=request.user)
        args = {'form': form}
        return render(request, 'users/edit_profile.html', args)
Пример #17
0
def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST,
                               request.FILES,
                               instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('users:profile')
    else:
        form = EditProfileForm(instance=request.user)
        args = {'form': form}
        return render(request, 'registration/edit_profile.html', args)
Пример #18
0
 def test_fields_label(self):
     form = EditProfileForm(original_username='******')
     self.assertTrue(
         form.fields['username'].label is None or form.fields['username'].label == 'Username*'
     )
     self.assertTrue(
         form.fields['phone_number'].label is None or form.fields['phone_number'].label == 'Phone'
     )
     self.assertTrue(
         form.fields['location'].label is None or form.fields['location'].label == 'Location'
     )
     self.assertTrue(
         form.fields['profile_image'].label is None or form.fields['profile_image'].label == 'Image'
     )
Пример #19
0
def editProfile(request, userID):
    user = request.user
    if user.id != userID:
        return HttpResponse('Unauthorized', status=401)
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('frontpage')
    else:
        form = EditProfileForm(instance=request.user)
        context = {'form': form}
        return render(request, 'users/edit_profile.html', context)
Пример #20
0
def user(request, pk):
    user = get_object_or_404(User, pk=pk)
    posts = Post.objects.filter(user_id=pk, deleted=False)
    form2 = SignInForm()

    if request.method == 'POST':
        form = EditProfileForm(request.POST, request.FILES)
        if form.is_valid(
        ) and form.cleaned_data['password'] == form.cleaned_data['confirm']:
            u = User.objects.get(id=request.user.pk)
            u.first_name = form.cleaned_data['first_name']
            u.last_name = form.cleaned_data['last_name']
            u.userdetails.bio = form.cleaned_data['bio']
            if form.cleaned_data['image'] is not None:
                u.userdetails.image = form.cleaned_data['image']
            u.set_password(form.cleaned_data['password'])
            u.save()
            update_session_auth_hash(request, u)
            messages.add_message(request, messages.INFO, "Profile edited!")
            return redirect('user', user.pk)
    else:
        form = EditProfileForm(
            initial={
                'first_name': user.first_name,
                'last_name': user.last_name,
                'bio': user.userdetails.bio
            })

    return render(
        request, 'user.html', {
            'user': user,
            'posts': posts,
            'form': form,
            'form2': form2,
            'title': user.username
        })
Пример #21
0
def edit_profile(request):
    if request.method == 'POST':
        form_user = EditUserForm(data=request.POST, instance=request.user)
        form_profile = EditProfileForm(data=request.POST, files=request.FILES, instance=request.user.profile)
    else:
        form_user = EditUserForm(instance=request.user)
        form_profile = EditProfileForm(instance=request.user.profile)
    context = dict(
        form_user=form_user,
        form_profile=form_profile,
    )

    if request.method == 'POST' and form_user.is_valid() and form_profile.is_valid():
        form_user.save()
        form_profile.save()
        return redirect(reverse('users.profile'))
    return render(request, 'users/profile_edit.html', context)
Пример #22
0
def edit_profile(request,pk=None):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)
        if form.is_valid():
            form.save()
            args = {'form': form}
        # return render(request, 'users/edit_profile.html', {'form': form})
            # args = {'user': user}
            return render(request, 'users/profile.html', args)
    else:
        form = EditProfileForm(instance=request.user)
        if pk:
            user = User.objects.get(pk=pk)
        else:
            user = request.user
        args = {'user': user}
        return render(request, 'users/view_profile.html', args)
Пример #23
0
def edit_profile(request):
    """User edition view.

    **Context**

    ``form``
        An instance of EditProfileForm.

    **Template:**

    :template:`users/edit_profile.html`

    :template:`users/includes/_edit_profile_form.html`
    """
    context = dict()
    if request.method == 'POST':
        form = EditProfileForm(request.POST,
                               request.FILES,
                               instance=request.user)
        if form.is_valid():
            form.save()
            messages.add_message(
                request,
                messages.SUCCESS,
                'Profile updated successfully',
            )
            return redirect('users:profile')
        else:
            messages.add_message(request, messages.ERROR, 'Mistakes were made')

    form_data = {
        'email': request.user.email,
        'username': request.user.username,
        'avatar': request.user.avatar,
        'timezone': request.user.timezone,
        'riot_id': request.user.riot_id,
        'riot_tag': request.user.riot_tag,
    }
    context = {
        'form': EditProfileForm(initial=form_data),
    }
    return render(request, 'users/edit_profile.html', context)
Пример #24
0
def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)
        profile_form = ProfileForm(
            request.POST, request.FILES, instance=request.user.profile
        )  # request.FILES is show the selected image or file

        if form.is_valid() and profile_form.is_valid():
            user_form = form.save()
            custom_form = profile_form.save(False)
            custom_form.user = user_form
            custom_form.save()
            return redirect('profile')
    else:
        form = EditProfileForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
        args = {}
        # args.update(csrf(request))
        args['form'] = form
        args['profile_form'] = profile_form
        return render(request, 'users/editprofile.html', args)
Пример #25
0
def edit_profile(request):
    if request.user.userprofile is None:
        user_profile = UserProfile(user=request.user)
        user_profile.save
    if request.method == 'POST':
        form = EditProfileForm(request.POST,
                               request.FILES or None,
                               instance=request.user)
        profile_form = ProfileForm(
            request.POST,
            request.FILES or None,
            instance=request.user.userprofile
        )  # request.FILES is show the selected image or file

        if form.is_valid() and profile_form.is_valid():
            contactPrivacy = request.POST.get('contactPrivacy')
            profilePrivacy = request.POST.get('profilePrivacy')
            gender = request.POST.get('gender')
            user_form = form.save()
            custom_form = profile_form.save(False)
            custom_form.user = user_form
            custom_form.contactDetailsVisible = contactPrivacy
            custom_form.profileDetailsVisible = profilePrivacy
            custom_form.gender = gender
            custom_form.save()
            return redirect('users:user-profile')
    a = get_object_or_404(UserProfile, user=request.user)
    profilePic, coverPic = a.profilePic, a.coverPic
    form = EditProfileForm(instance=request.user)
    profile_form = ProfileForm(instance=request.user.userprofile)
    # args.update(csrf(request))
    context = {
        'form': profile_form,
        'myform': form,
        'pp': profilePic,
        'cp': coverPic,
        'a': a,
    }
    return render(request, 'users/editprofile.html', context)
Пример #26
0
 def get(self, request):
     user_form = EditUserForm(instance=request.user)
     profile_form = EditProfileForm(instance=request.user.profile)
     args = {'user_form': user_form, 'profile_form': profile_form}
     return render(request, 'users/edit_profile.html', args)