diff --git a/libreforms_fastapi/app/__init__.py b/libreforms_fastapi/app/__init__.py index c03092e..3a06734 100644 --- a/libreforms_fastapi/app/__init__.py +++ b/libreforms_fastapi/app/__init__.py @@ -4446,6 +4446,7 @@ async def api_admin_write_form_config( write_form_config_yaml( config_path=config.FORM_CONFIG_PATH, form_config_str=_form_config.content, + env=config.ENVIRONMENT, # validate=True, timezone=config.TIMEZONE, ) @@ -5293,7 +5294,7 @@ async def ui_admin_write_form_config(request: Request, config = Depends(get_conf form_config_str = get_form_config_yaml(config_path=config.FORM_CONFIG_PATH).strip() - past_versions = get_form_backups() + past_versions = get_form_backups(config_path=config.FORM_CONFIG_PATH, env=config.ENVIRONMENT) # print(form_config_str) diff --git a/libreforms_fastapi/utils/config.py b/libreforms_fastapi/utils/config.py index b070182..ca58d62 100644 --- a/libreforms_fastapi/utils/config.py +++ b/libreforms_fastapi/utils/config.py @@ -153,11 +153,11 @@ def validate_timezone(cls, v): # Here we specify a path to our JSON form config representation, see # https://github.com/signebedi/libreforms-fastapi/issues/37. - FORM_CONFIG_PATH:str = os.getenv('FORM_CONFIG_PATH', os.path.join(os.path.join(os.getcwd(), "instance", "form_config.yml"))) + FORM_CONFIG_PATH:str = os.getenv('FORM_CONFIG_PATH', os.path.join(os.path.join(os.getcwd(), "instance", f"{env}_form_config.yml"))) # Here we allow admins to decide whether to enable site documentation DOCS_ENABLED:bool = os.getenv('DOCS_ENABLED', 'False') == 'True' - DOCS_PATH:str = os.getenv('DOCS_PATH', os.path.join(os.path.join(os.getcwd(), "instance", "docs.md"))) + DOCS_PATH:str = os.getenv('DOCS_PATH', os.path.join(os.path.join(os.getcwd(), "instance", f"{env}_docs.md"))) # Here we allow users to export form data as excel, see # https://github.com/signebedi/libreforms-fastapi/issues/215. diff --git a/libreforms_fastapi/utils/docs.py b/libreforms_fastapi/utils/docs.py index bf1c405..e5ec070 100644 --- a/libreforms_fastapi/utils/docs.py +++ b/libreforms_fastapi/utils/docs.py @@ -142,7 +142,7 @@ def get_docs( - + def write_docs(docs_path, content, scrub_unsafe=False): """ Writes content to a document at docs_path, backing up the original if it exists. diff --git a/libreforms_fastapi/utils/document_database.py b/libreforms_fastapi/utils/document_database.py index 800ad46..2518b23 100644 --- a/libreforms_fastapi/utils/document_database.py +++ b/libreforms_fastapi/utils/document_database.py @@ -899,8 +899,8 @@ def get_all_documents_for_user(self, username: str, exclude_deleted:bool=True) - def get_all_documents_as_excel( self, form_name:str, - file_path:str=os.path.join("instance", "export"), - limit_users:Union[bool, str]=False, + file_path:bool|str=False, + limit_users:bool|str=False, exclude_deleted:bool=True, escape_output:bool=False, exclude_journal:bool=True, @@ -909,7 +909,10 @@ def get_all_documents_as_excel( if not self.use_excel: return False - + + if not file_path: + file_path=os.path.join("instance", f"{self.env}_export") + # if not file_path.endswith(".xlsx"): # raise ImproperExcelFilenameFormat(form_name, file_path) @@ -1094,11 +1097,14 @@ def get_one_document( exclude_deleted:bool=True, escape_output:bool=False, to_file:bool=False, - file_path:str=os.path.join("instance", "export"), + file_path:bool|str=False, ): """Retrieves a single entry that matches the search query.""" self._check_form_exists(form_name) + if not file_path: + file_path=os.path.join("instance", f"{self.env}_export") + document = self.databases[form_name].get(doc_id=document_id) if not document: diff --git a/libreforms_fastapi/utils/pydantic_models.py b/libreforms_fastapi/utils/pydantic_models.py index 0c039b1..0a34b22 100644 --- a/libreforms_fastapi/utils/pydantic_models.py +++ b/libreforms_fastapi/utils/pydantic_models.py @@ -390,6 +390,7 @@ def get_form_config_yaml(config_path=None): def write_form_config_yaml( config_path, form_config_str, + env, # validate=True, timezone=ZoneInfo("America/New_York") ): @@ -425,7 +426,7 @@ def write_form_config_yaml( os.makedirs(basedir) # Create a backup of the current config - config_backup_directory = Path(os.getcwd()) / 'instance' / 'form_config_backups' + config_backup_directory = Path(os.getcwd()) / 'instance' / f'{env}_form_config_backups' config_backup_directory.mkdir(parents=True, exist_ok=True) datetime_format = datetime.now(timezone).strftime("%Y%m%d%H%M%S") @@ -448,12 +449,15 @@ def write_form_config_yaml( return True -def get_form_backups(config_path=None): +def get_form_backups(config_path=None, env=None): current_config = get_form_config_yaml(config_path=config_path) + if not env: + env = "" + # Define the backup directory path - directory_path = os.path.join(os.getcwd(), 'instance', 'form_config_backups') + directory_path = os.path.join(os.getcwd(), 'instance', f'{env}_form_config_backups') os.makedirs(directory_path, exist_ok=True) # Get the list of files in the directory