Skip to content

Commit

Permalink
add options for media_url and static_url (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
viseshrp authored Dec 11, 2024
1 parent 23bdd12 commit 960213a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
40 changes: 32 additions & 8 deletions src/ansys/dynamicreporting/core/serverless/adr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 13 additions & 14 deletions src/ansys/dynamicreporting/core/serverless/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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):
Expand All @@ -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)

0 comments on commit 960213a

Please sign in to comment.