-
Notifications
You must be signed in to change notification settings - Fork 47
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
160 changed files
with
9,026 additions
and
4,880 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
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 |
---|---|---|
@@ -1,57 +1,36 @@ | ||
import logging | ||
|
||
from django.core.exceptions import ValidationError | ||
|
||
from rdmo.core.xml import flat_xml_to_elements, filter_elements_by_type | ||
from rdmo.core.imports import (get_foreign_field, set_common_fields, | ||
validate_instance) | ||
from rdmo.domain.models import Attribute | ||
from rdmo.options.models import Option | ||
|
||
from .models import Condition | ||
from .validators import ConditionUniqueKeyValidator | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def import_conditions(root): | ||
elements = flat_xml_to_elements(root) | ||
logger = logging.getLogger(__name__) | ||
|
||
for element in filter_elements_by_type(elements, 'condition'): | ||
import_condition(element) | ||
|
||
|
||
def import_condition(element): | ||
def import_condition(element, save=False): | ||
try: | ||
condition = Condition.objects.get(uri=element['uri']) | ||
condition = Condition.objects.get(uri=element.get('uri')) | ||
except Condition.DoesNotExist: | ||
log.info('Condition not in db. Created with uri %s.', element['uri']) | ||
condition = Condition() | ||
|
||
condition.uri_prefix = element['uri_prefix'] or '' | ||
condition.key = element['key'] or '' | ||
condition.comment = element['comment'] or '' | ||
set_common_fields(condition, element) | ||
|
||
condition.source = None | ||
if element['source']: | ||
try: | ||
condition.source = Attribute.objects.get(uri=element['source']) | ||
except Attribute.DoesNotExist: | ||
pass | ||
condition.source = get_foreign_field(condition, element.get('source'), Attribute) | ||
condition.target_option = get_foreign_field(condition, element.get('target_option'), Option) | ||
|
||
condition.relation = element['relation'] | ||
condition.target_text = element['target_text'] or '' | ||
condition.relation = element.get('relation') | ||
condition.target_text = element.get('target_text') or '' | ||
|
||
condition.target_option = None | ||
if element['target_option']: | ||
try: | ||
condition.target_option = Option.objects.get(uri=element['target_option']) | ||
except Option.DoesNotExist: | ||
pass | ||
if save and validate_instance(condition): | ||
if condition.id: | ||
logger.info('Catalog created with uri %s.', element.get('uri')) | ||
else: | ||
logger.info('Catalog %s updated.', element.get('uri')) | ||
|
||
try: | ||
ConditionUniqueKeyValidator(condition).validate() | ||
except ValidationError as e: | ||
log.info('Condition not saving "%s" due to validation error (%s).', element['uri'], e) | ||
pass | ||
else: | ||
log.info('Condition saving to "%s".', element['uri']) | ||
condition.save() | ||
condition.imported = True | ||
|
||
return condition |
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,24 @@ | ||
# Generated by Django 2.2.13 on 2020-08-24 11:47 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('conditions', '0020_require_uri_prefix'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='condition', | ||
name='source', | ||
field=models.ForeignKey(blank=True, db_constraint=False, help_text='The attribute of the value for this condition.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='conditions', to='domain.Attribute', verbose_name='Source'), | ||
), | ||
migrations.AlterField( | ||
model_name='condition', | ||
name='target_option', | ||
field=models.ForeignKey(blank=True, db_constraint=False, help_text='If using a value pointing to an option, the option this condition is checking against.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='conditions', to='options.Option', verbose_name='Target (Option)'), | ||
), | ||
] |
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
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
Oops, something went wrong.