Skip to content

Commit

Permalink
Squashed commit of the following: (#38)
Browse files Browse the repository at this point in the history
commit 6afa95f
Author: Mark Calvert <[email protected]>
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 e7a51aa
Merge: 41ab3bc cbd8123
Author: Mark Calvert <[email protected]>
Date:   Wed May 18 13:43:11 2022 +1200

    Merge branch 'develop' into feature/SUPDESQ-35-quantity_kind

commit 41ab3bc
Merge: 36d1aa0 dcbb949
Author: Mark Calvert <[email protected]>
Date:   Mon Dec 20 10:43:57 2021 +1300

    Merge branch 'develop' into feature/SUPDESQ-35-quantity_kind

commit 36d1aa0
Author: awang setyawan <[email protected]>
Date:   Mon Dec 6 07:29:26 2021 +0700

    [SUPDESQ-35] - added support for quantity kind

commit f5c2029
Author: awang setyawan <[email protected]>
Date:   Sat Dec 18 11:15:59 2021 +0700

    [SUPDESQ-34] remove update db cmd

commit 7774cb2
Author: awang setyawan <[email protected]>
Date:   Sat Dec 18 11:09:26 2021 +0700

    [SUPDESQ-34] fix updated screen and prevent secure vocab to be visible

commit 8209c65
Author: awang setyawan <[email protected]>
Date:   Sat Dec 4 20:16:45 2021 +0700

    [SUPDESQ-34] - fix downgrade command

commit 4359f4e
Author: awang setyawan <[email protected]>
Date:   Wed Dec 1 08:06:59 2021 +0700

    [SUPDESQ-34] - added migration script

commit 8a80db6
Author: awang setyawan <[email protected]>
Date:   Sun Nov 28 07:55:41 2021 +0700

    [SUPDESQ-34] - enable select2 autocomplete and fix field group dropdown

commit 4fd90e8
Author: awang setyawan <[email protected]>
Date:   Tue Nov 16 12:46:48 2021 +0700

    [SUPDESQ-34] updated option and added validator

commit 9081b74
Author: awang setyawan <[email protected]>
Date:   Mon Nov 15 12:49:09 2021 +0700

    [SUPDESQ-34] added schema and linked field to vocab

commit 84130cf
Author: awang setyawan <[email protected]>
Date:   Tue Nov 2 15:01:30 2021 +0700

    [SUPDESQ-34] added new col and its cli comand
  • Loading branch information
MarkCalvert authored May 18, 2022
1 parent cbd8123 commit b68f9bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 50 deletions.
61 changes: 20 additions & 41 deletions ckanext/vocabulary_services/logic/action/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 9 additions & 9 deletions ckanext/vocabulary_services/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)))
Expand All @@ -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
Expand Down

0 comments on commit b68f9bb

Please sign in to comment.