Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count of owned assets in user listing page is zero, even if they own assets #64

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -9,6 +9,7 @@ v0.2.4 | March XX, 2021
Bugfixes
--------
* 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>`_]


v0.2.3 | February 27, 2021
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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why did this not work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our UI calls our API, but the API is not returning the assets of the user like SQLAlchemy automatically does (with a second query). Our API returns a JSON structure, and whoever wants the assets can make a second call.

<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