fix thumbnail dir not being created for new installs, add traceback printing for failed thumbnail printing, fix naive datetime object loading

on new installs, the thumbnail directory in /viewer/static is not created automatically
when thumbnail generation fails for any reason, tracebacks are not printed
i fixed the numerous warnings generated by not setting the DateTimeField to a datetime object with a timezone (fixed with dateutil package)
This commit is contained in:
Xevion
2020-11-05 18:27:01 -06:00
parent 85cf832ec2
commit 710924c419
3 changed files with 13 additions and 2 deletions

View File

@@ -3,3 +3,4 @@ humanize~=3.1.0
django-jsonfield~=1.4.1
opencv-python~=4.4.0.46
Pillow~=8.0.1
python-dateutil~=2.8.1

View File

@@ -1,5 +1,6 @@
import mimetypes
import os
import traceback
import uuid
from datetime import datetime
from typing import Tuple
@@ -8,10 +9,16 @@ import humanize
import jsonfield
from django.db import models
from django.urls import reverse
from dateutil.tz import tzlocal
from django.utils import timezone
from viewer import helpers
try:
os.makedirs(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'thumbnails'))
except FileExistsError:
pass
class ServedDirectory(models.Model):
"""
@@ -105,7 +112,8 @@ class File(models.Model):
fileLastModified = models.DateTimeField(null=True, default=None)
size = models.PositiveIntegerField(null=True)
resolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE, related_name='file', null=True)
thumbnailResolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE, related_name='real_file', null=True)
thumbnailResolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE, related_name='real_file',
null=True)
@classmethod
def create(cls, full_path: str, parent: ServedDirectory, refresh: bool = True) -> 'File':
@@ -134,7 +142,7 @@ class File(models.Model):
"""Refresh this file's metadata"""
self.lastRefreshed = timezone.now()
fileLastModified = datetime.fromtimestamp(os.path.getmtime(self.path))
fileLastModified = datetime.fromtimestamp(os.path.getmtime(self.path), tz=tzlocal())
updated = fileLastModified != self.fileLastModified
self.fileLastModified = fileLastModified
@@ -214,6 +222,7 @@ class File(models.Model):
try:
helpers.generate_thumbnail(self.path, self.__thumbs_path)
except Exception:
traceback.print_exc()
print(f'Could not thumbnail: {self.filename}')
self.delete_thumbnail()

View File

@@ -82,6 +82,7 @@ def submit_new(request):
if not os.path.isdir(request.POST['path']):
raise ValueError('A invalid Directory was specified in the request.')
s.save()
s.refresh()
except KeyError:
return render(request, 'message.html', status=403,
context={'title': 'Invalid Options',