Skip to content

Commit

Permalink
Allow to search user in admin by email
Browse files Browse the repository at this point in the history
As a fallback for when searching by username did not return any results.
  • Loading branch information
chdorner committed Feb 2, 2016
1 parent 2d0e3ca commit ce78358
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 4 additions & 2 deletions h/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,12 @@ def users_index(request):
user_meta = {}
username = request.params.get('username')

if username is not None:
if username:
user = models.User.get_by_username(username)
if not user:
user = models.User.get_by_email(username)

if user is not None:
if user:
# Fetch information on how many annotations the user has created
userid = util.userid_from_username(username, request)
query = _all_user_annotations_query(userid)
Expand Down
4 changes: 2 additions & 2 deletions h/templates/admin/users.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<form method="GET" class="form-inline">
<div class="form-group">
<label for="username">Username</label>
<label for="username">Username or email</label>
<input type="text" class="form-control" name="username">
<input type="submit" class="btn btn-default" value="Find">
</div>
Expand Down Expand Up @@ -44,7 +44,7 @@
</tbody>
</table>
{% else %}
<p>No user found with username <em>{{ username }}</em>!</p>
<p>No user found with username or email <em>{{ username }}</em>!</p>
{% endif %}
{% endif %}
{% endblock %}
13 changes: 13 additions & 0 deletions h/test/admin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,18 @@ def test_users_index_looks_up_users_by_username(User):

User.get_by_username.assert_called_with("bob")

@users_index_fixtures
def test_users_index_looks_up_users_by_email(User):
es = MagicMock()
request = DummyRequest(params={"username": "bob@builder.com"},
es=es)

User.get_by_username.return_value = None

admin.users_index(request)

User.get_by_email.assert_called_with("bob@builder.com")


@users_index_fixtures
def test_users_index_queries_annotation_count(User):
Expand All @@ -471,6 +483,7 @@ def test_users_index_no_user_found(User):
request = DummyRequest(params={"username": "bob"},
es=es)
User.get_by_username.return_value = None
User.get_by_email.return_value = None

result = admin.users_index(request)

Expand Down

0 comments on commit ce78358

Please sign in to comment.