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_task(element):
try:
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']
def render_question(self, xml, question):
xml.startElement('question', {'dc:uri': question['uri']})
self.render_text_element(xml, 'uri_prefix', {}, question['uri_prefix'])
self.render_text_element(xml, 'key', {}, question['key'])
self.render_text_element(xml, 'path', {}, question['path'])
self.render_text_element(xml, 'dc:comment', {}, question['comment'])
self.render_text_element(xml, 'attribute', {'dc:uri': question['attribute']}, None)
self.render_text_element(xml, 'questionset', {'dc:uri': question['questionset']}, None)
self.render_text_element(xml, 'is_collection', {}, question['is_collection'])
self.render_text_element(xml, 'order', {}, question['order'])
for lang_code, lang_string, lang_field in get_languages():
self.render_text_element(xml, 'help', {'lang': lang_code}, question['help_%s' % lang_code])
self.render_text_element(xml, 'text', {'lang': lang_code}, question['text_%s' % lang_code])
self.render_text_element(xml, 'verbose_name', {'lang': lang_code}, question['verbose_name_%s' % lang_code])
self.render_text_element(xml, 'verbose_name_plural', {'lang': lang_code}, question['verbose_name_plural_%s' % lang_code])
self.render_text_element(xml, 'widget_type', {}, question['widget_type'])
self.render_text_element(xml, 'value_type', {}, question['value_type'])
self.render_text_element(xml, 'maximum', {}, question['maximum'])
self.render_text_element(xml, 'minimum', {}, question['minimum'])
self.render_text_element(xml, 'step', {}, question['step'])
self.render_text_element(xml, 'unit', {}, question['unit'])
xml.startElement('optionsets', {})
if 'optionsets' in question and question['optionsets']:
for optionset in question['optionsets']:
self.render_text_element(xml, 'optionset', {'dc:uri': optionset}, None)
def trans(self, field):
current_language = get_language()
languages = get_languages()
for lang_code, lang_string, lang_field in languages:
if lang_code == current_language:
return getattr(self, '%s_%s' % (field, lang_field)) or ''
primary_lang_field = languages[0][2]
return getattr(self, '%s_%s' % (field, primary_lang_field)) or ''
def import_catalog(element):
try:
catalog = Catalog.objects.get(uri=element['uri'])
except Catalog.DoesNotExist:
log.info('Catalog not in db. Created with uri %s.', element['uri'])
catalog = Catalog()
catalog.uri_prefix = element['uri_prefix'] or ''
catalog.key = element['key'] or ''
catalog.comment = element['comment'] or ''
catalog.order = element['order']
for lang_code, lang_string, lang_field in get_languages():
set_lang_field(catalog, 'title', element, lang_code, lang_field)
try:
CatalogUniqueKeyValidator(catalog).validate()
except ValidationError as e:
log.info('Catalog not saving "%s" due to validation error (%s).', element['uri'], e)
pass
else:
log.info('Catalog saving to "%s".', element['uri'])
catalog.save()
catalog.sites.add(Site.objects.get_current())
def render_section(self, xml, section):
xml.startElement('section', {'dc:uri': section['uri']})
self.render_text_element(xml, 'uri_prefix', {}, section['uri_prefix'])
self.render_text_element(xml, 'key', {}, section['key'])
self.render_text_element(xml, 'path', {}, section['path'])
self.render_text_element(xml, 'dc:comment', {}, section['comment'])
self.render_text_element(xml, 'catalog', {'dc:uri': section['catalog']}, None)
self.render_text_element(xml, 'order', {}, section['order'])
for lang_code, lang_string, lang_field in get_languages():
self.render_text_element(xml, 'title', {'lang': lang_code}, section['title_%s' % lang_code])
xml.endElement('section')
if 'questionsets' in section and section['questionsets']:
for questionset in section['questionsets']:
self.render_questionset(xml, questionset)
log.info('Section not in db. Created with uri %s.', element['uri'])
section = Section()
try:
section.catalog = Catalog.objects.get(uri=element['catalog'])
except Catalog.DoesNotExist:
log.info('Catalog not in db. Skipping.')
return
section.uri_prefix = element['uri_prefix'] or ''
section.key = element['key'] or ''
section.comment = element['comment'] or ''
section.order = element['order']
for lang_code, lang_string, lang_field in get_languages():
set_lang_field(section, 'title', element, lang_code, lang_field)
try:
SectionUniquePathValidator(section).validate()
except ValidationError as e:
log.info('Section not saving "%s" due to validation error (%s).', element['uri'], e)
pass
else:
log.info('Section saving to "%s".', element['uri'])
section.save()
def render_task(self, xml, task):
xml.startElement('task', {'dc:uri': task['uri']})
self.render_text_element(xml, 'uri_prefix', {}, task['uri_prefix'])
self.render_text_element(xml, 'key', {}, task['key'])
self.render_text_element(xml, 'dc:comment', {}, task['comment'])
for lang_code, lang_string, lang_field in get_languages():
self.render_text_element(xml, 'title', {'lang': lang_code}, task['title_%s' % lang_code])
self.render_text_element(xml, 'text', {'lang': lang_code}, task['text_%s' % lang_code])
self.render_text_element(xml, 'start_attribute', {'dc:uri': task['start_attribute']}, None)
self.render_text_element(xml, 'end_attribute', {'dc:uri': task['end_attribute']}, None)
self.render_text_element(xml, 'days_before', {}, task['days_before'])
self.render_text_element(xml, 'days_after', {}, task['days_after'])
xml.startElement('conditions', {})
if 'conditions' in task and task['conditions']:
for condition in task['conditions']:
self.render_text_element(xml, 'condition', {'dc:uri': condition}, None)
xml.endElement('conditions')
xml.endElement('task')
def render_catalog(self, xml, catalog):
xml.startElement('catalog', {'dc:uri': catalog['uri']})
self.render_text_element(xml, 'uri_prefix', {}, catalog['uri_prefix'])
self.render_text_element(xml, 'key', {}, catalog['key'])
self.render_text_element(xml, 'dc:comment', {}, catalog['comment'])
self.render_text_element(xml, 'order', {}, catalog['order'])
for lang_code, lang_string, lang_field in get_languages():
self.render_text_element(xml, 'title', {'lang': lang_code}, catalog['title_%s' % lang_code])
xml.endElement('catalog')
if 'sections' in catalog and catalog['sections']:
for section in catalog['sections']:
self.render_section(xml, section)