def v_user_delete(user_id): form = UserDeleteForm() a_user = UserApi() try: existing_user = a_user.read(user_id) except DatabaseItemDoesNotExist as e: flash(_('No user with id {0}').format(user_id)) return redirect(url_for('admin.v_user_list')) except Exception as e: flash(_('An unexpected error occurred: {0}').format(e)) # flash('An unexpected error occurred.') return redirect(url_for('admin.v_user_list')) if request.method == 'POST' and form.validate_on_submit(): if a_user.delete(user_id) is True: flash(_('User {0} deleted').format(existing_user.email)) return redirect(url_for('admin.v_user_list')) else: flash(_('Unable to delete user {0}').format(existing_user.email)) return render_template('admin/generic/delete.html', action_url=url_for('admin.v_user_delete', user_id=user_id), item_type=_('User'), item_identifier=existing_user.email, form=form) return render_template('admin/generic/delete.html', action_url=url_for('admin.v_user_delete', user_id=user_id), item_type=_('User'), item_identifier=existing_user.email, form=form)
def v_user_edit(user_id): form = UserModifyForm() a_user = UserApi() db_roles = a_roles.list() possible_roles = [] for db_role in db_roles: possible_roles.append((db_role.id, db_role.role)) form.roles.choices = possible_roles try: existing_user = a_user.read(user_id) except DatabaseItemDoesNotExist: flash(_('A user with id {0} does not exist.').format(user_id)) return redirect(url_for('admin.v_user_list')) if request.method == 'POST' and form.validate_on_submit(): input_data = { 'email': form.email.data, 'password': form.password.data, 'roles': form.roles.data } ## # We very much dislike empty passwords, so we assume that, if the password field # is empty, the user didn't want to change it. if input_data['password'] == '' or input_data['password'] is None: update_password = False else: update_password = True try: edited_user = a_user.update(user_id, input_data, update_password) except DatabaseItemDoesNotExist as e: flash(_('No user with id {0}').format(user_id)) return redirect(url_for('admin.v_user_list')) except RequiredAttributeMissing as e: flash( _('A required form element was not submitted: {0}').format(e)) return render_template('admin/user/edit.html', form=form, user_id=user_id) except Exception as e: flash(_('An unexpected error occurred: {0}').format(e)) # flash('An unexpected error occurred.') return render_template('admin/user/edit.html', form=form, user_id=user_id) else: return redirect(url_for('admin.v_user_list')) else: ## # Add the data from the existing user for the edit form. This must be done after validate_on_submit() # or it will overwrite the data from the submitted form. form.email.default = existing_user.email roles_default = [] for role in existing_user.roles: roles_default.append(str(role.id)) form.roles.default = roles_default # http://stackoverflow.com/questions/5519729/wtforms-how-to-select-options-in-selectmultiplefield form.process() return render_template('admin/user/edit.html', form=form, user_id=user_id)