Skip to content

Commit

Permalink
Modified: added env-specific notation to instance files (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
signebedi committed Jul 1, 2024
1 parent 044c061 commit 6d6588a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
3 changes: 2 additions & 1 deletion libreforms_fastapi/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions libreforms_fastapi/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion libreforms_fastapi/utils/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions libreforms_fastapi/utils/document_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions libreforms_fastapi/utils/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
):
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 6d6588a

Please sign in to comment.