Skip to content

Commit 960213a

Browse files
authored
add options for media_url and static_url (#205)
1 parent 23bdd12 commit 960213a

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

src/ansys/dynamicreporting/core/serverless/adr.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def __init__(
4141
databases: Optional[dict] = None,
4242
media_directory: Optional[str] = None,
4343
static_directory: Optional[str] = None,
44+
media_url: Optional[str] = None,
45+
static_url: Optional[str] = None,
4446
debug: Optional[bool] = None,
4547
opts: Optional[dict] = None,
4648
request: Optional[HttpRequest] = None,
@@ -50,6 +52,8 @@ def __init__(
5052
self._databases = databases or {}
5153
self._media_directory = None
5254
self._static_directory = None
55+
self._media_url = media_url
56+
self._static_url = static_url
5357
self._debug = debug
5458
self._request = request # passed when used in the context of a webserver.
5559
self._session = None
@@ -120,6 +124,14 @@ def __init__(
120124
elif "CEI_NEXUS_LOCAL_STATIC_DIR" in os.environ:
121125
self._static_directory = self._check_dir(os.environ["CEI_NEXUS_LOCAL_STATIC_DIR"])
122126

127+
def _is_sqlite(self, database: str) -> bool:
128+
return "sqlite" in self._databases.get(database, {}).get("ENGINE", "")
129+
130+
def _get_db_dir(self, database: str) -> str:
131+
if self._is_sqlite(database):
132+
return self._databases.get(database, {}).get("NAME", "")
133+
return ""
134+
123135
def _get_install_directory(self, ansys_installation: str) -> Path:
124136
dirs_to_check = []
125137
if ansys_installation:
@@ -209,6 +221,26 @@ def setup(self, collect_static: bool = False) -> None:
209221
if self._static_directory is not None:
210222
overrides["STATIC_ROOT"] = str(self._static_directory)
211223

224+
# relative URLs: By default, ADR serves static files from the URL /static/
225+
# and media files from the URL /media/. These can be changed using the
226+
# static_url and media_url options. URLs must be relative and start and end with
227+
# a forward slash.
228+
if self._media_url is not None:
229+
if not self._media_url.startswith("/") or not self._media_url.endswith("/"):
230+
raise ImproperlyConfiguredError(
231+
"The 'media_url' option must be a relative URL and start and end with a forward slash."
232+
" Example: '/media/'"
233+
)
234+
overrides["MEDIA_URL"] = self._media_url
235+
236+
if self._static_url is not None:
237+
if not self._static_url.startswith("/") or not self._static_url.endswith("/"):
238+
raise ImproperlyConfiguredError(
239+
"The 'static_url' option must be a relative URL and start and end with a forward slash."
240+
" Example: '/static/'"
241+
)
242+
overrides["STATIC_URL"] = self._static_url
243+
212244
if self._databases:
213245
if "default" not in self._databases:
214246
raise ImproperlyConfiguredError(
@@ -462,14 +494,6 @@ def create_objects(
462494
count += 1
463495
return count
464496

465-
def _is_sqlite(self, database: str) -> bool:
466-
return "sqlite" in self._databases[database]["ENGINE"]
467-
468-
def _get_db_dir(self, database: str) -> str:
469-
if self._is_sqlite(database):
470-
return self._databases[database]["NAME"]
471-
return ""
472-
473497
def _copy_template(self, template: Template, **kwargs) -> Template:
474498
# depth-first walk down from the root, which is 'template',
475499
# and copy the children along the way.

src/ansys/dynamicreporting/core/serverless/item.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ def file_ext(self):
209209
def has_file(self):
210210
return self._file is not None
211211

212-
def get_file_path(self):
212+
@property
213+
def file_path(self):
213214
return self._orm_instance.payloadfile.path
214215

215216
@classmethod
@@ -234,10 +235,8 @@ def save(self, **kwargs):
234235
# todo: check backward compatibility: _movie is now _anim.
235236
self._orm_instance.payloadfile = f"{self.guid}_{self.type}.{self._file_ext}"
236237
# Save file to the target path
237-
target_path = self.get_file_path()
238-
self._save_file(target_path, self._file)
239-
# Update content and save ORM instance
240-
self.content = target_path
238+
self._save_file(self.file_path, self._file)
239+
# save ORM instance
241240
super().save(**kwargs)
242241

243242
def delete(self, **kwargs):
@@ -420,14 +419,13 @@ def save(self, **kwargs):
420419
target_ext = "png" if not self._enhanced else self._file_ext
421420
self._orm_instance.payloadfile = f"{self.guid}_image.{target_ext}"
422421
# Save the image
423-
target_path = self.get_file_path()
424422
if target_ext == "png" and self._file_ext != target_ext:
425423
try:
426-
image.save(target_path, format="PNG")
424+
image.save(self.file_path, format="PNG")
427425
except OSError as e:
428426
print(f"Error converting image to PNG: {e}")
429427
else: # save image as is (if enhanced or already PNG)
430-
self._save_file(target_path, img_bytes)
428+
self._save_file(self.file_path, img_bytes)
431429
image.close()
432430
super().save(**kwargs)
433431

@@ -443,9 +441,8 @@ class Scene(FilePayloadMixin, Item):
443441

444442
def save(self, **kwargs):
445443
super().save(**kwargs)
446-
file_name = self.get_file_path()
447-
if not Path(get_avz_directory(file_name)).exists():
448-
rebuild_3d_geometry(file_name)
444+
if not Path(get_avz_directory(self.file_path)).exists():
445+
rebuild_3d_geometry(self.file_path)
449446

450447

451448
class File(FilePayloadMixin, Item):
@@ -454,6 +451,8 @@ class File(FilePayloadMixin, Item):
454451

455452
def save(self, **kwargs):
456453
super().save(**kwargs)
457-
file_name = self.get_file_path()
458-
if file_is_3d_geometry(file_name) and not Path(get_avz_directory(file_name)).exists():
459-
rebuild_3d_geometry(file_name)
454+
if (
455+
file_is_3d_geometry(self.file_path)
456+
and not Path(get_avz_directory(self.file_path)).exists()
457+
):
458+
rebuild_3d_geometry(self.file_path)

0 commit comments

Comments
 (0)