mirror of
https://github.com/Xevion/simple-viewer.git
synced 2026-01-31 04:26:05 -06:00
big migrate, add refreshing to create class method, make size, resolution and file modification fields nullable, fix ServedDirectory lastRefreshed's lack of default, remove last of easy_thumbnails
also adds some small documentation/typing fixes
This commit is contained in:
@@ -0,0 +1,75 @@
|
|||||||
|
# Generated by Django 3.1.2 on 2020-11-04 01:09
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
import django.utils.timezone
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('viewer', '0006_file_thumbnail'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='ImageResolution',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('x', models.PositiveIntegerField()),
|
||||||
|
('y', models.PositiveIntegerField()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='file',
|
||||||
|
name='fileLastModified',
|
||||||
|
field=models.DateTimeField(default=None, null=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='file',
|
||||||
|
name='initialCreation',
|
||||||
|
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='file',
|
||||||
|
name='lastModified',
|
||||||
|
field=models.DateTimeField(auto_now=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='file',
|
||||||
|
name='lastRefreshed',
|
||||||
|
field=models.DateTimeField(default=django.utils.timezone.now),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='file',
|
||||||
|
name='size',
|
||||||
|
field=models.PositiveIntegerField(null=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='serveddirectory',
|
||||||
|
name='initialCreation',
|
||||||
|
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='serveddirectory',
|
||||||
|
name='lastModified',
|
||||||
|
field=models.DateTimeField(auto_now=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='serveddirectory',
|
||||||
|
name='lastRefreshed',
|
||||||
|
field=models.DateTimeField(default=django.utils.timezone.now),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='file',
|
||||||
|
name='resolution',
|
||||||
|
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='file', to='viewer.imageresolution'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='file',
|
||||||
|
name='thumbnailResolution',
|
||||||
|
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='real_file', to='viewer.imageresolution'),
|
||||||
|
),
|
||||||
|
]
|
||||||
+20
-15
@@ -9,13 +9,9 @@ import jsonfield
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from easy_thumbnails.alias import aliases
|
|
||||||
|
|
||||||
from viewer import helpers
|
from viewer import helpers
|
||||||
|
|
||||||
if not aliases.get('small'):
|
|
||||||
aliases.set('small', {'size': (150, 80), 'crop': True})
|
|
||||||
|
|
||||||
|
|
||||||
class ServedDirectory(models.Model):
|
class ServedDirectory(models.Model):
|
||||||
"""
|
"""
|
||||||
@@ -37,7 +33,7 @@ class ServedDirectory(models.Model):
|
|||||||
known_subdirectories = jsonfield.JSONField('Tracked Subdirectories JSON', default=[])
|
known_subdirectories = jsonfield.JSONField('Tracked Subdirectories JSON', default=[])
|
||||||
|
|
||||||
lastModified = models.DateTimeField(auto_now=True)
|
lastModified = models.DateTimeField(auto_now=True)
|
||||||
lastRefreshed = models.DateTimeField()
|
lastRefreshed = models.DateTimeField(default=timezone.now)
|
||||||
initialCreation = models.DateTimeField(auto_now_add=True)
|
initialCreation = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
@@ -69,7 +65,7 @@ class ServedDirectory(models.Model):
|
|||||||
self.known_subdirectories = directories
|
self.known_subdirectories = directories
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
return self.path
|
return self.path
|
||||||
|
|
||||||
|
|
||||||
@@ -86,6 +82,9 @@ class ImageResolution(models.Model):
|
|||||||
self.x, self.y = size
|
self.x, self.y = size
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return f'{self.x} x {self.y}'
|
||||||
|
|
||||||
|
|
||||||
class File(models.Model):
|
class File(models.Model):
|
||||||
"""
|
"""
|
||||||
@@ -101,22 +100,28 @@ class File(models.Model):
|
|||||||
|
|
||||||
lastModified = models.DateTimeField(auto_now=True)
|
lastModified = models.DateTimeField(auto_now=True)
|
||||||
initialCreation = models.DateTimeField(auto_now_add=True)
|
initialCreation = models.DateTimeField(auto_now_add=True)
|
||||||
|
lastRefreshed = models.DateTimeField(default=timezone.now)
|
||||||
|
|
||||||
fileLastModified = models.DateTimeField()
|
fileLastModified = models.DateTimeField(null=True, default=None)
|
||||||
lastRefreshed = models.DateTimeField()
|
size = models.PositiveIntegerField(null=True)
|
||||||
size = models.PositiveIntegerField()
|
resolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE, related_name='file', null=True)
|
||||||
resolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE)
|
thumbnailResolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE, related_name='real_file', null=True)
|
||||||
thumbnailResolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, full_path: str, parent: ServedDirectory) -> 'File':
|
def create(cls, full_path: str, parent: ServedDirectory, refresh: bool = True) -> 'File':
|
||||||
"""Simple shortcut for creating a File database entry with just the path."""
|
"""
|
||||||
return File(
|
Simple shortcut for creating a File database entry with just the path.
|
||||||
|
Refreshes the file after creation.
|
||||||
|
"""
|
||||||
|
file = File(
|
||||||
path=full_path,
|
path=full_path,
|
||||||
filename=os.path.basename(full_path),
|
filename=os.path.basename(full_path),
|
||||||
mediatype=File.get_mediatype(full_path),
|
mediatype=File.get_mediatype(full_path),
|
||||||
directory=parent
|
directory=parent
|
||||||
)
|
)
|
||||||
|
if refresh:
|
||||||
|
file.refresh()
|
||||||
|
return file
|
||||||
|
|
||||||
def refresh(self) -> None:
|
def refresh(self) -> None:
|
||||||
"""Refresh this file's metadata"""
|
"""Refresh this file's metadata"""
|
||||||
@@ -163,7 +168,7 @@ class File(models.Model):
|
|||||||
return humanize.naturalsize(self.size)
|
return humanize.naturalsize(self.size)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def thumbnail_static_path(self):
|
def thumbnail_static_path(self) -> str:
|
||||||
"""Used for accessing the thumbnail via the static URL"""
|
"""Used for accessing the thumbnail via the static URL"""
|
||||||
return f'/thumbnails/{self.thumbnail}'
|
return f'/thumbnails/{self.thumbnail}'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user