This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
439 additions
and
87 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
src/metax_api/management/commands/create_missing_rems_items.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from metax_api.services.redis_cache_service import RedisClient | ||
from metax_api.utils import ReferenceDataLoader | ||
from metax_api.models import CatalogRecordV2 | ||
from metax_api.services.rems_service import REMSCatalogItemNotFoundException, REMSService | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
class Command(BaseCommand): | ||
def handle(self, *args, **options): | ||
rems_service = REMSService() | ||
found_entity_count = 0 | ||
created_entity_count = 0 | ||
missing_entity_count = 0 | ||
try: | ||
rems_crs = CatalogRecordV2.objects.filter(rems_identifier__isnull=False) | ||
_logger.info(f"Found {len(rems_crs)} CatalogRecords with rems_identifiers") | ||
for cr in rems_crs: | ||
try: | ||
rems_service.get_rems_entity(cr) | ||
found_entity_count += 1 | ||
except REMSCatalogItemNotFoundException as e: | ||
missing_entity_count += 1 | ||
if not cr.access_granter: | ||
_logger.info( | ||
f"Missing access_granter for {cr.identifier}, not creating REMS entity" | ||
) | ||
continue | ||
if len(cr.research_dataset.get("access_rights", {}).get("license") or []) == 0: | ||
_logger.info( | ||
f"Missing license for {cr.identifier}, not creating REMS entity" | ||
) | ||
continue | ||
|
||
_logger.info( | ||
f"REMS entity {cr.rems_identifier} for dataset {cr.identifier} not found, creating" | ||
) | ||
rems_service.create_rems_entity(cr, cr.access_granter) | ||
created_entity_count += 1 | ||
|
||
except Exception as e: | ||
_logger.error(e) | ||
raise e | ||
|
||
_logger.info(f"CatalogRecords with existing REMS entities: {found_entity_count}") | ||
_logger.info(f"Missing REMS entities: {missing_entity_count}") | ||
_logger.info(f"Created REMS entities: {created_entity_count}") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django.db import migrations | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def replace_metadata_provider_user(cr, old_user, new_user): | ||
logger.info(f"replacing metadata_provider_user: {old_user} with new_user: {new_user}") | ||
if cr.metadata_provider_user: | ||
if cr.metadata_provider_user == old_user: | ||
cr.metadata_provider_user = new_user | ||
logger.info("metadata_provider_user changed") | ||
|
||
def change_metadata_provider_user(apps, schema_editor): | ||
new_user = "frickmar" | ||
old_user = "[email protected]" | ||
CatalogRecord = apps.get_model('metax_api', 'CatalogRecord') | ||
crs = CatalogRecord.objects.filter(metadata_provider_user=old_user) | ||
logger.info(f"Found {len(crs)} catalog records to update") | ||
for cr in crs: | ||
try: | ||
logger.info(f"changing metadata_provider_user for cr {cr.identifier}") | ||
replace_metadata_provider_user(cr, old_user, new_user) | ||
# cr.editor_permissions.user_id = "asdf" | ||
cr.save() | ||
cr.editor_permissions.users.update(user_id=new_user) | ||
logger.info("cr save successful") | ||
except Exception as e: | ||
logger.error(e) | ||
|
||
|
||
def revert(apps, schema_editor): | ||
new_user = "frickmar" | ||
old_user = "[email protected]" | ||
CatalogRecord = apps.get_model('metax_api', 'CatalogRecord') | ||
crs = CatalogRecord.objects.filter(metadata_provider_user=new_user) | ||
logger.info(f"Found {len(crs)} catalog records to update") | ||
for cr in crs: | ||
try: | ||
logger.info(f"changing metadata_provider_user for cr {cr.identifier}") | ||
replace_metadata_provider_user(cr, new_user, old_user) | ||
# cr.editor_permissions.user_id = old_user | ||
cr.save() | ||
cr.editor_permissions.users.update(user_id=old_user) | ||
logger.info("cr save successful") | ||
except Exception as e: | ||
logger.error(e) | ||
logger.error(e) | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('metax_api', '0045_add_publish_fields_to_catalogs'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(change_metadata_provider_user, revert), | ||
] |
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
Oops, something went wrong.