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

Add group members view with permission checks #30

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

HarmonicReflux
Copy link
Contributor

@HarmonicReflux HarmonicReflux commented Nov 14, 2024

Description

  • Implement group_members_view to display members of a research group based on the GroupMember model.
  • Added permission checks to allow access only for the group owner or an admin.
  • Included check for PI (Principal Investigator) status to ensure group ownership.
  • Render group_members.html for authorised users; display no_group.html with a message for non-PI users.

Fixes #18, #34.

Type of change

  • Documentation (non-breaking change that adds or improves the documentation)
  • New feature (non-breaking change which adds functionality)
  • Optimization (non-breaking, back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (whatever its nature)

Key checklist

  • All tests pass (eg. python -m pytest)
  • The documentation builds and looks OK (eg. python -m sphinx -b html docs docs/build)
  • Pre-commit hooks run successfully (eg. pre-commit run --all-files)

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added or an issue has been opened to tackle that in the future. (Indicate issue here: # (issue))

… checks.

- Implement `group_members_view` to display members of a research group based on the `GroupMember` model.
- Added permission checks to allow access only for the group owner or an admin.
- Included check for PI (Principal Investigator) status to ensure group ownership.
- Render `group_members.html` for authorised users; display `no_group.html` with a message for non-PI users.
@HarmonicReflux HarmonicReflux self-assigned this Nov 14, 2024
@HarmonicReflux HarmonicReflux changed the title Add group members view with Ruff required docstring and permissions… Add group members view with permission checks Nov 14, 2024
@HarmonicReflux HarmonicReflux added backend Related to the business logic of the tool feature Adding a new functionality, small or large labels Nov 14, 2024
@cc-a
Copy link
Collaborator

cc-a commented Nov 19, 2024

Based on our session yesterday you're hopefully now able to try this out in the browser @HarmonicReflux?

@HarmonicReflux
Copy link
Contributor Author

HarmonicReflux commented Nov 25, 2024

I am getting closer @cc-a , but encounter this error int eh CI/CD tests:
failed_test

I import admin using the codebase we already have, i.e. like so
from django.contrib import admin
so not sure why the error is thrown.

@cc-a
Copy link
Collaborator

cc-a commented Nov 26, 2024

I've taken the liberty of pushing a couple of commits. The first addresses the issue you reported by updating the settings used during tests to include the django.contrib.auth module. This came up now as we added some functionality using the admin module. An unfortunate clunky artifact on of how standalone Django apps work.

I then hit a subsequent issue with mypy that I was unable to resolve. I'm going to create a follow up issue to look into how to deal with these mypy issues properly but have disabled the mypy checks for now. Maybe I'm just missing something but I'm increasingly dubious of the merits of using mypy with Django.

@HarmonicReflux
Copy link
Contributor Author

Thanks for this.

Well, as per out discussions in person and online, maybe its best to deactivate mypy for this project entirely .
I think insufficient support of non-primitive types in Python are a "limitation of the language on purpose". If they are more pain than good for this project, disabling them may be our best bet, but that is just my opinion.

It is certainly worth looking into it in more detail so we have a good assessment of the technical debt we incur switching mypy off.

@HarmonicReflux HarmonicReflux marked this pull request as ready for review November 26, 2024 23:35
Copy link
Contributor

@AdrianDAlessandro AdrianDAlessandro left a comment

Choose a reason for hiding this comment

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

I can't work out how to test this. How do I access the view?

@SaranjeetKaur
Copy link
Contributor

SaranjeetKaur commented Nov 27, 2024

A question: Is a new file generated in the migrations/ folder each time makemigrations.py is run? For example, in this PR there is a new file migrations/0003_remove_groupmembership_owner_researchgroup_and_more.py.

@AdrianDAlessandro
Copy link
Contributor

A question: Is a new file generated in the migrations/ folder each time makemigrations.py is run? For example, in this PR there is a new file migrations/0003_remove_groupmembership_owner_researchgroup_and_more.py.

Yes, if there are changes to the models then we need to make migrations, which are then saved in migrations/

@cc-a
Copy link
Collaborator

cc-a commented Nov 27, 2024

@AdrianDAlessandro Unfortunately you'll have to type the url in manually. Until we get #21 there is nowhere obvious to put the link.

Copy link
Collaborator

@cc-a cc-a left a comment

Choose a reason for hiding this comment

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

We'll need some tests for this but let's do that in follow up PR.

@AdrianDAlessandro
Copy link
Contributor

@AdrianDAlessandro Unfortunately you'll have to type the url in manually. Until we get #21 there is nowhere obvious to put the link.

I went to /group/1/members/ and it wasn't a valid URL. Am I doing something wrong there?

@cc-a
Copy link
Collaborator

cc-a commented Nov 27, 2024

@AdrianDAlessandro I think you need to go to icl/group/1/members/ as all of the plugin urls are added to Coldfront under a prefix.

Copy link
Contributor

@AdrianDAlessandro AdrianDAlessandro left a comment

Choose a reason for hiding this comment

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

Ok, I managed to work out I need to navigate to http://127.0.0.1:8000/icl/group/1/members/ and it worked for when I wasn't a PI. But after setting is_pi to True it failed with this error (I believe the same error mypy was showing):

...
  File "/usr/src/imperial_coldfront_plugin/imperial_coldfront_plugin/views.py", line 51, in group_members_view
    group_members = GroupMembership.objects.filter(owner=user)
...
FieldError at /icl/group/1/members/
Cannot resolve keyword 'owner' into field. Choices are: group, group_id, id, member, member_id


from .models import GroupMembership

User = get_user_model()
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels wrong... Shouldn't we be using get_user_model() directly each time?

Comment on lines +45 to +46
if request.user != user and not request.user.is_superuser:
return HttpResponseForbidden("Permission denied")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why can only superusers can view this?

if request.user != user and not request.user.is_superuser:
return HttpResponseForbidden("Permission denied")

if not user.userprofile.is_pi:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this userprofile a field provided by coldfront?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to the business logic of the tool feature Adding a new functionality, small or large
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Group membership view
4 participants