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 bb2c6b2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 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
14 changes: 10 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 @@ -2279,7 +2281,10 @@ def copy_editable_fields(
self._copy_basic_attributes(resource, alternative_resource, excluded_attributes)

if include_contributors:
# Contributors will always come from "resource", as contributor constraints
# will require contributors to be present on the resource
self.copy_contributors_from(resource)
# Copy unclaimed records for unregistered users
self.copy_unclaimed_records(resource)

self._copy_m2m_fields(resource, alternative_resource, excluded_attributes)
Expand All @@ -2288,17 +2293,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
5 changes: 1 addition & 4 deletions osf/models/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,6 @@ def create_from_node(cls, user, schema, node=None, data=None, provider=None):
provider.validate_schema(schema)

excluded_attributes = []

if node:
branched_from = node
else:
Expand All @@ -1280,13 +1279,11 @@ 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 bb2c6b2

Please sign in to comment.