Skip to content

Commit

Permalink
#1412: video storage management commands as per review
Browse files Browse the repository at this point in the history
code improvement
  • Loading branch information
susanodd committed Jan 13, 2025
1 parent d8dbaac commit ecb9296
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions signbank/dataset_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,21 @@ def gloss_annotations_check(dataset):

def gloss_backup_videos(gloss):

if not gloss:
return GlossVideo.objects.none()
backup_videos = GlossVideo.objects.filter(gloss=gloss,
glossvideonme=None,
glossvideoperspective=None,
version__gt=0).order_by('version', 'id')
return backup_videos


def remove_backup_glossvideo_objects_with_no_file(gloss):
# NOT CALLED
glossvideos = gloss_backup_videos(gloss)
objects_with_missing_files = []
for glossvideo in glossvideos:
video_file_full_path = os.path.join(WRITABLE_FOLDER, str(glossvideo.videofile))
if not os.path.exists(video_file_full_path):
objects_with_missing_files.append(glossvideo)
for obj in objects_with_missing_files:
if settings.DEBUG_VIDEOS:
relative_path = str(glossvideo.videofile)
print('dataset_operations:remove_backup_glossvideo_objects_with_no_file: ', relative_path)
obj.delete()


def renumber_backup_videos(gloss):
# this method accounts for poorly enumerated or missing version numbers
# it renumbers the existing backup videos starting with 1
glossvideos = gloss_backup_videos(gloss)
if not glossvideos:
return
for inx, gloss_video in enumerate(glossvideos, 1):
current_version = gloss_video.version
if inx != current_version:
Expand All @@ -74,13 +65,16 @@ def renumber_backup_videos(gloss):

def rename_backup_videos(gloss):
glossvideos = gloss_backup_videos(gloss)
if not glossvideos:
return
idgloss = gloss.idgloss
glossid = str(gloss.id)
two_letter_dir = get_two_letter_dir(idgloss)
dataset_dir = gloss.lemma.dataset.acronym
for inx, glossvideo in enumerate(glossvideos, 1):
video_file_full_path = os.path.join(WRITABLE_FOLDER, str(glossvideo.videofile))
video_extension = video_file_type_extension(video_file_full_path)
desired_filename_without_extension = idgloss + '-' + str(gloss.id) + video_extension
desired_filename_without_extension = f'{idgloss}-{glossid}{video_extension}'
_, bak = os.path.splitext(glossvideo.videofile.name)
desired_extension = '.bak' + str(glossvideo.id)
desired_filename = desired_filename_without_extension + desired_extension
Expand Down Expand Up @@ -177,11 +171,12 @@ def rename_gloss_video(gloss):
if not glossvideo:
return
idgloss = gloss.idgloss
glossid = str(gloss.id)
two_letter_dir = get_two_letter_dir(idgloss)
dataset_dir = gloss.lemma.dataset.acronym
video_file_full_path = os.path.join(WRITABLE_FOLDER, str(glossvideo.videofile))
video_extension = video_file_type_extension(video_file_full_path)
desired_filename = idgloss + '-' + str(gloss.id) + video_extension
desired_filename = f'{idgloss}-{glossid}{video_extension}'
desired_relative_path = os.path.join(settings.GLOSS_VIDEO_DIRECTORY,
dataset_dir, two_letter_dir, desired_filename)
current_relative_path = str(glossvideo.videofile)
Expand Down Expand Up @@ -356,11 +351,6 @@ def update_gloss_video_backups(request, glossid):
if not gloss:
return JsonResponse({})

# commented out
# this only makes sense on the production server, since there are no files on the development servers
# and we can't test the code if we delete the objects
# remove_backup_glossvideo_objects_with_no_file(gloss)

rename_gloss_video(gloss)
rename_backup_videos(gloss)
renumber_backup_videos(gloss)
Expand Down

0 comments on commit ecb9296

Please sign in to comment.