def import_question(element, questionset_uri=False, save=False): try: if questionset_uri is False: question = Question.objects.get(uri=element.get('uri')) else: question = Question.objects.get(key=element.get('key'), questionset__uri=questionset_uri) except Question.DoesNotExist: question = Question() set_common_fields(question, element) question.questionset = get_foreign_field( question, questionset_uri or element.get('questionset'), QuestionSet) question.attribute = get_foreign_field(question, element.get('attribute'), Attribute) question.is_collection = element.get('is_collection') or False question.is_optional = element.get('is_optional') or False question.order = element.get('order') or 0 set_lang_field(question, 'text', element) set_lang_field(question, 'help', element) set_lang_field(question, 'default_text', element) set_lang_field(question, 'verbose_name', element) set_lang_field(question, 'verbose_name_plural', element) question.default_option = get_foreign_field(question, element.get('default_option'), Option) question.default_external_id = element.get('default_external_id') or '' if element.get('widget_type') in get_widget_types(): question.widget_type = element.get('widget_type') else: question.widget_type = 'text' question.value_type = element.get('value_type') or '' question.maximum = element.get('maximum') question.minimum = element.get('minimum') question.step = element.get('step') question.unit = element.get('unit') or '' question.width = element.get('width') conditions = get_m2m_instances(question, element.get('conditions'), Condition) optionsets = get_m2m_instances(question, element.get('optionsets'), OptionSet) if save and validate_instance(question, QuestionLockedValidator, QuestionUniqueURIValidator): if question.id: logger.info('Question created with uri %s.', element.get('uri')) else: logger.info('Question %s updated.', element.get('uri')) question.save() question.conditions.set(conditions) question.optionsets.set(optionsets) question.imported = True return question
def import_question(element, parent_uri=False, save=False): if parent_uri is False: parent_uri = element.get('questionset') try: question = Question.objects.get(uri=element.get('uri'), questionset__uri=parent_uri) except Question.DoesNotExist: question = Question() set_common_fields(question, element) question.parent_uri = parent_uri question.questionset = get_foreign_field(question, parent_uri, QuestionSet) question.attribute = get_foreign_field(question, element.get('attribute'), Attribute) question.is_collection = element.get('is_collection') question.order = element.get('order') set_lang_field(question, 'text', element) set_lang_field(question, 'help', element) set_lang_field(question, 'verbose_name', element) set_lang_field(question, 'verbose_name_plural', element) question.widget_type = element.get('widget_type') or '' question.value_type = element.get('value_type') or '' question.maximum = element.get('maximum') question.minimum = element.get('minimum') question.step = element.get('step') question.unit = element.get('unit') or '' conditions = get_m2m_instances(question, element.get('conditions'), Condition) optionsets = get_m2m_instances(question, element.get('optionsets'), OptionSet) if save and validate_instance(question): if question.id: logger.info('Question created with uri %s.', element.get('uri')) else: logger.info('Question %s updated.', element.get('uri')) question.save() question.conditions.set(conditions) question.optionsets.set(optionsets) question.imported = True return question
def import_optionset(element, save=False): try: optionset = OptionSet.objects.get(uri=element.get('uri')) except OptionSet.DoesNotExist: optionset = OptionSet() set_common_fields(optionset, element) optionset.order = element.get('order') optionset.provider_key = element.get('provider_key') or '' conditions = get_m2m_instances(optionset, element.get('conditions'), Condition) if save and validate_instance(optionset): if optionset.id: logger.info('OptionSet created with uri %s.', element.get('uri')) else: logger.info('OptionSet %s updated.', element.get('uri')) optionset.save() optionset.conditions.set(conditions) optionset.imported = True return optionset
def import_task(element, save=False): try: task = Task.objects.get(uri=element.get('uri')) except Task.DoesNotExist: task = Task() set_common_fields(task, element) set_lang_field(task, 'title', element) set_lang_field(task, 'text', element) task.start_attribute = get_foreign_field(task, element.get('start_attribute'), Attribute) task.end_attribute = get_foreign_field(task, element.get('end_attribute'), Attribute) task.days_before = element.get('days_before') task.days_after = element.get('days_after') conditions = get_m2m_instances(task, element.get('conditions'), Condition) if save and validate_instance(task): if task.id: logger.info('Task created with uri %s.', element.get('uri')) else: logger.info('Task %s updated.', element.get('uri')) task.save() task.sites.add(Site.objects.get_current()) task.conditions.set(conditions) task.imported = True return task
def import_view(element, save=False): try: view = View.objects.get(uri=element.get('uri')) except View.DoesNotExist: view = View() set_common_fields(view, element) view.template = element.get('template') set_lang_field(view, 'title', element) set_lang_field(view, 'help', element) catalogs = get_m2m_instances(view, element.get('catalogs'), Catalog) if save and validate_instance(view): if view.id: logger.info('View created with uri %s.', element.get('uri')) else: logger.info('View %s updated.', element.get('uri')) view.save() view.sites.add(Site.objects.get_current()) view.catalogs.set(catalogs) view.imported = True return view
def import_questionset(element, section_uri=False, questionset_uri=False, save=False): try: if section_uri is False: questionset = QuestionSet.objects.get(uri=element.get('uri')) else: if questionset_uri is False: questionset = QuestionSet.objects.get(key=element.get('key'), section__uri=section_uri) else: questionset = QuestionSet.objects.get( key=element.get('key'), section__uri=section_uri, questionset__uri=questionset_uri) except QuestionSet.DoesNotExist: questionset = QuestionSet() set_common_fields(questionset, element) questionset.section = get_foreign_field( questionset, section_uri or element.get('section'), Section) questionset.questionset = get_foreign_field( questionset, questionset_uri or element.get('questionset'), QuestionSet) questionset.attribute = get_foreign_field(questionset, element.get('attribute'), Attribute) questionset.is_collection = element.get('is_collection') or False questionset.order = element.get('order') or 0 set_lang_field(questionset, 'title', element) set_lang_field(questionset, 'help', element) set_lang_field(questionset, 'verbose_name', element) set_lang_field(questionset, 'verbose_name_plural', element) conditions = get_m2m_instances(questionset, element.get('conditions'), Condition) if save and validate_instance(questionset, QuestionSetLockedValidator, QuestionSetUniqueURIValidator): if questionset.id: logger.info('QuestionSet created with uri %s.', element.get('uri')) else: logger.info('QuestionSet %s updated.', element.get('uri')) questionset.save() questionset.conditions.set(conditions) questionset.imported = True return questionset
def import_questionset(element, parent_uri=False, save=False): if parent_uri is False: parent_uri = element.get('section') try: questionset = QuestionSet.objects.get(uri=element.get('uri'), section__uri=parent_uri) except QuestionSet.DoesNotExist: questionset = QuestionSet() set_common_fields(questionset, element) questionset.parent_uri = parent_uri questionset.section = get_foreign_field(questionset, parent_uri, Section) questionset.attribute = get_foreign_field(questionset, element.get('attribute'), Attribute) questionset.is_collection = element.get('is_collection') questionset.order = element.get('order') set_lang_field(questionset, 'title', element) set_lang_field(questionset, 'help', element) set_lang_field(questionset, 'verbose_name', element) set_lang_field(questionset, 'verbose_name_plural', element) conditions = get_m2m_instances(questionset, element.get('conditions'), Condition) if save and validate_instance(questionset): if questionset.id: logger.info('QuestionSet created with uri %s.', element.get('uri')) else: logger.info('QuestionSet %s updated.', element.get('uri')) questionset.save() questionset.conditions.set(conditions) questionset.imported = True return questionset