Update cds-videos records _access.update with e-groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
from invenio_db import db | |
from invenio_pidstore.models import PersistentIdentifier | |
from invenio_search import current_search | |
from invenio_search.utils import schema_to_index | |
from cds.modules.records.api import CDSRecord | |
from cds.modules.deposit.api import Project, Video | |
from cds.modules.fixtures.cli import _load_json_source | |
from cds.modules.deposit.resolver import get_video_pid | |
def _mapper(total, current): | |
total[current['name']]=current['access'].get('responsible', []) | |
return total | |
def update_cern_records(): | |
pids = PersistentIdentifier.query.filter_by(pid_type='depid') | |
for pid in pids: | |
if (pid.is_deleted()): | |
continue | |
record = CDSRecord.get_record(pid.object_uuid) | |
category = record.get('category') | |
if category is None or category != 'CERN': | |
continue | |
_, doctype = schema_to_index(record['$schema'], index_names=current_search.mappings.keys()) | |
if doctype.startswith('project'): | |
deposit = Project.get_record(pid.object_uuid) | |
is_published = deposit.is_published() | |
if is_published: | |
deposit = deposit.edit() | |
deposit.setdefault('_access', {}).setdefault('update',[]).extend(['vmo-restictedrights@cern.ch']) | |
deposit['_access']['update'] = list(set(deposit['_access']['update'])) | |
deposit.update() | |
if is_published: | |
deposit = deposit.publish() | |
deposit.commit() | |
db.session.commit() | |
def update_records(): | |
categories = _load_json_source('categories.json') | |
categories = categories = reduce(_mapper, categories, {}) | |
pids = PersistentIdentifier.query.filter_by(pid_type='depid') | |
for pid in pids: | |
if (pid.is_deleted()): | |
continue | |
record = CDSRecord.get_record(pid.object_uuid) | |
category = record.get('category') | |
if category is None or category == 'CERN': | |
continue | |
_, doctype = schema_to_index(record['$schema'], index_names=current_search.mappings.keys()) | |
if doctype.startswith('project'): | |
deposit = Project.get_record(pid.object_uuid) | |
is_published = deposit.is_published() | |
if is_published: | |
deposit = deposit.edit() | |
deposit.setdefault('_access', {}).setdefault('update',[]).extend(categories.get(record['category'])) | |
deposit['_access']['update'] = list(set(deposit['_access']['update'])) | |
deposit = deposit.update() | |
if is_published: | |
deposit = deposit.publish() | |
deposit.commit() | |
db.session.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment