def profile(request): form = EditProfileForm(request.user) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('account/index') elif 'delete' in request.form: return redirect_to('account/delete') elif form.validate(request.form): picfile = request.files.get('picfile') picture = UserPicture(request.user) if picfile: form.save_changes() picture.place_file(picfile) else: pictype = request.user.userpictype if not form['userpictype']: form.data['userpictype'] = pictype if form['userpictype'] != pictype: picture.remove() form.save_changes() db.commit() flash(_(u'Your profile was updated successfully.'), 'info') return form.redirect('account/index') return render_account_response('account/edit_profile.html', 'profile.profile', form=form.as_widget())
def edit_user(request, user_id=None): """Edit a user. This can also create a user. If a new user is created the dialog is simplified, some unimportant details are left out. """ user = None if user_id is not None: user = User.query.get(user_id) if user is None: raise NotFound() form = EditUserForm(user) if request.method == 'POST': if request.form.get('cancel'): return form.redirect('admin/manage_users') elif request.form.get('delete') and user: return redirect_to('admin/delete_user', user_id=user.id) elif form.validate(request.form): picfile = request.files.get('picfile') if user is None: user = form.make_user() if picfile and form['userpictype'] == 'Upload': picture.place_file(picfile) msg = _(u'User %s created successfully.') icon = 'add' else: picture = UserPicture(request.user) if picfile: form.save_changes() if form['userpictype'] == 'Upload': picture.place_file(picfile) else: pictype = user.userpictype if not form['userpictype']: form.data['userpictype'] = pictype if form['userpictype'] != pictype: picture.remove() form.save_changes() msg = _(u'User %s edited successfully.') icon = 'info' db.commit() html_user_detail = u'<a href="%s">%s</a>' % ( escape(url_for(user)), escape(user.username) ) flash(msg % html_user_detail, icon) if request.form.get('save'): return form.redirect('admin/manage_users') return redirect_to('admin/edit_user', user_id=user.id) return render_admin_response('admin/edit_user.html', 'users_groups.users', form=form.as_widget())