174 lines
5.9 KiB
Python
174 lines
5.9 KiB
Python
"""
|
|
Django Admin Configuration for Plugin Distribution System
|
|
"""
|
|
from django.contrib import admin
|
|
from django.utils.html import format_html
|
|
from django.urls import reverse
|
|
from unfold.admin import ModelAdmin, TabularInline
|
|
from .models import Plugin, PluginVersion, PluginInstallation, PluginDownload
|
|
|
|
|
|
class PluginVersionInline(TabularInline):
|
|
"""Inline admin for plugin versions."""
|
|
model = PluginVersion
|
|
extra = 0
|
|
fields = ['version', 'status', 'file_size', 'released_at', 'download_count']
|
|
readonly_fields = ['download_count']
|
|
ordering = ['-version_code']
|
|
|
|
def download_count(self, obj):
|
|
return obj.get_download_count()
|
|
download_count.short_description = 'Downloads'
|
|
|
|
|
|
@admin.register(Plugin)
|
|
class PluginAdmin(ModelAdmin):
|
|
"""Admin configuration for Plugin model."""
|
|
|
|
list_display = ['name', 'slug', 'platform', 'is_active', 'latest_version', 'total_downloads', 'created_at']
|
|
list_filter = ['platform', 'is_active', 'created_at']
|
|
search_fields = ['name', 'slug', 'description']
|
|
readonly_fields = ['created_at', 'updated_at', 'total_downloads', 'active_installations']
|
|
|
|
fieldsets = [
|
|
('Basic Info', {
|
|
'fields': ['name', 'slug', 'platform', 'description', 'homepage_url', 'is_active']
|
|
}),
|
|
('Statistics', {
|
|
'fields': ['total_downloads', 'active_installations'],
|
|
'classes': ['collapse']
|
|
}),
|
|
('Timestamps', {
|
|
'fields': ['created_at', 'updated_at'],
|
|
'classes': ['collapse']
|
|
}),
|
|
]
|
|
|
|
inlines = [PluginVersionInline]
|
|
|
|
def latest_version(self, obj):
|
|
latest = obj.get_latest_version()
|
|
if latest:
|
|
return f"v{latest.version}"
|
|
return "-"
|
|
latest_version.short_description = 'Latest Version'
|
|
|
|
def total_downloads(self, obj):
|
|
return obj.get_download_count()
|
|
total_downloads.short_description = 'Total Downloads'
|
|
|
|
def active_installations(self, obj):
|
|
return PluginInstallation.objects.filter(plugin=obj, is_active=True).count()
|
|
active_installations.short_description = 'Active Installations'
|
|
|
|
|
|
@admin.register(PluginVersion)
|
|
class PluginVersionAdmin(ModelAdmin):
|
|
"""Admin configuration for PluginVersion model."""
|
|
|
|
list_display = ['plugin', 'version', 'status', 'file_size_display', 'download_count', 'released_at']
|
|
list_filter = ['plugin', 'status', 'released_at']
|
|
search_fields = ['plugin__name', 'version', 'changelog']
|
|
readonly_fields = ['version_code', 'created_at', 'download_count']
|
|
|
|
fieldsets = [
|
|
('Version Info', {
|
|
'fields': ['plugin', 'version', 'version_code', 'status']
|
|
}),
|
|
('File Info', {
|
|
'fields': ['file_path', 'file_size', 'checksum']
|
|
}),
|
|
('Requirements', {
|
|
'fields': ['min_api_version', 'min_platform_version', 'min_php_version']
|
|
}),
|
|
('Release', {
|
|
'fields': ['changelog', 'force_update', 'released_at']
|
|
}),
|
|
('Statistics', {
|
|
'fields': ['download_count'],
|
|
'classes': ['collapse']
|
|
}),
|
|
('Timestamps', {
|
|
'fields': ['created_at'],
|
|
'classes': ['collapse']
|
|
}),
|
|
]
|
|
|
|
actions = ['release_versions', 'mark_as_update_ready']
|
|
|
|
def file_size_display(self, obj):
|
|
if obj.file_size:
|
|
kb = obj.file_size / 1024
|
|
if kb > 1024:
|
|
return f"{kb / 1024:.1f} MB"
|
|
return f"{kb:.1f} KB"
|
|
return "-"
|
|
file_size_display.short_description = 'Size'
|
|
|
|
def download_count(self, obj):
|
|
return obj.get_download_count()
|
|
download_count.short_description = 'Downloads'
|
|
|
|
@admin.action(description="Release selected versions")
|
|
def release_versions(self, request, queryset):
|
|
from django.utils import timezone
|
|
count = queryset.filter(status__in=['draft', 'testing', 'staged']).update(
|
|
status='released',
|
|
released_at=timezone.now()
|
|
)
|
|
self.message_user(request, f"Released {count} version(s)")
|
|
|
|
@admin.action(description="Mark as update ready (push to installations)")
|
|
def mark_as_update_ready(self, request, queryset):
|
|
count = queryset.filter(status='released').update(status='update_ready')
|
|
self.message_user(request, f"Marked {count} version(s) as update ready")
|
|
|
|
|
|
@admin.register(PluginInstallation)
|
|
class PluginInstallationAdmin(ModelAdmin):
|
|
"""Admin configuration for PluginInstallation model."""
|
|
|
|
list_display = ['site', 'plugin', 'current_version', 'is_active', 'health_status', 'last_health_check']
|
|
list_filter = ['plugin', 'is_active', 'health_status']
|
|
search_fields = ['site__name', 'plugin__name']
|
|
readonly_fields = ['created_at', 'updated_at']
|
|
|
|
fieldsets = [
|
|
('Installation', {
|
|
'fields': ['site', 'plugin', 'current_version', 'is_active']
|
|
}),
|
|
('Health', {
|
|
'fields': ['health_status', 'last_health_check']
|
|
}),
|
|
('Updates', {
|
|
'fields': ['pending_update', 'update_notified_at']
|
|
}),
|
|
('Timestamps', {
|
|
'fields': ['created_at', 'updated_at'],
|
|
'classes': ['collapse']
|
|
}),
|
|
]
|
|
|
|
|
|
@admin.register(PluginDownload)
|
|
class PluginDownloadAdmin(ModelAdmin):
|
|
"""Admin configuration for PluginDownload model."""
|
|
|
|
list_display = ['plugin', 'version', 'site', 'download_type', 'ip_address', 'created_at']
|
|
list_filter = ['plugin', 'download_type', 'created_at']
|
|
search_fields = ['plugin__name', 'site__name', 'ip_address']
|
|
readonly_fields = ['created_at']
|
|
date_hierarchy = 'created_at'
|
|
|
|
fieldsets = [
|
|
('Download Info', {
|
|
'fields': ['plugin', 'version', 'download_type']
|
|
}),
|
|
('Context', {
|
|
'fields': ['site', 'account', 'ip_address', 'user_agent']
|
|
}),
|
|
('Timestamp', {
|
|
'fields': ['created_at']
|
|
}),
|
|
]
|