Skip to content

Commit

Permalink
schemas: remove fields on documents and deposits
Browse files Browse the repository at this point in the history
Move the schema processing to dedicated classes.

* Adds fixtures for organisations and users.
* Removes collections, subdivisions, customField1, customField2
  and customField3 on documents and deposits schema.
* Removes subdivison on users for shared organisation.
* Closes rero#752.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Feb 15, 2022
1 parent 7eebf8e commit e2fef6f
Show file tree
Hide file tree
Showing 11 changed files with 474 additions and 85 deletions.
5 changes: 5 additions & 0 deletions data/organisations/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,10 @@
"path": "./unifr/logo.png"
}
]
},
{
"code": "shared",
"name": "Organisation shared",
"isShared": true
}
]
120 changes: 120 additions & 0 deletions data/users/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,125 @@
"organisation": {
"$ref": "https://sonar.ch/api/organisations/hepvs"
}
},
{
"first_name": "Shakira",
"last_name": "Greaves",
"email": "rero.sonar+admin-shared@gmail.com",
"password": "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvfR3m1mNVfsHgfgA",
"role": "admin",
"birth_date": "1971-12-01",
"street": "Brunnacherstrasse 112",
"postal_code": "5330",
"city": "Zurzach",
"phone": "+41625037543",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/shared"
}
},
{
"first_name": "Jordon",
"last_name": "Frank",
"email": "rero.sonar+moderator-shared@gmail.com",
"password": "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvfR3m1mNVfsHgfgA",
"role": "moderator",
"birth_date": "1964-10-10",
"street": "Lichtmattstrasse 123",
"postal_code": "5736",
"city": "Burg",
"phone": "+41627676171",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/shared"
}
},
{
"first_name": "Fabio",
"last_name": "Dowling",
"email": "rero.sonar+submitter-shared@gmail.com",
"password": "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvfR3m1mNVfsHgfgA",
"role": "submitter",
"birth_date": "1983-09-14",
"street": "Kirchstrasse 57",
"postal_code": "1660",
"city": "La Lécherette",
"phone": "+41269639084",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/shared"
}
},
{
"first_name": "Cherise",
"last_name": "Stark",
"email": "rero.sonar+user-shared@gmail.com",
"password": "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvfR3m1mNVfsHgfgA",
"role": "user",
"birth_date": "1984-04-20",
"street": "Rasenstrasse 2",
"postal_code": "4577",
"city": "Hessigkofen",
"phone": "+41323666556",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/shared"
}
},
{
"first_name": "Tammy",
"last_name": "Davey",
"email": "rero.sonar+admin-unifr@gmail.com",
"password": "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvfR3m1mNVfsHgfgA",
"role": "admin",
"birth_date": "1971-12-01",
"street": "Brunnacherstrasse 112",
"postal_code": "5330",
"city": "Zurzach",
"phone": "+41625037543",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/unifr"
}
},
{
"first_name": "Nick",
"last_name": "Whiteley",
"email": "rero.sonar+moderator-unifr@gmail.com",
"password": "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvfR3m1mNVfsHgfgA",
"role": "moderator",
"birth_date": "1964-10-10",
"street": "Lichtmattstrasse 123",
"postal_code": "5736",
"city": "Burg",
"phone": "+41627676171",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/unifr"
}
},
{
"first_name": "James",
"last_name": "Keller",
"email": "rero.sonar+submitter-unifr@gmail.com",
"password": "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvfR3m1mNVfsHgfgA",
"role": "submitter",
"birth_date": "1983-09-14",
"street": "Kirchstrasse 57",
"postal_code": "1660",
"city": "La Lécherette",
"phone": "+41269639084",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/unifr"
}
},
{
"first_name": "Aya",
"last_name": "Cochran",
"email": "rero.sonar+user-unifr@gmail.com",
"password": "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvfR3m1mNVfsHgfgA",
"role": "user",
"birth_date": "1984-04-20",
"street": "Rasenstrasse 2",
"postal_code": "4577",
"city": "Hessigkofen",
"phone": "+41323666556",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/unifr"
}
}
]
33 changes: 27 additions & 6 deletions sonar/json_schemas/deposits_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,35 @@ def process(self):
"""
schema = super().process()

if not current_user_record or current_user_record.is_moderator:
organisation = {}
if current_user_record:
organisation = current_user_record.replace_refs() \
.get('organisation')

if not current_user_record or (current_user_record.is_moderator and \
organisation.get('isDedicated', False)):
return schema

# Remove some fields on json for the shared organisation
if not organisation.get('isDedicated', False):
# Remove fields for shared organisation
for field in [
'collections', 'customField1', 'customField2', 'customField3'
]:
schema['properties']['metadata']['properties']\
.pop(field, None)
propertiesOrder = schema['properties']['metadata']\
.get('propertiesOrder', [])
if field in propertiesOrder:
propertiesOrder.remove(field)

# Remove subdivisions field
schema['properties']['diffusion']['properties'].pop(
'subdivisions', None)
order = schema['properties']['diffusion']['propertiesOrder']
schema['properties']['diffusion']['propertiesOrder'] = [
v for v in order if v != 'subdivisions'
]
'subdivisions', None)
propertiesOrder = schema['properties']['diffusion']\
.get('propertiesOrder', [])
if 'subdivisions' in propertiesOrder:
schema['properties']['diffusion']['propertiesOrder']\
.remove('subdivisions')

return schema
56 changes: 56 additions & 0 deletions sonar/json_schemas/documents_json_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2022 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Documents JSON schema class."""

from sonar.modules.users.api import current_user_record

from .json_schema_base import JSONSchemaBase


class DocumentsJSONSchema(JSONSchemaBase):
"""JSON schema for deposits."""

def process(self):
"""Documents JSON schema custom process.
:returns: The processed schema.
"""
schema = super().process()

if not current_user_record:
return schema

# Get Organisation for the current logged user
organisation = current_user_record.replace_refs()\
.get('organisation', {})
# Remove some fields on json for the shared organisation
if not organisation.get('isDedicated', False):
for field in [
'collections', 'subdivisions', 'customField1',
'customField2', 'customField3'
]:
schema['properties'].pop(field, None)
if field in schema.get('propertiesOrder', []):
schema['propertiesOrder'].remove(field)

if not current_user_record.is_superuser:
schema['properties'].pop('organisation', None)
if 'organisation' in schema.get('propertiesOrder', []):
schema['propertiesOrder'].remove('organisation')

return schema
10 changes: 9 additions & 1 deletion sonar/json_schemas/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@
"""Factory for JSON schema."""

from .deposits_json_schema import DepositsJSONSchema
from .documents_json_schema import DocumentsJSONSchema
from .json_schema_base import JSONSchemaBase
from .organisations_json_schema import OrganisationsJSONSchema
from .projects_json_schema import ProjectsJSONSchema
from .users_json_schema import UsersJSONSchema


class JSONSchemaFactory():
"""Factory for JSON schema."""

SCHEMAS = {
'deposits': DepositsJSONSchema
'deposits': DepositsJSONSchema,
'documents': DocumentsJSONSchema,
'organisations': OrganisationsJSONSchema,
'projects': ProjectsJSONSchema,
'users': UsersJSONSchema
}

@staticmethod
Expand Down
42 changes: 42 additions & 0 deletions sonar/json_schemas/organisations_json_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2022 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Organisations JSON schema class."""

from sonar.modules.users.api import current_user_record

from .json_schema_base import JSONSchemaBase


class OrganisationsJSONSchema(JSONSchemaBase):
"""JSON schema for deposits."""

def process(self):
"""Organisations JSON schema custom process.
:returns: The processed schema.
"""
schema = super().process()

# Remove modes fields if user does not have superuser role.
if not current_user_record.is_superuser:
propertiesOrder = schema.get('propertiesOrder', []);
for field in ['isDedicated', 'isShared']:
if field in propertiesOrder:
schema['propertiesOrder'].remove(field)

return schema
44 changes: 44 additions & 0 deletions sonar/json_schemas/projects_json_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2022 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Projects JSON schema class."""

from sonar.modules.users.api import current_user_record

from .json_schema_base import JSONSchemaBase


class ProjectsJSONSchema(JSONSchemaBase):
"""JSON schema for deposits."""

def process(self):
"""Projects JSON schema custom process.
:returns: The processed schema.
"""
schema = super().process()

# Remove modes fields if user does not have superuser role.
if current_user_record and not current_user_record.is_superuser:
schema['properties']['metadata']['properties'].pop(
'organisation', None)
if 'organisation' in schema['properties']['metadata']\
['propertiesOrder']:
schema['properties']['metadata']['propertiesOrder'].remove(
'organisation')

return schema
Loading

0 comments on commit e2fef6f

Please sign in to comment.