From 86677350e6d45302f154d39258ed557337b3673c Mon Sep 17 00:00:00 2001 From: Mark Calvert Date: Wed, 18 May 2022 13:51:34 +1200 Subject: [PATCH 1/2] Squashed commit of the following: commit 6afa95f873c350ae1aa4da2623961c40c60a75ec Author: Mark Calvert Date: Wed May 18 13:48:00 2022 +1200 [SUPDESQ-35] Refactored `vocabulary_service_term_upsert` code Updated existing_term to use label and uri has the key for allow_duplicate_terms Refactored code for existing terms to update if there are any changes commit e7a51aa422d9d4217f18e3c1479d58daee5d6131 Merge: 41ab3bc cbd8123 Author: Mark Calvert Date: Wed May 18 13:43:11 2022 +1200 Merge branch 'develop' into feature/SUPDESQ-35-quantity_kind commit 41ab3bcffeb5c6f4ebdb4b4a02130c70d4d798fe Merge: 36d1aa0 dcbb949 Author: Mark Calvert Date: Mon Dec 20 10:43:57 2021 +1300 Merge branch 'develop' into feature/SUPDESQ-35-quantity_kind commit 36d1aa0bcdedf58250324b14b1f7393cbb59a420 Author: awang setyawan Date: Mon Dec 6 07:29:26 2021 +0700 [SUPDESQ-35] - added support for quantity kind commit f5c2029d499a2430a6b38cea3c819bc87366dd4b Author: awang setyawan Date: Sat Dec 18 11:15:59 2021 +0700 [SUPDESQ-34] remove update db cmd commit 7774cb21328038429281e409f4ca35d029a266b6 Author: awang setyawan Date: Sat Dec 18 11:09:26 2021 +0700 [SUPDESQ-34] fix updated screen and prevent secure vocab to be visible commit 8209c655679ec3a3443c6319dab298b49a825e89 Author: awang setyawan Date: Sat Dec 4 20:16:45 2021 +0700 [SUPDESQ-34] - fix downgrade command commit 4359f4ec9607234e7f0daa49b6cb32971500af70 Author: awang setyawan Date: Wed Dec 1 08:06:59 2021 +0700 [SUPDESQ-34] - added migration script commit 8a80db6ad34d0fba72c9427158eb76831785a3f7 Author: awang setyawan Date: Sun Nov 28 07:55:41 2021 +0700 [SUPDESQ-34] - enable select2 autocomplete and fix field group dropdown commit 4fd90e847cf0985c7f691330a35d4d1d7dcda4a4 Author: awang setyawan Date: Tue Nov 16 12:46:48 2021 +0700 [SUPDESQ-34] updated option and added validator commit 9081b74c8dbfbef4f50f56eeda1eaddbb9120303 Author: awang setyawan Date: Mon Nov 15 12:49:09 2021 +0700 [SUPDESQ-34] added schema and linked field to vocab commit 84130cfb3af948e531c12da1438b0022f1504e17 Author: awang setyawan Date: Tue Nov 2 15:01:30 2021 +0700 [SUPDESQ-34] added new col and its cli comand --- .../logic/action/create.py | 61 ++++++------------- ckanext/vocabulary_services/model.py | 18 +++--- 2 files changed, 29 insertions(+), 50 deletions(-) diff --git a/ckanext/vocabulary_services/logic/action/create.py b/ckanext/vocabulary_services/logic/action/create.py index b208365..4c3616d 100644 --- a/ckanext/vocabulary_services/logic/action/create.py +++ b/ckanext/vocabulary_services/logic/action/create.py @@ -72,52 +72,31 @@ def vocabulary_service_term_upsert(context, data_dict): if vocabulary_service_id and label and uri: existing_term = None - - if VocabularyService.is_allow_duplicate_terms(vocabulary_service_id): - # Load any term for the given vocabulary_service.id with matching uri - existing_term = VocabularyServiceTerm.get_by_uri(vocabulary_service_id, uri) + allow_duplicate_terms = VocabularyService.is_allow_duplicate_terms(vocabulary_service_id) + if allow_duplicate_terms: + # Load any term for the given vocabulary_service.id with matching label AND uri + existing_term = VocabularyServiceTerm.get_by_label_and_uri(vocabulary_service_id, label, uri) else: # Load any term for the given vocabulary_service.id with matching label OR uri existing_term = VocabularyServiceTerm.get_by_label_or_uri(vocabulary_service_id, label, uri) if existing_term: - # If duplicate terms are allowed. - if VocabularyService.is_allow_duplicate_terms(vocabulary_service_id): - if (existing_term.label == label) and (existing_term.uri != uri): - # If label is the same but uri is different, let's create them. - vocabulary_service_term_create(context, data_dict) - elif (existing_term.label != label - or existing_term.definition != definition - or existing_term.broader != broader) and existing_term.uri == uri: - # Update the term label if the URI is the same and label different. - # Update the term definition if the URI is the same and definition different. - existing_term.label = label - existing_term.broader = broader - existing_term.definition = definition - existing_term.quantity_kind = quantity_kind - existing_term.date_modified = datetime.utcnow() - - session.add(existing_term) - session.commit() - else: - return True - else: - # Check if something has changed - if so, update it, otherwise skip it... - if existing_term.label != label \ - or existing_term.uri != uri \ - or existing_term.definition != definition \ - or existing_term.quantity_kind != quantity_kind \ - or existing_term.broader != broader: - # Update the term - existing_term.label = label - existing_term.uri = uri - existing_term.broader = broader - existing_term.definition = definition - existing_term.quantity_kind = quantity_kind - existing_term.date_modified = datetime.utcnow() - - session.add(existing_term) - session.commit() + # Check if something has changed - if so, update it, otherwise skip it... + if (existing_term.label != label + or existing_term.uri != uri + or existing_term.definition != definition + or existing_term.broader != broader + or existing_term.quantity_kind != quantity_kind): + # Update the term + existing_term.label = label + existing_term.uri = uri + existing_term.broader = broader + existing_term.definition = definition + existing_term.quantity_kind = quantity_kind + existing_term.date_modified = datetime.utcnow() + + session.add(existing_term) + session.commit() else: vocabulary_service_term_create(context, data_dict) diff --git a/ckanext/vocabulary_services/model.py b/ckanext/vocabulary_services/model.py index df3cbcb..a4fb83a 100644 --- a/ckanext/vocabulary_services/model.py +++ b/ckanext/vocabulary_services/model.py @@ -5,7 +5,7 @@ from sqlalchemy import types, Column, Table, func, ForeignKey from ckan.model.domain_object import DomainObject from sqlalchemy.orm import relation -from sqlalchemy import or_ +from sqlalchemy import or_, and_ vocabulary_service_table = Table('vocabulary_service', meta.metadata, @@ -103,10 +103,10 @@ def schema_and_linked_schema_field_and_name_exists(cls, schema, linked_schema_fi '''Returns true if there is a vocabulary with the same schema and linked_schema_field (case insensitive)''' query = meta.Session.query(cls) return query\ - .filter(func.lower(cls.schema) == func.lower(schema))\ - .filter(func.lower(cls.linked_schema_field) == func.lower(linked_schema_field))\ - .filter(func.lower(cls.name) == func.lower(name))\ - .first() is not None + .filter(func.lower(cls.schema) == func.lower(schema))\ + .filter(func.lower(cls.linked_schema_field) == func.lower(linked_schema_field))\ + .filter(func.lower(cls.name) == func.lower(name))\ + .first() is not None @classmethod def get_by_name(cls, name): @@ -143,7 +143,7 @@ def get(cls, reference): @classmethod def get_by_label_or_uri(cls, vocabulary_service_id, label, uri): - '''Returns a VocabularyServiceTerm object referenced by its id.''' + '''Returns a VocabularyServiceTerm object referenced by its label or uri.''' query = meta.Session.query(cls)\ .filter(cls.vocabulary_service_id == vocabulary_service_id)\ .filter(or_(func.lower(cls.label) == func.lower(label), func.lower(cls.uri) == func.lower(uri))) @@ -152,11 +152,11 @@ def get_by_label_or_uri(cls, vocabulary_service_id, label, uri): return vocabulary_service_term @classmethod - def get_by_uri(cls, vocabulary_service_id, uri): - '''Returns a VocabularyServiceTerm object referenced by its uri.''' + def get_by_label_and_uri(cls, vocabulary_service_id, label, uri): + '''Returns a VocabularyServiceTerm object referenced by its label and uri.''' query = meta.Session.query(cls)\ .filter(cls.vocabulary_service_id == vocabulary_service_id)\ - .filter(func.lower(cls.uri) == func.lower(uri)) + .filter(and_(func.lower(cls.label) == func.lower(label), func.lower(cls.uri) == func.lower(uri))) vocabulary_service_term = query.first() return vocabulary_service_term From b250df4253b5e04710626fcc75bde950b95e5575 Mon Sep 17 00:00:00 2001 From: Mark Calvert Date: Thu, 19 May 2022 12:17:19 +1200 Subject: [PATCH 2/2] Added quantity_kind field value to action vocabulary_service_term_create --- ckanext/vocabulary_services/logic/action/create.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ckanext/vocabulary_services/logic/action/create.py b/ckanext/vocabulary_services/logic/action/create.py index 4c3616d..c1173df 100644 --- a/ckanext/vocabulary_services/logic/action/create.py +++ b/ckanext/vocabulary_services/logic/action/create.py @@ -48,7 +48,8 @@ def vocabulary_service_term_create(context, data_dict): label=data_dict.get('label', ''), uri=data_dict.get('uri', ''), broader=data_dict.get('broader', ''), - definition=data_dict.get('definition', '') + definition=data_dict.get('definition', ''), + quantity_kind=data_dict.get('quantity_kind', '') ) session.add(term) @@ -82,11 +83,11 @@ def vocabulary_service_term_upsert(context, data_dict): if existing_term: # Check if something has changed - if so, update it, otherwise skip it... - if (existing_term.label != label - or existing_term.uri != uri - or existing_term.definition != definition - or existing_term.broader != broader - or existing_term.quantity_kind != quantity_kind): + if (existing_term.label != label or + existing_term.uri != uri or + existing_term.definition != definition or + existing_term.broader != broader or + existing_term.quantity_kind != quantity_kind): # Update the term existing_term.label = label existing_term.uri = uri