-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/#554 filter destruction lists #572
base: main
Are you sure you want to change the base?
Changes from all commits
fd6f42b
4bbdfe5
103afe9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,6 +9,38 @@ | |||||||||||||||||
from .serializers import UserSerializer | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
@extend_schema( | ||||||||||||||||||
tags=["Users"], | ||||||||||||||||||
summary=_("Users list"), | ||||||||||||||||||
description=_("List all the users."), | ||||||||||||||||||
responses={ | ||||||||||||||||||
200: UserSerializer(many=True), | ||||||||||||||||||
}, | ||||||||||||||||||
) | ||||||||||||||||||
class UsersView(ListAPIView): | ||||||||||||||||||
serializer_class = UserSerializer | ||||||||||||||||||
|
||||||||||||||||||
def get_queryset(self) -> QuerySet[User]: | ||||||||||||||||||
return User.objects.all() | ||||||||||||||||||
Comment on lines
+20
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for not doing:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
@extend_schema( | ||||||||||||||||||
tags=["Users"], | ||||||||||||||||||
summary=_("Record managers list"), | ||||||||||||||||||
description=_( | ||||||||||||||||||
"List all the users that have the permission to create destruction lists." | ||||||||||||||||||
), | ||||||||||||||||||
responses={ | ||||||||||||||||||
200: UserSerializer(many=True), | ||||||||||||||||||
}, | ||||||||||||||||||
) | ||||||||||||||||||
class RecordManagersView(ListAPIView): | ||||||||||||||||||
serializer_class = UserSerializer | ||||||||||||||||||
|
||||||||||||||||||
def get_queryset(self) -> QuerySet[User]: | ||||||||||||||||||
return User.objects.record_managers() | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
@extend_schema( | ||||||||||||||||||
tags=["Users"], | ||||||||||||||||||
summary=_("Main reviewers list"), | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,50 @@ | |
|
||
|
||
class UserQuerySet(QuerySet): | ||
def record_managers(self) -> "UserQuerySet": | ||
""" | ||
Returns a QuerySet of users that have the permission to start a destruction list. | ||
""" | ||
permission = Permission.objects.get(codename="can_start_destruction") | ||
return self._users_with_permission(permission) | ||
|
||
def reviewers(self) -> "UserQuerySet": | ||
""" | ||
Returns a QuerySet of users that have the permission to review a destruction list. | ||
""" | ||
return self.filter( # noqa | ||
Q(groups__permissions__codename="can_review_destruction") | ||
| Q(user_permissions__codename="can_review_destruction") | ||
| Q(groups__permissions__codename="can_review_final_list") | ||
| Q(user_permissions__codename="can_review_final_list") | ||
).distinct() | ||
|
||
def main_reviewers(self) -> "UserQuerySet": | ||
""" | ||
Returns a QuerySet of users that have the permission to review a destruction list (main reviewers ony). | ||
""" | ||
permission = Permission.objects.get(codename="can_review_destruction") | ||
return self._users_with_permission(permission) | ||
|
||
def co_reviewers(self) -> "UserQuerySet": | ||
""" | ||
Returns a QuerySet of users that have the permission to review a destruction list (co reviewers ony). | ||
""" | ||
permission = Permission.objects.get(codename="can_co_review_destruction") | ||
return self._users_with_permission(permission) | ||
|
||
def archivists(self) -> "UserQuerySet": | ||
""" | ||
Returns a QuerySet of users that have the permission to perform a final review on a destruction list. | ||
""" | ||
permission = Permission.objects.get(codename="can_review_final_list") | ||
return self._users_with_permission(permission) | ||
|
||
def _users_with_permission(self, permission: Permission) -> "UserQuerySet": | ||
return self.filter( # noqa | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the added noqa? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My IDE resolves the type to be QuerySet but AFAIK if executed in this context it will return a UserQuerySet. |
||
Q(groups__permissions=permission) | Q(user_permissions=permission) | ||
).distinct() | ||
|
||
def annotate_permissions(self) -> "UserQuerySet": | ||
""" | ||
Adds `user_permission_codenames` and `group_permission_codenames` as `ArrayField` to the current QuerySet | ||
|
@@ -71,20 +115,3 @@ def create_superuser(self, username, email, password, **extra_fields): | |
raise ValueError("Superuser must have is_superuser=True.") | ||
|
||
return self._create_user(username, email, password, **extra_fields) | ||
|
||
def _users_with_permission(self, permission: Permission) -> UserQuerySet: | ||
return self.filter( | ||
Q(groups__permissions=permission) | Q(user_permissions=permission) | ||
).distinct() | ||
|
||
def main_reviewers(self) -> UserQuerySet: | ||
permission = Permission.objects.get(codename="can_review_destruction") | ||
return self._users_with_permission(permission) | ||
|
||
def archivists(self) -> UserQuerySet: | ||
permission = Permission.objects.get(codename="can_review_final_list") | ||
return self._users_with_permission(permission) | ||
|
||
def co_reviewers(self) -> UserQuerySet: | ||
permission = Permission.objects.get(codename="can_co_review_destruction") | ||
return self._users_with_permission(permission) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These endpoints are getting a bit out of hand, maybe we should refactor to use filters?
So then we would have
GET /api/v1/users?role=reviewer
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#574