diff --git a/filer/models/foldermodels.py b/filer/models/foldermodels.py index 7b396e8e9..631b9247a 100644 --- a/filer/models/foldermodels.py +++ b/filer/models/foldermodels.py @@ -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'), @@ -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, diff --git a/finder/management/commands/filer_to_finder.py b/finder/management/commands/filer_to_finder.py index 58892a3d3..2ca1e5a51 100644 --- a/finder/management/commands/filer_to_finder.py +++ b/finder/management/commands/filer_to_finder.py @@ -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): @@ -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, @@ -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, @@ -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, @@ -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 diff --git a/finder/models/inode.py b/finder/models/inode.py index 856e04494..f6893318a 100644 --- a/finder/models/inode.py +++ b/finder/models/inode.py @@ -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