Skip to content

Commit

Permalink
use all_tags as m2m value when copying tags
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Oct 17, 2023
1 parent 9b4bbec commit 39cfe46
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ def test_affiliated_institutions_are_copied_from_node_no_institutions(self, app,
"""
project = ProjectFactory(is_public=True, creator=user)
payload['data']['relationships']['branched_from']['data']['id'] = project._id

res = app.post_json_api(
url_draft_registrations,
payload,
Expand Down
11 changes: 7 additions & 4 deletions osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,9 @@ def copy_editable_fields(
self, resource, alternative_resource=None, include_contributors=True, save=True, excluded_attributes=None
):
"""
Copy editable fields from 'resource' to the current object.
Copy editable fields from 'resource' to the current object. DraftRegistrations have different rules for creation
based on whether they are based on OSF Projects or are 'no-project` Registrations that will copu default
metadata information from the user, or some other source other then an OSF Project or Node.
:param Object resource: Primary resource to copy attributes from
:param Object alternative_resource: Backup resource for copying attributes
Expand All @@ -2288,17 +2290,18 @@ def copy_editable_fields(
self.save()

def _copy_basic_attributes(self, resource, alternative_resource, excluded_attributes):
# Copy basic attributes such as title, description, category, and node_license
for attribute in ['title', 'description', 'category', 'node_license']:
if attribute not in excluded_attributes:
self.set_editable_attribute(attribute, resource, alternative_resource)

def _copy_m2m_fields(self, resource, alternative_resource, excluded_attributes):
# Copy m2m fields like tags, subjects, and affiliated_institutions
m2m_fields = ['tags', 'subjects', 'affiliated_institutions']
for field in m2m_fields:
if field not in excluded_attributes:
getattr(self, field).add(*self.stage_m2m_values(field, resource, alternative_resource))
if field == 'tags': # special case tags with value `all_tags'
self.tags.add(*self.stage_m2m_values('all_tags', resource, alternative_resource))
else:
getattr(self, field).add(*self.stage_m2m_values(field, resource, alternative_resource))

class Meta:
abstract = True
2 changes: 1 addition & 1 deletion osf/models/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,13 +1280,13 @@ def create_from_node(cls, user, schema, node=None, data=None, provider=None):
provider=provider,
)
draft.save()
draft.update(data, auth=Auth(user))
draft.copy_editable_fields(
branched_from,
Auth(user),
save=True,
excluded_attributes=excluded_attributes
)
draft.update(data, auth=Auth(user))

if not node:
draft.affiliated_institutions.add(*draft.creator.get_affiliated_institutions())
Expand Down

0 comments on commit 39cfe46

Please sign in to comment.