11from __future__ import absolute_import , unicode_literals
22
33from django .contrib import admin
4+ from django .utils .html import format_html
45from django .utils .translation import gettext_lazy as _
56
67from lunes_cms .cmsv2 .admins .base import BaseAdmin
@@ -31,6 +32,46 @@ def get_formset(self, request, obj=None, **kwargs):
3132 return formset
3233
3334
35+ class MigratedFilter (admin .SimpleListFilter ):
36+ """
37+ Admin filter for migration status.
38+
39+ Allows filtering units by whether they were migrated from v1 or created in v2.
40+ """
41+
42+ title = _ ("migration status" )
43+ parameter_name = "migrated"
44+
45+ def lookups (self , request , model_admin ):
46+ """
47+ Return the filter options.
48+
49+ Returns:
50+ list: A list of tuples containing (value, label) pairs for the filter options
51+ """
52+ return [
53+ ("yes" , _ ("Migrated from old data model" )),
54+ ("no" , _ ("Not migrated from old data model" )),
55+ ]
56+
57+ def queryset (self , request , queryset ):
58+ """
59+ Filter the queryset based on the selected option.
60+
61+ Args:
62+ request: The HTTP request
63+ queryset: The queryset to filter
64+
65+ Returns:
66+ QuerySet: The filtered queryset
67+ """
68+ if self .value () == "yes" :
69+ return queryset .filter (v1_id__isnull = False )
70+ if self .value () == "no" :
71+ return queryset .filter (v1_id__isnull = True )
72+ return queryset
73+
74+
3475class UnitAdmin (BaseAdmin ):
3576 """
3677 Admin interface for the Unit model.
@@ -41,26 +82,28 @@ class UnitAdmin(BaseAdmin):
4182
4283 fields = [
4384 "title" ,
85+ "migrated_status" ,
4486 "description" ,
4587 "icon" ,
4688 "image_tag" ,
4789 "jobs" ,
4890 "created_by" ,
4991 "released" ,
5092 ]
51- readonly_fields = ["created_by" , "image_tag" ]
93+ readonly_fields = ["created_by" , "image_tag" , "migrated_status" ]
5294 inlines = [WordInline ]
5395 search_fields = ["title" ]
5496 list_display = [
5597 "title" ,
98+ "migrated_status" ,
5699 "released" ,
57100 "list_icon" ,
58101 "related_jobs" ,
59102 "creator_group" ,
60103 "created_at_date" ,
61104 ]
62105 list_display_links = ["title" ]
63- list_filter = ["released" ]
106+ list_filter = ["released" , MigratedFilter ]
64107 list_per_page = 25
65108
66109 class Media :
@@ -120,3 +163,25 @@ def created_at_date(self, obj):
120163 return obj .created_at .date ()
121164
122165 created_at_date .short_description = _ ("created at" )
166+
167+ def migrated_status (self , obj ):
168+ """
169+ Display a badge indicating whether the unit was migrated from v1 or created in v2.
170+
171+ Args:
172+ obj: The unit object
173+
174+ Returns:
175+ str: HTML formatted badge showing migration status
176+ """
177+ if obj .v1_id is not None :
178+ return format_html (
179+ '<span style="background-color: #28a745; color: white; padding: 3px 8px; '
180+ 'border-radius: 3px; font-size: 13px; font-weight: 500;">Migrated</span>'
181+ )
182+ return format_html (
183+ '<span style="background-color: #007bff; color: white; padding: 3px 8px; '
184+ 'border-radius: 3px; font-size: 13px; font-weight: 500;">New</span>'
185+ )
186+
187+ migrated_status .short_description = _ ("migrated" )
0 commit comments