Skip to content

Commit

Permalink
adopt finder_to_filer for modified inode models
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Nov 13, 2024
1 parent 6789780 commit cfc913a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
5 changes: 0 additions & 5 deletions filer/models/foldermodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ class Folder(models.Model, mixins.IconsMixin):
related_name='children',
on_delete=models.CASCADE,
)

name = models.CharField(
_('name'),
max_length=255,
)

owner = models.ForeignKey(
getattr(settings, 'AUTH_USER_MODEL', 'auth.User'),
verbose_name=_('owner'),
Expand All @@ -111,17 +109,14 @@ class Folder(models.Model, mixins.IconsMixin):
null=True,
blank=True,
)

uploaded_at = models.DateTimeField(
_('uploaded at'),
auto_now_add=True,
)

created_at = models.DateTimeField(
_('created at'),
auto_now_add=True,
)

modified_at = models.DateTimeField(
_('modified at'),
auto_now=True,
Expand Down
32 changes: 21 additions & 11 deletions finder/management/commands/filer_to_finder.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from pathlib import Path

from django.contrib.admin import site
from django.contrib.admin import site as admin_site
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand
from easy_thumbnails.conf import settings

from filer.models.filemodels import Folder as FilerFolder
from filer.models.imagemodels import Image as FilerImage

from finder.contrib.image.models import ImageFileModel as FinderImage
from finder.models.file import FileModel as FinderFile
from finder.models.folder import FolderModel as FinderFolder
from finder.contrib.image.models import ImageFileModel as FinderImage
from finder.models.realm import RealmModel as FinderRealmModel


class Command(BaseCommand):
Expand All @@ -20,13 +23,20 @@ def handle(self, verbosity, *args, **options):
self.forward()

def forward(self):
site = Site.objects.get(id=settings.SITE_ID)
owner = FilerFolder.objects.filter(parent__isnull=True, owner__isnull=False).first().owner
try:
realm = FinderRealmModel.objects.get(site=site, slug=admin_site.name)
except FinderRealmModel.DoesNotExist:
root_folder = FinderFolder.objects.create(name='__root__', owner=owner)
realm = FinderRealmModel.objects.create(site=site, slug=admin_site.name, root_folder=root_folder)

for filer_folder in FilerFolder.objects.filter(parent__isnull=True):
self.migrate_folder(filer_folder, FinderFolder.objects.get_root_folder(site))
self.migrate_folder(filer_folder, FinderFolder.objects.get_root_folder(realm))

def migrate_folder(self, filer_folder, finder_parent):
try:
finder_folder = next(finder_parent.listdir(name=filer_folder.name, is_folder=True))
except StopIteration:
finder_folder = finder_parent.listdir(name=filer_folder.name, is_folder=True).first()
if finder_folder is None:
finder_folder = FinderFolder.objects.create(
name=filer_folder.name,
parent=finder_parent,
Expand All @@ -50,8 +60,8 @@ def migrate_file(self, filer_file, finder_parent):
path = Path(filer_file.file.name)
inode_id = path.parent.stem
try:
finder_file = next(FinderFile.objects.filter_inodes(id=inode_id))
except StopIteration:
finder_file = FinderFile.objects.get_inode(id=inode_id)
except FinderFile.DoesNotExist:
FinderFile.objects.create(
id=inode_id,
name=filer_file.name if filer_file.name else filer_file.original_filename,
Expand Down Expand Up @@ -88,8 +98,8 @@ def migrate_image(self, filer_image, finder_parent):
except ValueError:
pass
try:
finder_image = next(FinderImage.objects.filter_inodes(id=inode_id))
except StopIteration:
finder_image = FinderImage.objects.get_inode(id=inode_id)
except FinderFile.DoesNotExist:
FinderImage.objects.create(
id=inode_id,
name=filer_image.name if filer_image.name else filer_image.original_filename,
Expand All @@ -106,7 +116,7 @@ def migrate_image(self, filer_image, finder_parent):
meta_data=meta_data,
)
else:
if True or filer_image.modified_at > finder_image.last_modified_at:
if filer_image.modified_at > finder_image.last_modified_at:
finder_image.name = filer_image.name if filer_image.name else filer_image.original_filename
finder_image.file_name = path.name
finder_image.last_modified_at = filer_image.modified_at
Expand Down
9 changes: 6 additions & 3 deletions finder/models/inode.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ def get_inode(self, **lookup):
folder_qs = FolderModel.objects.none()
elif (folder_qs := FolderModel.objects.filter(**lookup)).exists():
return folder_qs.get()
values = folder_qs.values('id', mime_type=Value(None, output_field=models.CharField())).union(*[
model.objects.values('id', 'mime_type').filter(**lookup) for model in FileModel.get_models()
]).get()
try:
values = folder_qs.values('id', mime_type=Value(None, output_field=models.CharField())).union(*[
model.objects.values('id', 'mime_type').filter(**lookup) for model in FileModel.get_models()
]).get()
except FolderModel.DoesNotExist as exc:
raise FileModel.DoesNotExist(exc)
return FileModel.objects.get_model_for(values['mime_type']).objects.get(id=values['id'])

@classmethod
Expand Down

0 comments on commit cfc913a

Please sign in to comment.