Skip to content

Commit

Permalink
Fix loading/clearing of media files (#2297)
Browse files Browse the repository at this point in the history
Currently, `manage.py loaddemo` incorrectly creates a file (not a
directory) called `media/ethics`.

Even worse, `manage.py resetdb` currently crashes if that file exists.

Fixes #2247
  • Loading branch information
tompollard authored Sep 19, 2024
2 parents 93ad99a + e50276a commit 3b76b1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
4 changes: 3 additions & 1 deletion physionet-django/user/management/commands/loaddemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ def copy_demo_media():
for subdir in os.listdir(demo_media_root):
demo_subdir = os.path.join(demo_media_root, subdir)
target_subdir = os.path.join(settings.MEDIA_ROOT, subdir)
os.makedirs(target_subdir, exist_ok=True)
for item in [i for i in os.listdir(demo_subdir) if i != '.gitkeep']:
path = os.path.join(demo_subdir, item)
if os.path.isdir(path):
shutil.copytree(os.path.join(demo_subdir, item),
os.path.join(target_subdir, item))
else:
shutil.copy(path, target_subdir)
shutil.copy(os.path.join(demo_subdir, item),
os.path.join(target_subdir, item))

# Published project files should have been made read-only at
# the time of publication
Expand Down
27 changes: 12 additions & 15 deletions physionet-django/user/management/commands/resetdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,19 @@ def clear_media_files():
"""
Remove all media files.
Removes all content in the media root, excluding the immediate
subfolders themselves and the .gitkeep files.
Remove all content in the media root, except that if a file called
".gitkeep" is found, preserve that file and the subdirectory
containing it.
"""
for subdir in os.listdir(settings.MEDIA_ROOT):
media_subdir = os.path.join(settings.MEDIA_ROOT, subdir)
subdir_items = [os.path.join(media_subdir, item) for item in os.listdir(media_subdir) if item != '.gitkeep']
for item in subdir_items:
if os.path.isdir(item):
for root, dirs, files in os.walk(item):
for d in dirs:
os.chmod(os.path.join(root, d), 0o755)
for f in files:
os.chmod(os.path.join(root, f), 0o755)
shutil.rmtree(item)
else:
os.remove(item)
for path, subdirs, files in os.walk(settings.MEDIA_ROOT, topdown=False):
os.chmod(path, 0o755)
for name in files:
if name != '.gitkeep':
os.remove(os.path.join(path, name))
for name in subdirs:
if not os.listdir(os.path.join(path, name)):
os.rmdir(os.path.join(path, name))


def clear_created_static_files():
"""
Expand Down

0 comments on commit 3b76b1d

Please sign in to comment.