Skip to content

Commit

Permalink
merge methods for clarity and DRYness
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnetordoff committed Feb 26, 2025
1 parent 63f36cb commit 226808e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
34 changes: 14 additions & 20 deletions osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
ReviewTriggers,
)

from osf.utils.requests import get_request_and_user_id, get_current_request
from osf.utils.requests import get_request_and_user_id
from website.project import signals as project_signals
from website import settings, mails, language
from website.project.licenses import set_license
Expand Down Expand Up @@ -482,31 +482,25 @@ def get_addon_key(cls, config):
def addons(self):
return self.get_addons()

def get_addons(self, service_type: str | None = None):
request, user_id = get_request_and_user_id()
if flag_is_active(request, features.ENABLE_GV):
osf_addons = filter(
lambda x: x is not None,
(self.get_addon(addon) for addon in self.OSF_HOSTED_ADDONS)
)
return itertools.chain(osf_addons, self._get_addons_from_gv(requesting_user_id=user_id, service_type=service_type))
def get_addons(self, service_type: str | None = None, in_request_context: bool = True):
'''
This gets all a user's addons whether that user is the model user (self.) or the user making the request (the
user signing off on whatever auth mechicanism such as token or basic auth.
return [_f for _f in [
self.get_addon(config.short_name)
for config in self.ADDONS_AVAILABLE
] if _f]
service_type is the addon type such as "storage" or "citations"
in_request_context is the addon for the requesting user? or is it outside the request context.
'''
if in_request_context:
request, user_id = get_request_and_user_id()
else:
user_id = self._id

def get_addons_for_self(self):
"""
This refers to the model self, not the user making the request.
"""
request = get_current_request()
if flag_is_active(request, features.ENABLE_GV):
if not in_request_context or (in_request_context and flag_is_active(request, features.ENABLE_GV)):
osf_addons = filter(
lambda x: x is not None,
(self.get_addon(addon) for addon in self.OSF_HOSTED_ADDONS)
)
return itertools.chain(osf_addons, self._get_addons_from_gv(requesting_user_id=self._id))
return itertools.chain(osf_addons, self._get_addons_from_gv(requesting_user_id=user_id, service_type=service_type))

return [_f for _f in [
self.get_addon(config.short_name)
Expand Down
2 changes: 1 addition & 1 deletion osf/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ def contributed(self):
@property
def can_be_merged(self):
"""The ability of the `merge_user` method to fully merge the user"""
return all(addon.can_be_merged for addon in self.get_addons_for_self())
return all(addon.can_be_merged for addon in self.get_addons(in_request_context=False))

def merge_user(self, user):
"""Merge a registered user into this account. This user will be
Expand Down
3 changes: 2 additions & 1 deletion osf_tests/test_merging_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ def is_mrm_field(value):
def test_merge_unconfirmed(self):
self._add_unconfirmed_user()
unconfirmed_username = self.unconfirmed.username
self.user.merge_user(self.unconfirmed)
with override_flag(ENABLE_GV, active=True):
self.user.merge_user(self.unconfirmed)

assert self.unconfirmed.is_merged is True
assert self.unconfirmed.merged_by == self.user
Expand Down

0 comments on commit 226808e

Please sign in to comment.