From 92a5290aefad0c5062e70eb77c7df7af648ff475 Mon Sep 17 00:00:00 2001 From: Allen Lee Date: Wed, 4 Sep 2024 21:04:55 -0700 Subject: [PATCH] refactor: move logic to CodeMetaSchema --- django/core/view_helpers.py | 6 +-- django/library/models.py | 76 ++++++++++++++++-------------- django/library/tests/test_views.py | 10 +--- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/django/core/view_helpers.py b/django/core/view_helpers.py index 383c00f72..52b7f7d8a 100644 --- a/django/core/view_helpers.py +++ b/django/core/view_helpers.py @@ -107,10 +107,6 @@ def get_search_queryset( criteria.update(tags__name__in=[t.lower() for t in tags]) operator = 'and' """ - - """ - Build text search query - """ if query: Query.get(query).add_hit() @@ -120,6 +116,7 @@ def get_search_queryset( # filters, query = parse_query_string(query, operator="and") # criteria.update(filters) + # generate search query from text query = build_search_query(query) else: query = MATCH_ALL @@ -176,7 +173,6 @@ def retrieve_with_perms(self, request, *args, **kwargs): def add_user_retrieve_perms(instance, data, user): - print(user.get_all_permissions()) data["has_change_perm"] = user.has_perm( f"{instance._meta.app_label}.change_{instance._meta.model_name}", instance, diff --git a/django/library/models.py b/django/library/models.py index 304be652e..321cb6a07 100644 --- a/django/library/models.py +++ b/django/library/models.py @@ -190,11 +190,14 @@ def to_affiliation_string(cls, afl): @property def codemeta_affiliation(self): """ - FIXME: move to CodeMeta class For now codemeta affiliations appear to be a single https://schema.org/Organization """ if self.json_affiliations: - return self.to_codemeta_affiliation(self.json_affiliations[0]) + return CodeMetaSchema.convert_affiliation(self.json_affiliations[0]) + + @property + def primary_affiliation(self): + return self.json_affiliations[0] if self.json_affiliations else {} @property def primary_affiliation_name(self): @@ -204,20 +207,6 @@ def primary_affiliation_name(self): def primary_json_affiliation_name(self): return self.json_affiliations[0]["name"] if self.json_affiliations else "" - def to_codemeta_affiliation(self, affiliation): - # FIXME: move to CodeMeta class - if affiliation: - return { - # FIXME: may switch to https://schema.org/ResearchOrganization at some point - "@type": "Organization", - "@id": affiliation.get("ror_id"), - "name": affiliation.get("name"), - "url": affiliation.get("url"), - "identifier": affiliation.get("ror_id"), - "sameAs": affiliation.get("ror_id"), - } - return {} - @staticmethod def from_user(user): """ @@ -274,21 +263,8 @@ def member_profile_url(self): def get_markdown_link(self): return f"[{self.get_full_name()}]({self.member_profile_url})" - # FIXME: move to CodeMeta transformer class def to_codemeta(self): - codemeta = { - "@type": "Person", - # FIXME: Contributor should proxy to User / MemberProfile fields if User is available and given_name and family_name are not set - "givenName": self.given_name, - "familyName": self.family_name, - } - if self.orcid_url: - codemeta["@id"] = self.orcid_url - if self.json_affiliations: - codemeta["affiliation"] = self.codemeta_affiliation - if self.email: - codemeta["email"] = self.email - return codemeta + return CodeMetaSchema.convert_contributor(self) def get_aggregated_search_fields(self): return " ".join( @@ -2596,7 +2572,7 @@ def convert_platforms(self): def license_url(self): if self.license: return self.license.url - return "DEFAULT LICENSE - https://opensource.org/licenses/MIT" + return "DEFAULT LICENSE: https://opensource.org/licenses/MIT" @property def descriptions(self): @@ -2673,10 +2649,8 @@ def convert(cls, codebase_release: CodebaseRelease): common_metadata = codebase_release.common_metadata metadata = { **cls.INITIAL_METADATA, - # FIXME: inherit fields from common_metadata, look into how to do this - # better, snake_case to camelCase of variable names is also an issue - **common_metadata.to_dict(), "@id": common_metadata.permanent_url, + "name": common_metadata.name, "copyrightYear": common_metadata.copyright_year, "dateCreated": common_metadata.date_created.isoformat(), "dateModified": common_metadata.date_modified.isoformat(), @@ -2717,10 +2691,42 @@ def to_creative_work(cls, text): @classmethod def convert_authors(cls, common_metadata: CommonMetadata): return [ - author.contributor.to_codemeta() + cls.convert_contributor(author.contributor) for author in common_metadata.release_contributor_authors ] + @classmethod + def convert_ror_affiliation(cls, affiliation: dict): + if affiliation: + return { + # FIXME: may switch to https://schema.org/ResearchOrganization at some point + "@type": "Organization", + "@id": affiliation.get("ror_id"), + "name": affiliation.get("name"), + "url": affiliation.get("url"), + "identifier": affiliation.get("ror_id"), + "sameAs": affiliation.get("ror_id"), + } + return {} + + @classmethod + def convert_contributor(cls, contributor: Contributor): + codemeta = { + "@type": "Person", + # FIXME: Contributor should proxy to User / MemberProfile fields if User is available and given_name and family_name are not set + "givenName": contributor.given_name, + "familyName": contributor.family_name, + } + if contributor.orcid_url: + codemeta["@id"] = contributor.orcid_url + if contributor.json_affiliations: + codemeta["affiliation"] = cls.convert_ror_affiliation( + contributor.primary_affiliation + ) + if contributor.email: + codemeta["email"] = contributor.email + return codemeta + @classmethod def convert_target_product(cls, common_metadata: CommonMetadata): target_product = { diff --git a/django/library/tests/test_views.py b/django/library/tests/test_views.py index 2c6ad9d50..6e4c390e3 100644 --- a/django/library/tests/test_views.py +++ b/django/library/tests/test_views.py @@ -414,20 +414,12 @@ def test_list_files(self): self.assertEqual( response.status_code, expected_status_code, - msg="{} {}".format(repr(user), response.data), + msg=f"{user} {response.data}", ) def test_delete_file(self): path_to_foo = pathlib.Path("foo.txt") api = self.codebase_release.get_fs_api() - print(self.codebase_release) - print( - "CodebaseRelease perm %s" - % self.submitter.has_perm( - "library.delete_codebaserelease", self.codebase_release - ) - ) - # Unpublished codebase release permissions response = self.client.delete( api.get_absolute_url(