[Groups] Add group logic to remaining routes#167
Conversation
Codecov Report
@@ Coverage Diff @@
## master #167 +/- ##
==========================================
- Coverage 91.09% 91.02% -0.08%
==========================================
Files 34 34
Lines 1191 1248 +57
==========================================
+ Hits 1085 1136 +51
- Misses 106 112 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
frgfm
left a comment
There was a problem hiding this comment.
Thanks for the PR!
Overall, I think we only need to change route signature formatting (stick to black when it's not a one-liner), and most importantly, optimize the group checking part of this PR. I added a few comments for this
| if entry is None: | ||
| return None | ||
| return entry["group_id"] |
There was a problem hiding this comment.
Three things to consider :
- if we keep this logic, the implementation is fine by me, we can also switch to a one-liner
return entry['group_id'] if isinstance(entry, dict) else None
- only a question but : shouldn't we raise an error if the entry-id doesn't exist?
- if we want to start optimizing SQL queries, here we actually only need the group_id field
| async def get_alert(alert_id: int = Path(..., gt=0), | ||
| requester=Security(get_current_access, | ||
| scopes=[AccessType.admin, AccessType.user])): |
There was a problem hiding this comment.
Let's keep black formatting when we have several lines:
async def get_alert(
alert_id: int = Path(..., gt=0),
requester=Security(get_current_access, _=[AccessType.admin, AccessType.user])
):
There was a problem hiding this comment.
Not sure I get it, didn't you ask in another review to have several lines in those cases?
There was a problem hiding this comment.
Sure, but 1 line per function argument
Besides, I think we can annotation typing to requester
| requested_group_id = await get_entity_group_id(alerts, alert_id) | ||
| await check_group_read(requester.id, requested_group_id) | ||
| return await crud.get_entry(alerts, alert_id) |
There was a problem hiding this comment.
over these 3 lines, we are querying the alerts table twice, we should try to improve this I think
There was a problem hiding this comment.
Only way I see to slightly speed up the process with the current implementation we have would be to do
retrieved_alert = await crud.get_entry(alerts, alert_id)
requested_group_id = await get_entity_group_id(devices, retrieved_alert["device_id"])
await check_group_read(requester.id, requested_group_id)
return retrieved_alertbut not sure it is worth it, it makes the code harder to read and not very logical.
| requested_group_id = await get_entity_group_id(alerts, alert_id) | ||
| await check_group_update(requester.id, requested_group_id) | ||
| return await crud.update_entry(alerts, payload, alert_id) |
There was a problem hiding this comment.
same suggestion as above roughly, not sure we can really speed up the process
| """ | ||
| Based on a site_id, updates information about the specified site | ||
| """ | ||
| # TODO: validate this one |
There was a problem hiding this comment.
validate the general design of group checking you mean?
There was a problem hiding this comment.
Validate that the route need to haveccess restrcted to group
| [2, 1, 401, "Permission denied"], | ||
| [1, 999, 404, "Entry not found"], | ||
| [1, 0, 422, None], | ||
| [4, 1, 401, "You can't access this ressource"], |
There was a problem hiding this comment.
A "Permission denied" would be better to unify error messages
There was a problem hiding this comment.
HMm, I personnally think that it would be helpful for the user to let him know that he is allowed to use that route but that he should check its input
There was a problem hiding this comment.
Perhaps let's make it a more detailed message then, because I understand it the same way as "Permission denied" (which can be applied to route or sub entry getting)
What I mean is: the route will fail anyway, the user won't know where and in one case that person will get "Permission denied" and another "You can't access this resource". I would understand that they meant there is a difference but I wouldn't have any clue what it is 😅
frgfm
left a comment
There was a problem hiding this comment.
Thanks for the PR! If you feel like it, I think we need to add a good explanation of how access is handled in the API in our README. While our documentation is very detailed for each route, the general process might sound obscure to most
Context
This PR is the last part of the group implementation. After having implemented the group logic for the fetching route, we have added the group logic to the remaining routes.
Content:
Routes for which I have a strong doubt have a # TODO comment in them.
The commits are separated to ease the review, there is one commit per object.
This PR should close #125