From 960213a6371a7870e96b3a8364399f9201160e4f Mon Sep 17 00:00:00 2001 From: Visesh Rajendraprasad Date: Wed, 11 Dec 2024 12:21:52 -0500 Subject: [PATCH] add options for media_url and static_url (#205) --- .../dynamicreporting/core/serverless/adr.py | 40 +++++++++++++++---- .../dynamicreporting/core/serverless/item.py | 27 ++++++------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/ansys/dynamicreporting/core/serverless/adr.py b/src/ansys/dynamicreporting/core/serverless/adr.py index f314bc6b..daf80935 100644 --- a/src/ansys/dynamicreporting/core/serverless/adr.py +++ b/src/ansys/dynamicreporting/core/serverless/adr.py @@ -41,6 +41,8 @@ def __init__( databases: Optional[dict] = None, media_directory: Optional[str] = None, static_directory: Optional[str] = None, + media_url: Optional[str] = None, + static_url: Optional[str] = None, debug: Optional[bool] = None, opts: Optional[dict] = None, request: Optional[HttpRequest] = None, @@ -50,6 +52,8 @@ def __init__( self._databases = databases or {} self._media_directory = None self._static_directory = None + self._media_url = media_url + self._static_url = static_url self._debug = debug self._request = request # passed when used in the context of a webserver. self._session = None @@ -120,6 +124,14 @@ def __init__( elif "CEI_NEXUS_LOCAL_STATIC_DIR" in os.environ: self._static_directory = self._check_dir(os.environ["CEI_NEXUS_LOCAL_STATIC_DIR"]) + def _is_sqlite(self, database: str) -> bool: + return "sqlite" in self._databases.get(database, {}).get("ENGINE", "") + + def _get_db_dir(self, database: str) -> str: + if self._is_sqlite(database): + return self._databases.get(database, {}).get("NAME", "") + return "" + def _get_install_directory(self, ansys_installation: str) -> Path: dirs_to_check = [] if ansys_installation: @@ -209,6 +221,26 @@ def setup(self, collect_static: bool = False) -> None: if self._static_directory is not None: overrides["STATIC_ROOT"] = str(self._static_directory) + # relative URLs: By default, ADR serves static files from the URL /static/ + # and media files from the URL /media/. These can be changed using the + # static_url and media_url options. URLs must be relative and start and end with + # a forward slash. + if self._media_url is not None: + if not self._media_url.startswith("/") or not self._media_url.endswith("/"): + raise ImproperlyConfiguredError( + "The 'media_url' option must be a relative URL and start and end with a forward slash." + " Example: '/media/'" + ) + overrides["MEDIA_URL"] = self._media_url + + if self._static_url is not None: + if not self._static_url.startswith("/") or not self._static_url.endswith("/"): + raise ImproperlyConfiguredError( + "The 'static_url' option must be a relative URL and start and end with a forward slash." + " Example: '/static/'" + ) + overrides["STATIC_URL"] = self._static_url + if self._databases: if "default" not in self._databases: raise ImproperlyConfiguredError( @@ -462,14 +494,6 @@ def create_objects( count += 1 return count - def _is_sqlite(self, database: str) -> bool: - return "sqlite" in self._databases[database]["ENGINE"] - - def _get_db_dir(self, database: str) -> str: - if self._is_sqlite(database): - return self._databases[database]["NAME"] - return "" - def _copy_template(self, template: Template, **kwargs) -> Template: # depth-first walk down from the root, which is 'template', # and copy the children along the way. diff --git a/src/ansys/dynamicreporting/core/serverless/item.py b/src/ansys/dynamicreporting/core/serverless/item.py index aec8dca2..b331232a 100644 --- a/src/ansys/dynamicreporting/core/serverless/item.py +++ b/src/ansys/dynamicreporting/core/serverless/item.py @@ -209,7 +209,8 @@ def file_ext(self): def has_file(self): return self._file is not None - def get_file_path(self): + @property + def file_path(self): return self._orm_instance.payloadfile.path @classmethod @@ -234,10 +235,8 @@ def save(self, **kwargs): # todo: check backward compatibility: _movie is now _anim. self._orm_instance.payloadfile = f"{self.guid}_{self.type}.{self._file_ext}" # Save file to the target path - target_path = self.get_file_path() - self._save_file(target_path, self._file) - # Update content and save ORM instance - self.content = target_path + self._save_file(self.file_path, self._file) + # save ORM instance super().save(**kwargs) def delete(self, **kwargs): @@ -420,14 +419,13 @@ def save(self, **kwargs): target_ext = "png" if not self._enhanced else self._file_ext self._orm_instance.payloadfile = f"{self.guid}_image.{target_ext}" # Save the image - target_path = self.get_file_path() if target_ext == "png" and self._file_ext != target_ext: try: - image.save(target_path, format="PNG") + image.save(self.file_path, format="PNG") except OSError as e: print(f"Error converting image to PNG: {e}") else: # save image as is (if enhanced or already PNG) - self._save_file(target_path, img_bytes) + self._save_file(self.file_path, img_bytes) image.close() super().save(**kwargs) @@ -443,9 +441,8 @@ class Scene(FilePayloadMixin, Item): def save(self, **kwargs): super().save(**kwargs) - file_name = self.get_file_path() - if not Path(get_avz_directory(file_name)).exists(): - rebuild_3d_geometry(file_name) + if not Path(get_avz_directory(self.file_path)).exists(): + rebuild_3d_geometry(self.file_path) class File(FilePayloadMixin, Item): @@ -454,6 +451,8 @@ class File(FilePayloadMixin, Item): def save(self, **kwargs): super().save(**kwargs) - file_name = self.get_file_path() - if file_is_3d_geometry(file_name) and not Path(get_avz_directory(file_name)).exists(): - rebuild_3d_geometry(file_name) + if ( + file_is_3d_geometry(self.file_path) + and not Path(get_avz_directory(self.file_path)).exists() + ): + rebuild_3d_geometry(self.file_path)