Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def import_condition(element):
try:
condition = Condition.objects.get(uri=element['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 ''
condition.source = None
if element['source']:
try:
condition.source = Attribute.objects.get(uri=element['source'])
except Attribute.DoesNotExist:
pass
condition.relation = element['relation']
condition.target_text = element['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
try:
ConditionUniqueKeyValidator(condition).validate()
except ValidationError as e:
log.info('QuestionSet not in db. Created with uri %s.', element['uri'])
questionset = QuestionSet()
try:
questionset.section = Section.objects.get(uri=element['section'])
except Section.DoesNotExist:
log.info('Section not in db. Skipping.')
return
questionset.uri_prefix = element['uri_prefix'] or ''
questionset.key = element['key'] or ''
questionset.comment = element['comment'] or ''
if element['attribute']:
try:
questionset.attribute = Attribute.objects.get(uri=element['attribute'])
except Attribute.DoesNotExist:
pass
questionset.is_collection = element['is_collection']
questionset.order = element['order']
for lang_code, lang_string, lang_field in get_languages():
set_lang_field(questionset, 'title', element, lang_code, lang_field)
set_lang_field(questionset, 'help', element, lang_code, lang_field)
set_lang_field(questionset, 'verbose_name', element, lang_code, lang_field)
set_lang_field(questionset, 'verbose_name_plural', element, lang_code, lang_field)
try:
QuestionSetUniquePathValidator(questionset).validate()
except ValidationError as e:
log.info('QuestionSet not saving "%s" due to validation error (%s).', element['uri'], e)
task = Task.objects.get(uri=element['uri'])
except Task.DoesNotExist:
log.info('Task not in db. Created with uri %s.', element['uri'])
task = Task()
task.uri_prefix = element['uri_prefix'] or ''
task.key = element['key'] or ''
task.comment = element['comment'] or ''
for lang_code, lang_string, lang_field in get_languages():
set_lang_field(task, 'title', element, lang_code, lang_field)
set_lang_field(task, 'text', element, lang_code, lang_field)
if element['start_attribute']:
try:
task.start_attribute = Attribute.objects.get(uri=element['start_attribute'])
except Attribute.DoesNotExist:
pass
if element['end_attribute']:
try:
task.end_attribute = Attribute.objects.get(uri=element['end_attribute'])
except Attribute.DoesNotExist:
pass
task.days_before = element['days_before']
task.days_after = element['days_after']
try:
TaskUniqueKeyValidator(task).validate()
except ValidationError as e:
log.info('Task not saving "%s" due to validation error (%s).', element['uri'], e)