def process_form_definition_loading(cls, form_def): from antares.apps.core.models import SystemParameter from antares.apps.core.constants import FieldDataType separator = SystemParameter.find_one("DEFAULT_FORM_NAME_SEPARATOR", FieldDataType.STRING, '-') definition_obj = etree.fromstring(form_def.definition) FormDefinition.verify_xml_against_schema(form_def) form_name = definition_obj.find('headerElements/formName') form_version = definition_obj.find('headerElements/formVersion') if (form_name is not None and form_name.text and form_version is not None and form_version.text): form_def.form_name = form_name.text form_def.form_version = form_version.text form_def.id = "{form_name}{separator}{form_version}".format( form_name=form_def.form_name, separator=separator, form_version=form_def.form_version) else: raise InvalidFormDefinitionException( _(__name__ + ".incomplete_form_id_definition")) definition_obj = FormDefinition.set_blank_header_xml(definition_obj) form_def.definition = etree.tostring(definition_obj, encoding='UTF-8', xml_declaration=False) form_def.verify_and_create_supporting_files(True) return form_def
def save(self, *args, **kwargs): if self.script_engine is None: self.script_engine = ScriptEngineType.to_enum( SystemParameter.find_one("DEFAULT_SCRIPT_ENGINE", FieldDataType.STRING, str(ScriptEngineType.JAVASCRIPT))) super(FlowActionDefinition, self).save(*args, **kwargs)
def process_account_transaction_hrn_script( cls, account_transaction: AccountTransaction) -> AccountTransaction: """ Gets a new HRN Code based on the transaction type """ hrn_script = account_transaction.transaction_type_type.hrn_script if not hrn_script: account_transaction.hrn_code = HrnCode._get_next_hrn_by_params( HrnModuleType.ACCOUNT_TRANSACTION, "GENERAL_ACCOUNT_TRANSACTION_SEQUENCE") return account_transaction language = ScriptEngineType.to_enum( SystemParameter.find_one('DEFAULT_SCRIPT_ENGINE', FieldDataType.STRING, ScriptEngineType.JAVASCRIPT.value)) if language == ScriptEngineType.PYTHON: # remember to return a dictionary with two values, hrn_string and # hrn_tile value = eval(hrn_script) if value.get('hrn_code'): account_transaction.hrn_code = value.get('hrn_code') else: account_transaction.hrn_script = HrnCode._get_next_hrn_by_params( HrnModuleType.ACCOUNT_TRANSACTION, "GENERAL_ACCOUNT_TRANSACTION_SEQUENCE") elif ScriptEngineType.JAVASCRIPT: context = js2py.EvalJs({ 'transaction': account_transaction, 'logger': logger, 'HrnCode': HrnCode, 'user': get_request().user }) context.execute(hrn_script) if (hasattr(context, 'hrn_code') and context.hrn_code is not None): account_transaction.hrn_code = context.hrn_code else: account_transaction.hrn_script = HrnCode._get_next_hrn_by_params( HrnModuleType.ACCOUNT_TRANSACTION, "GENERAL_ACCOUNT_TRANSACTION_SEQUENCE") return account_transaction return account_transaction
def process_flow_activity_hrn_script(cls, flow_activity): """ Gets a new HRN Code based on the activity """ hrn_script = flow_activity.flow_case.flow_definition.hrn_script if not hrn_script: flow_activity.hrn_code = HrnCode._get_next_hrn_by_params( HrnModuleType.FLOW_ACTIVITY, "GENERAL_FLOW_ACTIVITY_SEQUENCE") return flow_activity language = ScriptEngineType.to_enum( SystemParameter.find_one('DEFAULT_SCRIPT_ENGINE', FieldDataType.STRING, ScriptEngineType.JAVASCRIPT.value)) if language == ScriptEngineType.PYTHON: # remember to return a dictionary with two values, hrn_string and # hrn_tile value = eval(hrn_script) if value.get('hrn_code'): flow_activity.hrn_code = value.get('hrn_code') else: flow_activity.hrn_code = HrnCode._get_next_hrn_by_params( HrnModuleType.FLOW_ACTIVITY, "GENERAL_FLOW_ACTIVITY_SEQUENCE") return flow_activity elif ScriptEngineType.JAVASCRIPT: context = js2py.EvalJs({ 'activity': flow_activity, 'logger': logger, 'HrnCode': HrnCode, 'user': get_request().user }) context.execute(hrn_script) if (hasattr(context, 'hrn_code') and context.hrn_code is not None): flow_activity.hrn_code = context.hrn_code else: flow_activity.hrn_code = HrnCode._get_next_hrn_by_params( HrnModuleType.FLOW_ACTIVITY, "GENERAL_FLOW_ACTIVITY_SEQUENCE") return flow_activity return flow_activity
def process_document_hrn_script(cls, document, event_type): """ Gets a new HRN Code based on the document definition """ hrn_node = document.document_xml.find( 'headerElements/options/hrnScript') if hrn_node is None or not hrn_node.text: document.set_header_field( 'hrn_code', HrnCode._get_next_hrn_by_params(HrnModuleType.DOCUMENT, "GENERAL_DOCUMENT_SEQUENCE")) return language = ScriptEngineType.to_enum(hrn_node.get('language')) execution_string = hrn_node.text document = document header_fields = document.get_header_field_dict(False) if language is None: language = ScriptEngineType.to_enum( SystemParameter.find_one('DEFAULT_SCRIPT_ENGINE', FieldDataType.STRING, ScriptEngineType.JAVASCRIPT.value)) if execution_string is None: document.set_header_field( 'hrn_code', HrnCode._get_next_hrn_by_params("Document", "GENERAL_DOCUMENT_SEQUENCE")) return document if language == ScriptEngineType.PYTHON: # remember to return a dictionary with two values, hrn_string and # hrn_tile value = eval(execution_string) if value.get('hrn_code'): document.set_header_field("hrn_code", value.get('hrn_code')) else: # default value document.set_header_field( 'hrn_code', HrnCode._get_next_hrn_by_params( HrnModuleType.DOCUMENT, "GENERAL_DOCUMENT_SEQUENCE")) if value.get('hrn_title'): document.set_header_field("hrn_title", value.get('hrn_title')) elif ScriptEngineType.JAVASCRIPT: context = js2py.EvalJs({ 'event_type': event_type.value, 'fields': document.get_field_dict(), 'header_fields': header_fields, 'document': document, 'logger': logger, 'HrnCode': HrnCode, 'user': get_request().user }) # here we return the values as it is, jst hrn_script and hrn_title context.execute(execution_string) if (hasattr(context, 'hrn_code') and context.hrn_code is not None): document.set_header_field("hrn_code", context.hrn_code) else: # default value document.set_header_field( 'hrn_code', HrnCode._get_next_hrn_by_params( HrnModuleType.DOCUMENT, "GENERAL_DOCUMENT_SEQUENCE")) if (hasattr(context, 'hrn_title') and context.hrn_title is not None): document.set_header_field("hrn_title", context.hrn_title) return document
def post_document(cls, document: Document) -> None: status = MessageStatus.find_or_create_one( document=document, module=SystemModuleType.NOTIFICATIONS) if MessageStatusType.to_enum( status.status) != MessageStatusType.PENDING: return for rule in NotificationRule.find_by_form_definition( document.header.form_definition): data_type = document.get_field_data_type(rule.user_code_variable) if (data_type and data_type == FieldDataType.UUID): notification_user = User.find_one( document.get_field_value(rule.user_code_variable)) if (notification_user is None): raise ValueError(_(__name__ + ".exceptions.user_not_found")) data_type = document.get_field_data_type(rule.date_variable) if (data_type and data_type == FieldDataType.DATE): notification_date = document.get_field_value( rule.date_variable) if (notification_date is None): notification_date = timezone.now() else: notification_date = timezone.now() data_type = document.get_field_data_type(rule.content_variable) if (data_type and (data_type == FieldDataType.STRING or data_type == FieldDataType.TEXT)): notification_contents = document.get_field_value( rule.content_variable) if (notification_contents is None): raise ValueError( _(__name__ + ".exceptions.no_contents_available")) if rule.title_variable: data_type = document.get_field_data_type(rule.title_variable) if (data_type and (data_type == FieldDataType.STRING or data_type == FieldDataType.TEXT)): notification_title = document.get_field_value( rule.title_variable) if (notification_title is None): raise ValueError( _(__name__ + ".exceptions.no_title_available")) else: notification_title = _( SystemParameter.find_one( "DEFAULT_NOTIFICATION_TITLE", FieldDataType.STRING, __name__ + ".default.notification_title")) if document.get_author() is None: raise ValueError( _(__name__ + ".exceptions.the_document_has_no_author")) #we have to check that everything is there to post a new record notification_record = NotificationRecord() notification_record.author = document.get_author() notification_record.content = notification_contents notification_record.title = notification_title notification_record.document = document notification_record.post_date = notification_date notification_record.save() status.set_status(MessageStatusType.PROCESSED)
def get_print_site_path(self): return os.path.join( SystemParameter.find_one("DOC_DEFAULT_TEMPLATE_OS_HOME", FieldDataType.STRING, self.DOC_DEFAULT_TEMPLATE_OS_HOME), self.id, self.DOC_PRINT_FILE_NAME)
def get_edit_js_site_path(self): return os.path.join( SystemParameter.find_one("DOC_DEFAULT_TEMPLATE_MEDIA_ROOT", FieldDataType.STRING, self.DOC_DEFAULT_TEMPLATE_MEDIA_ROOT), self.id, 'js', self.DOC_EDIT_JS_FILE_NAME)
def verify_and_create_supporting_files(self, creating_forms=False): template_home = os.path.join(settings.BASE_DIR, settings.BASE_APP_DIR, 'templates', self.DOC_DEFAULT_TEMPLATE_OS_HOME, self.id) if not os.path.isdir(template_home): os.makedirs(template_home) template_media_home = os.path.join( settings.MEDIA_ROOT, self.DOC_DEFAULT_TEMPLATE_MEDIA_ROOT, self.id, 'js') if not os.path.exists(template_media_home): os.makedirs(template_media_home) # FormDefinition.verify_xml_against_schema(form_def) param_creating_form = SystemParameter.find_one( "DOC_DEFAULT_SUPPORTING_FILES_CREATION", FieldDataType.BOOLEAN, False) if creating_forms == False and param_creating_form == True: creating_forms = True if os.path.isfile(os.path.join( template_home, self.DOC_EDIT_FILE_NAME)) and creating_forms == True: os.remove(os.path.join(template_home, self.DOC_EDIT_FILE_NAME)) if not os.path.isfile( os.path.join(template_home, self.DOC_EDIT_FILE_NAME)): if self.edit_xslt: xslt = etree.fromstring(self.edit_xslt) elif os.path.isfile(self.DOC_XSLT_DEFAULT_EDIT_FILE): xslt = etree.parse(self.DOC_XSLT_DEFAULT_EDIT_FILE) else: raise FileNotFoundError dom = etree.fromstring(self.definition) transform = etree.XSLT(xslt) newdom = transform(dom) contents = str(newdom) with open(os.path.join(template_home, self.DOC_EDIT_FILE_NAME), 'w+', encoding='utf8') as text_file: print(contents, file=text_file) if (os.path.isfile( os.path.join(template_media_home, self.DOC_EDIT_JS_FILE_NAME)) and creating_forms == True): os.remove( os.path.join(template_media_home, self.DOC_EDIT_JS_FILE_NAME)) if not os.path.isfile( os.path.join(template_media_home, self.DOC_EDIT_JS_FILE_NAME)): if self.edit_js_xslt: xslt = etree.fromstring(self.edit_js_xslt) elif os.path.isfile(self.DOC_XSLT_DEFAULT_EDIT_JS_FILE): xslt = etree.parse(self.DOC_XSLT_DEFAULT_EDIT_JS_FILE) else: raise FileNotFoundError dom = etree.fromstring(self.definition) transform = etree.XSLT(xslt) newdom = transform(dom) contents = str(newdom) with open(os.path.join(template_media_home, self.DOC_EDIT_JS_FILE_NAME), 'w+', encoding='utf8') as text_file: print(contents, file=text_file) if os.path.isfile(os.path.join( template_home, self.DOC_VIEW_FILE_NAME)) and creating_forms == True: os.remove(os.path.join(template_home, self.DOC_VIEW_FILE_NAME)) if not os.path.isfile( os.path.join(template_home, self.DOC_VIEW_FILE_NAME)): if self.view_xslt: xslt = etree.fromstring(self.view_xslt) elif os.path.isfile(self.DOC_XSLT_DEFAULT_VIEW_FILE): xslt = etree.parse(self.DOC_XSLT_DEFAULT_VIEW_FILE) else: raise FileNotFoundError dom = etree.fromstring(self.definition) transform = etree.XSLT(xslt) newdom = transform(dom) contents = str(newdom) with open(os.path.join(template_home, self.DOC_VIEW_FILE_NAME), 'w+', encoding='utf8') as text_file: print(contents, file=text_file) logger.info('we processed the files for ' + self.id)