diff --git a/tests/test_role.py b/tests/test_role.py index 787c7078544108e1b8392edd558654fb01622c19..74f4550e02ca06e2dc29b4727af651b2196cfc6a 100644 --- a/tests/test_role.py +++ b/tests/test_role.py @@ -117,7 +117,7 @@ class TestRoleViews(UffdTestCase): self.assertEqual(r.status_code, 200) def test_new(self): - r = self.client.get(path=url_for('role.show'), follow_redirects=True) + r = self.client.get(path=url_for('role.new'), follow_redirects=True) dump('role_new', r) self.assertEqual(r.status_code, 200) diff --git a/uffd/role/templates/role.html b/uffd/role/templates/role.html index cbd5a115c4a51b3bcfceada3efe2886eb0f76558..8cfe3456e89ca41dc3662130495fae1cf58d7538 100644 --- a/uffd/role/templates/role.html +++ b/uffd/role/templates/role.html @@ -28,13 +28,13 @@ <div class="tab-pane fade show active" id="settings" role="tabpanel" aria-labelledby="settings-tab"> <div class="form-group col"> <label for="role-name">Role Name</label> - <input type="text" class="form-control" id="role-name" name="name" value="{{ role.name }}"> + <input type="text" class="form-control" id="role-name" name="name" value="{{ role.name or '' }}"> <small class="form-text text-muted"> </small> </div> <div class="form-group col"> <label for="role-description">Description</label> - <textarea class="form-control" id="role-description" name="description" rows="5">{{ role.description }}</textarea> + <textarea class="form-control" id="role-description" name="description" rows="5">{{ role.description or '' }}</textarea> <small class="form-text text-muted"> </small> </div> diff --git a/uffd/role/templates/role_list.html b/uffd/role/templates/role_list.html index 54b5a7d6458202e66af84fc73d8e08caee177733..d5b66624cd36f2b71082b52fdaf816a85e980c95 100644 --- a/uffd/role/templates/role_list.html +++ b/uffd/role/templates/role_list.html @@ -4,7 +4,7 @@ <div class="row"> <div class="col"> <p class="text-right"> - <a class="btn btn-primary" href="{{ url_for("role.show") }}"> + <a class="btn btn-primary" href="{{ url_for("role.new") }}"> <i class="fa fa-plus" aria-hidden="true"></i> New </a> </p> diff --git a/uffd/role/views.py b/uffd/role/views.py index 1d77a1bf1cd659078fd98e4638161846a7e5c6be..be6ecff5897fe2279f547da1ee9aeb00087ba2d4 100644 --- a/uffd/role/views.py +++ b/uffd/role/views.py @@ -51,23 +51,22 @@ def role_acl_check(): def index(): return render_template('role_list.html', roles=Role.query.all()) -@bp.route("/<int:roleid>") @bp.route("/new") -def show(roleid=False): +def new(): + return render_template('role.html', role=Role(), groups=Group.query.all(), roles=Role.query.all()) + +@bp.route("/<int:roleid>") +def show(roleid=None): # prefetch all users so the ldap orm can cache them and doesn't run one ldap query per user User.query.all() - if not roleid: - role = Role() - else: - role = Role.query.filter_by(id=roleid).one() + role = Role.query.filter_by(id=roleid).one() return render_template('role.html', role=role, groups=Group.query.all(), roles=Role.query.all()) @bp.route("/<int:roleid>/update", methods=['POST']) @bp.route("/new", methods=['POST']) @csrf_protect(blueprint=bp) -def update(roleid=False): - is_newrole = bool(not roleid) - if is_newrole: +def update(roleid=None): + if roleid is None: role = Role() db.session.add(role) else: @@ -91,7 +90,7 @@ def update(roleid=False): role.update_member_groups() db.session.commit() ldap.session.commit() - return redirect(url_for('role.show', roleid=roleid)) + return redirect(url_for('role.show', roleid=role.id)) @bp.route("/<int:roleid>/del") @csrf_protect(blueprint=bp)