-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
10 Add RDM->CLM CLI migration path #18
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
59854fc
Fix table and column names #10
johnatawnclementawn 6b14269
Add rdm to clm CLI migration path
johnatawnclementawn 5e4cae8
Merge branch 'main' into jmc/10_add_rdm_to_clm_migrator_cli
johnatawnclementawn 7b94313
Merge branch 'main' into jmc/10_add_rdm_to_clm_migrator_cli
johnatawnclementawn 0531901
Un-americanize test data
johnatawnclementawn 6b58d0f
Remove unused imports
johnatawnclementawn 51b40b1
Separate args to path.join
johnatawnclementawn 79e7b5d
Assert against total list items, rather than uuid
johnatawnclementawn 667df5b
Add test for no matching collection
johnatawnclementawn c1e02eb
Avoid piping both streams to same output
jacobtylerwalls eeddc4b
Remove unused import
jacobtylerwalls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
arches_references/management/commands/controlled_lists.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,120 @@ | ||
from arches.app.models.models import Value | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Commands for running controlled list operations | ||
|
||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"-o", | ||
"--operation", | ||
action="store", | ||
dest="operation", | ||
required=True, | ||
choices=["migrate_collections_to_controlled_lists"], | ||
help="The operation to perform", | ||
) | ||
|
||
parser.add_argument( | ||
"-co", | ||
"--collections", | ||
action="store", | ||
dest="collections_to_migrate", | ||
nargs="*", | ||
required=True, | ||
help="One or more collections to migrate to controlled lists", | ||
) | ||
|
||
parser.add_argument( | ||
"-ho", | ||
"--host", | ||
action="store", | ||
dest="host", | ||
default="http://localhost:8000/plugins/controlled-list-manager/item/", | ||
help="Provide a host for URI generation. Default is localhost", | ||
) | ||
|
||
parser.add_argument( | ||
"-ow", | ||
"--overwrite", | ||
action="store_true", | ||
dest="overwrite", | ||
default=False, | ||
help="Overwrite the entire controlled list and its list items/values. Default false.", | ||
) | ||
|
||
parser.add_argument( | ||
"-psl", | ||
"--preferred_sort_language", | ||
action="store", | ||
dest="preferred_sort_language", | ||
default="en", | ||
help="The language to use for sorting preferred labels. Default 'en'", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
if options["operation"] == "migrate_collections_to_controlled_lists": | ||
self.migrate_collections_to_controlled_lists( | ||
collections_to_migrate=options["collections_to_migrate"], | ||
host=options["host"], | ||
overwrite=options["overwrite"], | ||
preferred_sort_language=options["preferred_sort_language"], | ||
) | ||
|
||
def migrate_collections_to_controlled_lists( | ||
self, | ||
collections_to_migrate, | ||
host, | ||
overwrite, | ||
preferred_sort_language, | ||
): | ||
""" | ||
Uses a postgres function to migrate collections to controlled lists | ||
|
||
Example usage: | ||
python manage.py controlled_lists | ||
-o migrate_collections_to_controlled_lists | ||
-co 'Johns list' 'Getty AAT' | ||
-ho 'http://localhost:8000/plugins/controlled-list-manager/item/' | ||
-psl 'fr' | ||
-ow | ||
""" | ||
|
||
collections_in_db = list( | ||
Value.objects.filter( | ||
value__in=collections_to_migrate, | ||
valuetype__in=["prefLabel", "identifier"], | ||
concept__nodetype="Collection", | ||
).values_list("value", flat=True) | ||
) | ||
|
||
failed_collections = [ | ||
collection | ||
for collection in collections_to_migrate | ||
if collection not in collections_in_db | ||
] | ||
|
||
if len(failed_collections) > 0: | ||
self.stderr.write( | ||
"Failed to find the following collections in the database: %s" | ||
% ", ".join(failed_collections) | ||
) | ||
|
||
if len(collections_in_db) > 0: | ||
from django.db import connection | ||
|
||
cursor = connection.cursor() | ||
cursor.execute( | ||
""" | ||
select * from __arches_migrate_collections_to_clm( | ||
ARRAY[%s], %s, %s::boolean, %s | ||
); | ||
""", | ||
[collections_in_db, host, overwrite, preferred_sort_language], | ||
) | ||
result = cursor.fetchone() | ||
self.stdout.write(result[0]) |
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
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
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,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<rdf:RDF | ||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
xmlns:skos="http://www.w3.org/2004/02/skos/core#" | ||
xmlns:dcterms="http://purl.org/dc/terms/" | ||
> | ||
<skos:ConceptScheme rdf:about="http://www.archesproject.org/7c90899a-dbe9-4574-9175-e69481a80b3c"> | ||
<skos:hasTopConcept> | ||
<skos:Concept rdf:about="http://www.archesproject.org/0fc72a30-2ead-4afd-9e7a-a5067d2cc5c8"> | ||
<skos:inScheme rdf:resource="http://www.archesproject.org/7c90899a-dbe9-4574-9175-e69481a80b3c"/> | ||
<skos:prefLabel xml:lang="en">{"id": "7949d7b5-6e57-469a-8f38-87aac08e1788", "value": "Test Concept 2"}</skos:prefLabel> | ||
</skos:Concept> | ||
</skos:hasTopConcept> | ||
<skos:hasTopConcept> | ||
<skos:Concept rdf:about="http://www.archesproject.org/6490ac87-ac80-41d7-a135-1119b4cd912d"> | ||
<skos:prefLabel xml:lang="en">{"id": "fad6f17d-f7c8-4fa1-b358-e8626571599e", "value": "Test Concept 3"}</skos:prefLabel> | ||
<skos:inScheme rdf:resource="http://www.archesproject.org/7c90899a-dbe9-4574-9175-e69481a80b3c"/> | ||
</skos:Concept> | ||
</skos:hasTopConcept> | ||
<skos:hasTopConcept> | ||
<skos:Concept rdf:about="http://www.archesproject.org/89ff530a-f350-44f0-ac88-bdd8904eb57e"> | ||
<skos:inScheme rdf:resource="http://www.archesproject.org/7c90899a-dbe9-4574-9175-e69481a80b3c"/> | ||
<skos:prefLabel xml:lang="en">{"id": "9fa56006-6828-480f-8395-ad5c5a84726b", "value": "Test Concept 1"}</skos:prefLabel> | ||
</skos:Concept> | ||
</skos:hasTopConcept> | ||
<dcterms:title xml:lang="en">{"id": "f5e1a756-c658-4a3c-bc3a-e9293242e8f7", "value": "Concept Label Import Test"}</dcterms:title> | ||
</skos:ConceptScheme> | ||
</rdf:RDF> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. In the future we can consider implementing the
Func
expressions API to be able to use custom db functions with the ORM.