def get_module_names(): module_data = {} resources_path = os.path.join(Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY) if not os.path.exists(resources_path): os.makedirs(resources_path) record_field_details_path = Utility.get_file_name() is_null = False if os.path.exists(record_field_details_path): json = Initializer.get_json(record_field_details_path) if Constants.SDK_MODULE_METADATA not in json: is_null = True elif json[Constants.SDK_MODULE_METADATA] is None or len(json[Constants.SDK_MODULE_METADATA])==0: is_null = True if not os.path.exists(record_field_details_path) or is_null: module_data = Utility.get_modules(None) Utility.write_module_meta_data(record_field_details_path, module_data) return module_data record_field_details_json = Initializer.get_json(record_field_details_path) module_data = record_field_details_json[Constants.SDK_MODULE_METADATA] return module_data
def file_exists_flow(module_api_name, record_field_details_path, last_modified_time): with Utility.lock: record_field_details_json = Initializer.get_json(record_field_details_path) initializer = Initializer.get_initializer() if initializer.sdk_config.get_auto_refresh_fields() and not Utility.new_file and not Utility.get_modified_modules and (Constants.FIELDS_LAST_MODIFIED_TIME not in record_field_details_json or Utility.force_refresh or (time.time() * 1000 - record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME]) > 3600000): Utility.get_modified_modules = True last_modified_time = record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] if not Utility.force_refresh and Constants.FIELDS_LAST_MODIFIED_TIME in record_field_details_json else None Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False elif not Initializer.get_initializer().sdk_config.get_auto_refresh_fields() and Utility.force_refresh and not Utility.get_modified_modules: Utility.get_modified_modules = True Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False record_field_details_json = Initializer.get_json(record_field_details_path) if module_api_name is None or (module_api_name.lower() in record_field_details_json and record_field_details_json[module_api_name.lower()] is not None): return else: Utility.fill_data_type() record_field_details_json[module_api_name.lower()] = {} Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module_api_name) record_field_details_json = Initializer.get_json(record_field_details_path) record_field_details_json[module_api_name.lower()] = field_details Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json)
def get_related_lists(related_module_name, module_api_name, common_api_handler): with Utility.lock: try: is_new_data = False key = (module_api_name + Constants.UNDERSCORE + Constants.RELATED_LISTS).lower() resources_path = os.path.join( Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY) if not os.path.exists(resources_path): os.makedirs(resources_path) record_field_details_path = Utility.get_file_name() if not os.path.exists(record_field_details_path) or ( os.path.exists(record_field_details_path) and key not in Initializer.get_json(record_field_details_path) ): is_new_data = True related_list_values = Utility.get_related_list_details( module_api_name) record_field_details_json = Initializer.get_json( record_field_details_path) if os.path.exists( record_field_details_path) else {} record_field_details_json[key] = related_list_values Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) record_field_details_json = Initializer.get_json( record_field_details_path) module_related_list = record_field_details_json[key] if not Utility.check_related_list_exists( related_module_name, module_related_list, common_api_handler) and not is_new_data: del record_field_details_json[key] Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) Utility.get_related_lists(related_module_name, module_api_name, common_api_handler) except SDKException as e: Utility.logger.error(Constants.EXCEPTION + e.__str__()) raise e except Exception as e: sdk_exception = SDKException(cause=e) Utility.logger.error(Constants.EXCEPTION + sdk_exception.__str__()) raise sdk_exception
def __delete_fields(module): """ The method to delete fields of the given module from the current user's fields JSON file. Parameters: module(str): A string representing the module. Raises: SDKException """ try: record_field_details_path = os.path.join( ModuleFieldsHandler.__get_directory(), Converter.get_encoded_file_name()) if os.path.exists(record_field_details_path): record_field_details_json = Initializer.get_json( record_field_details_path) Utility.delete_fields(record_field_details_json, module) with open(record_field_details_path, mode="w") as file: json.dump(record_field_details_json, file) file.flush() file.close() except Exception as e: sdk_exception = SDKException(cause=e) raise sdk_exception
def verify_module_api_name(module_api_name): if module_api_name is not None and module_api_name.lower() in Constants.DEFAULT_MODULE_NAME_VS_API_NAME and Constants.DEFAULT_MODULE_NAME_VS_API_NAME.get(module_api_name.lower()) is not None: return Constants.DEFAULT_MODULE_NAME_VS_API_NAME.get(module_api_name.lower()) record_field_details_path = Utility.get_file_name() if os.path.exists(record_field_details_path): fields_json = Initializer.get_json(record_field_details_path) if Constants.SDK_MODULE_METADATA in fields_json and module_api_name.lower() in fields_json[Constants.SDK_MODULE_METADATA]: return fields_json[Constants.SDK_MODULE_METADATA][module_api_name.lower()][Constants.API_NAME] return module_api_name
def modify_fields(record_field_details_path, modified_time): modified_modules = Utility.get_modules(modified_time) record_field_details_json = Initializer.get_json(record_field_details_path) record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] = time.time() * 1000 Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) if len(modified_modules) > 0: for module in modified_modules.keys(): if module.lower() in record_field_details_json: Utility.delete_fields(record_field_details_json, module) Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) for module in modified_modules.keys(): module_data = modified_modules[module] Utility.get_fields_info(module_data[Constants.API_NAME])
def __get_module_detail_from_user_spec_json(self, module): try: import os from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: import os from ..initializer import Initializer record_field_details_path = os.path.join( Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY, Converter.get_encoded_file_name()) record_field_details = Initializer.get_json(record_field_details_path) module_detail = Utility.get_json_object(record_field_details, module) return module_detail
def write_module_meta_data(record_field_details_path, module_data): field_details_json = Initializer.get_json(record_field_details_path) if os.path.exists( record_field_details_path) else {} field_details_json[Constants.SDK_MODULE_METADATA] = module_data Utility.write_to_file(record_field_details_path, field_details_json)
def get_fields_info(module_api_name, handler_instance=None): """ This method to fetch field details of the current module for the current user and store the result in a JSON file. Parameters: module_api_name (str) : A string containing the CRM module API name. handler_instance (CommonAPIHandler) : A CommonAPIHandler instance. """ try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer last_modified_time = None record_field_details_path = None try: with Utility.lock: if module_api_name is not None and Utility.search_json_details(module_api_name) is not None: return initializer = Initializer.get_initializer() resources_path = os.path.join(initializer.resource_path, Constants.FIELD_DETAILS_DIRECTORY) if not os.path.exists(resources_path): os.makedirs(resources_path) module_api_name = Utility.verify_module_api_name(module_api_name) Utility.set_handler_api_path(module_api_name, handler_instance) if handler_instance is not None and handler_instance.get_module_api_name() is None and module_api_name.lower() not in Constants.SKIP_MODULES: return record_field_details_path = Utility.get_file_name() if os.path.exists(record_field_details_path): Utility.file_exists_flow(module_api_name, record_field_details_path, last_modified_time) elif initializer.sdk_config.get_auto_refresh_fields(): Utility.new_file = True Utility.fill_data_type() if len(Utility.api_supported_modules) == 0: Utility.api_supported_modules = Utility.get_modules(None) record_field_details_json = Initializer.get_json(record_field_details_path) if os.path.exists(record_field_details_path) else {} record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] = time.time() * 1000 if len(Utility.api_supported_modules) > 0: for module in Utility.api_supported_modules: if module.lower() not in record_field_details_json: module_data = Utility.api_supported_modules[module] record_field_details_json[module.lower()] = {} Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module_data[Constants.API_NAME]) record_field_details_json = Initializer.get_json(record_field_details_path) record_field_details_json[module.lower()] = field_details Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) Utility.new_file = False elif Utility.force_refresh and not Utility.get_modified_modules: Utility.get_modified_modules = True record_field_details_json = {} Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False else: Utility.fill_data_type() record_field_details_json = {module_api_name.lower(): {}} Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module_api_name) record_field_details_json = Initializer.get_json(record_field_details_path) record_field_details_json[module_api_name.lower()] = field_details Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) except Exception as e: if record_field_details_path is not None and os.path.exists(record_field_details_path): try: record_field_details_json = Initializer.get_json(record_field_details_path) if module_api_name is not None and module_api_name.lower() in record_field_details_json: del record_field_details_json[module_api_name.lower()] if Utility.new_file: if Constants.FIELDS_LAST_MODIFIED_TIME in record_field_details_json: del record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] Utility.new_file = False if Utility.get_modified_modules or Utility.force_refresh: Utility.get_modified_modules = False Utility.force_refresh = False if last_modified_time is not None: record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] = last_modified_time Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) except Exception as ex: sdk_exception = SDKException(cause=ex) Utility.logger.error(Constants.EXCEPTION + sdk_exception.__str__()) raise sdk_exception if not isinstance(e, SDKException): e = SDKException(cause=e) Utility.logger.info(Constants.EXCEPTION + e.__str__()) raise e
def get_fields_info(module_api_name): """ This method to fetch field details of the current module for the current user and store the result in a JSON file. Parameters: module_api_name (str) : A string containing the CRM module API name. """ try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer last_modified_time = None try: with Utility.lock: resources_path = os.path.join( Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY) if not os.path.exists(resources_path): if module_api_name is not None and Utility.search_json_details( module_api_name) is not None: return os.makedirs(resources_path) record_field_details_path = Utility.get_file_name() if os.path.exists(record_field_details_path): record_field_details_json = Initializer.get_json( record_field_details_path) if Initializer.get_initializer( ).sdk_config.get_auto_refresh_fields( ) and not Utility.new_file and not Utility.get_modified_modules and ( Constants.FIELDS_LAST_MODIFIED_TIME not in record_field_details_json or Utility.force_refresh or (time.time() * 1000 - record_field_details_json[ Constants.FIELDS_LAST_MODIFIED_TIME]) > 3600000): Utility.get_modified_modules = True last_modified_time = record_field_details_json[ Constants. FIELDS_LAST_MODIFIED_TIME] if Constants.FIELDS_LAST_MODIFIED_TIME in record_field_details_json else None Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False elif not Initializer.get_initializer( ).sdk_config.get_auto_refresh_fields( ) and Utility.force_refresh and not Utility.get_modified_modules: Utility.get_modified_modules = True Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False record_field_details_json = Initializer.get_json( record_field_details_path) if module_api_name is None or module_api_name.lower( ) in record_field_details_json: return else: Utility.fill_data_type() record_field_details_json[module_api_name.lower()] = {} Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details( module_api_name) record_field_details_json = Initializer.get_json( record_field_details_path) record_field_details_json[ module_api_name.lower()] = field_details Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) elif Initializer.get_initializer( ).sdk_config.get_auto_refresh_fields(): Utility.new_file = True Utility.fill_data_type() module_api_names = Utility.get_modules(None) record_field_details_json = { Constants.FIELDS_LAST_MODIFIED_TIME: time.time() * 1000 } for module in module_api_names: if module.lower() not in record_field_details_json: record_field_details_json[module.lower()] = {} Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module) record_field_details_json = Initializer.get_json( record_field_details_path) record_field_details_json[ module.lower()] = field_details Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) Utility.new_file = False elif Utility.force_refresh and not Utility.get_modified_modules: Utility.get_modified_modules = True record_field_details_json = {} Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False else: Utility.fill_data_type() record_field_details_json = {module_api_name.lower(): {}} Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module_api_name) record_field_details_json = Initializer.get_json( record_field_details_path) record_field_details_json[ module_api_name.lower()] = field_details Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) except Exception as e: if record_field_details_path is not None and os.path.exists( record_field_details_path): try: record_field_details_json = Initializer.get_json( record_field_details_path) if module_api_name is not None and module_api_name.lower( ) in record_field_details_json: del record_field_details_json[module_api_name.lower()] if Utility.new_file: if Constants.FIELDS_LAST_MODIFIED_TIME in record_field_details_json: del record_field_details_json[ Constants.FIELDS_LAST_MODIFIED_TIME] Utility.new_file = False if Utility.get_modified_modules or Utility.force_refresh: Utility.get_modified_modules = False Utility.force_refresh = False if last_modified_time is not None: record_field_details_json[ Constants. FIELDS_LAST_MODIFIED_TIME] = last_modified_time Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) except Exception as ex: sdk_exception = SDKException(cause=ex) Utility.logger.error(Constants.EXCEPTION + sdk_exception.__str__()) raise sdk_exception if not isinstance(e, SDKException): e = SDKException(cause=e) Utility.logger.info(Constants.EXCEPTION + e.__str__()) raise e