Skip to content

Commit

Permalink
update for node context specific curator status
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Jan 9, 2025
1 parent 2a65d23 commit 81c2612
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 15 additions & 1 deletion osf/models/contributor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models, IntegrityError
from osf.utils.fields import NonNaiveDateTimeField
from osf.utils import permissions
from osf.utils import permissions, workflows
from django.utils.functional import cached_property


class AbstractBaseContributor(models.Model):
Expand Down Expand Up @@ -35,6 +36,19 @@ class Contributor(AbstractBaseContributor):
def _id(self):
return f'{self.node._id}-{self.user._id}'

@cached_property
def is_curator(self):
"""
Determine if the user is a curator on this node.
This avoids querying the database repeatedly by caching the result.
"""
from osf.models import NodeRequest
return NodeRequest.objects.filter(
target=self.node,
creator=self,
request_type=workflows.NodeRequestTypes.INSTITUTIONAL_REQUEST
).exists()

class Meta:
unique_together = ('user', 'node')
# Make contributors orderable
Expand Down
10 changes: 8 additions & 2 deletions osf/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,16 @@ def osf_groups(self):
OSFGroup = apps.get_model('osf.OSFGroup')
return get_objects_for_user(self, 'member_group', OSFGroup, with_superuser=False)

def is_institutional_admin(self, institution=None):
def is_institutional_admin(self, institution=None, node=None):
"""
Checks if user is admin of any or of a specific institution.
Checks if user is admin of any or of a specific institution, or and curator on a specific node.
"""
if node:
return Contributor.objects.get(
node=node,
user=self,
).is_curator

if not institution:
return self.groups.filter(
name__startswith='institution_',
Expand Down
2 changes: 1 addition & 1 deletion website/profile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def serialize_user(user, node=None, admin=False, full=False, is_profile=False, i
'surname': user.family_name,
'fullname': fullname,
'shortname': fullname if len(fullname) < 50 else fullname[:23] + '...' + fullname[-23:],
'is_curator': user.is_institutional_admin(),
'is_curator': user.is_institutional_admin(node=node),
'profile_image_url': user.profile_image_url(size=settings.PROFILE_IMAGE_MEDIUM),
'active': user.is_active,
}
Expand Down

0 comments on commit 81c2612

Please sign in to comment.