Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to organize old plugin version packages #328

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions qgis-app/plugins/management/commands/organize_old_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# myapp/management/commands/organize_packages.py
import os
import shutil
from django.core.management.base import BaseCommand
from django.conf import settings
from plugins.models import PluginVersion

PLUGINS_STORAGE_PATH = getattr(settings, "PLUGINS_STORAGE_PATH", "packages")
class Command(BaseCommand):
help = 'Organize packages created before 2014 into folders by year'

def handle(self, *args, **options):
packages_dir = os.path.join(settings.MEDIA_ROOT, PLUGINS_STORAGE_PATH)

# Some of the packages created on 2014 also need to be organized
versions = PluginVersion.objects.filter(created_on__lt='2014-12-31').exclude(package__icontains='2014/')
self.stdout.write(self.style.NOTICE(f'{versions.count()} packages will be organized.'))

for version in versions:
year_folder = os.path.join(packages_dir, str(version.created_on.year))

# Create the year folder if it doesn't exist
os.makedirs(year_folder, exist_ok=True)

# Copy the package file to the year folder
old_path = version.package.path
if os.path.exists(old_path):
new_path = os.path.join(year_folder, os.path.basename(old_path))
if not os.path.exists(new_path):
shutil.copy(old_path, year_folder)

# Update the model with the new package path
version.package.name = os.path.relpath(new_path, settings.MEDIA_ROOT)
version.save()
else:
self.stdout.write(self.style.WARNING(f'Plugin version id {version.pk} ignored: {new_path} already exists.'))
else:
self.stdout.write(self.style.WARNING(f'Plugin version id {version.pk} ignored: {old_path} is not found.'))

self.stdout.write(self.style.SUCCESS('Packages organized successfully'))
Loading