Skip to content

Commit

Permalink
Count of owned assets in user listing page is zero, even if they own …
Browse files Browse the repository at this point in the history
…assets (#64)

We fixed this by getting assets separately when getting a user to render
  • Loading branch information
create-issue-branch[bot] committed Mar 21, 2021
1 parent 6e1c421 commit 679ea53
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -10,6 +10,7 @@ Bugfixes
--------
* Show screenshots in documentation and add some missing content [see `PR #60 <http://www.github.com/SeitaBV/flexmeasures/pull/60>`_]
* Documentation listed 2.0 API endpoints twice [see `PR #59 <http://www.github.com/SeitaBV/flexmeasures/pull/59>`_]
* User page did not list number of assets correctly [see `PR #64 <http://www.github.com/SeitaBV/flexmeasures/pull/64>`_]



Expand Down
20 changes: 16 additions & 4 deletions flexmeasures/ui/crud/users.py
Expand Up @@ -32,11 +32,15 @@ class UserForm(FlaskForm):
active = BooleanField("Activation Status", validators=[DataRequired()])


def render_user(user: Optional[User], msg: str = None):
def render_user(user: Optional[User], asset_count: int = 0, msg: str = None):
user_form = UserForm()
user_form.process(obj=user)
return render_flexmeasures_template(
"crud/user.html", user=user, user_form=user_form, msg=msg
"crud/user.html",
user=user,
user_form=user_form,
asset_count=asset_count,
msg=msg,
)


Expand Down Expand Up @@ -88,8 +92,16 @@ def get(self, id: str):
get_user_response = InternalApi().get(
url_for("flexmeasures_api_v2_0.get_user", id=id)
)
user = process_internal_api_response(get_user_response.json(), make_obj=True)
return render_user(user)
user: User = process_internal_api_response(
get_user_response.json(), make_obj=True
)
asset_count = 0
if user:
get_users_assets_response = InternalApi().get(
url_for("flexmeasures_api_v2_0.get_assets", owner_id=user.id)
)
asset_count = len(get_users_assets_response.json())
return render_user(user, asset_count=asset_count)

@roles_required("admin")
def toggle_active(self, id: str):
Expand Down
2 changes: 1 addition & 1 deletion flexmeasures/ui/templates/crud/user.html
Expand Up @@ -67,7 +67,7 @@ <h2> Account overview for {{ user.username }} </h2>
Assets owned
</td>
<td>
<a href="/assets/owned_by/{{ user.id }}">{{ user.assets | length }}</a>
<a href="/assets/owned_by/{{ user.id }}">{{ asset_count }}</a>
</td>
</tr>
<tr>
Expand Down
6 changes: 6 additions & 0 deletions flexmeasures/ui/tests/test_user_crud.py
Expand Up @@ -41,11 +41,17 @@ def test_user_page(client, as_admin, requests_mock):
requests_mock.get(
"http://localhost//api/v2_0/user/2", status_code=200, json=mock_user
)
requests_mock.get(
"http://localhost//api/v2_0/assets",
status_code=200,
json=[{}, {}, {}], # we only care about the length
)
user_page = client.get(url_for("UserCrudUI:get", id=2), follow_redirects=True)
assert user_page.status_code == 200
assert (
"Account overview for %s" % mock_user["username"]
).encode() in user_page.data
assert (">3</a>").encode() in user_page.data # this is the asset count
assert mock_user["email"].encode() in user_page.data


Expand Down

0 comments on commit 679ea53

Please sign in to comment.