@@ -265,101 +265,33 @@ def exclude_and_delete_children(modeladmin, request, queryset):
265265
266266
267267class CandidateURLForm (forms .ModelForm ):
268- tdamm_tag_ml = forms .MultipleChoiceField (
268+ # Define the fields as MultipleChoiceFields with checkboxes
269+ tdamm_tag_manual = forms .MultipleChoiceField (
269270 choices = CandidateURL .TDAMM_TAG_CHOICES ,
270271 required = False ,
271- label = "TDAMM ML Tags" ,
272+ label = "TDAMM Manual Tags" ,
272273 widget = forms .CheckboxSelectMultiple ,
273274 )
274275
275- tdamm_tag_manual = forms .MultipleChoiceField (
276+ tdamm_tag_ml = forms .MultipleChoiceField (
276277 choices = CandidateURL .TDAMM_TAG_CHOICES ,
277278 required = False ,
278- label = "TDAMM Manual Tags" ,
279+ label = "TDAMM ML Tags" ,
279280 widget = forms .CheckboxSelectMultiple ,
280281 )
281282
282283 class Meta :
283284 model = CandidateURL
284285 fields = "__all__"
285286
286- def __init__ (self , * args , ** kwargs ):
287- super ().__init__ (* args , ** kwargs )
288- instance = kwargs .get ("instance" )
289-
290- # Only show TDAMM fields if is_tdamm is True
291- if not instance or not instance .is_tdamm :
292- if "tdamm_tag_ml" in self .fields :
293- del self .fields ["tdamm_tag_ml" ]
294- if "tdamm_tag_manual" in self .fields :
295- del self .fields ["tdamm_tag_manual" ]
296- else :
297- # Initialize tdamm fields only if is_tdamm is True
298- if hasattr (self .instance , "tdamm_tag_ml" ):
299- self .fields ["tdamm_tag_ml" ].initial = self .instance .tdamm_tag_ml or []
300-
301- if hasattr (self .instance , "tdamm_tag_manual" ):
302- self .fields ["tdamm_tag_manual" ].initial = self .instance .tdamm_tag_manual or []
303-
304- def clean (self ):
305- cleaned_data = super ().clean ()
306- return cleaned_data
307-
308- def save (self , commit = True ):
309- instance = super ().save (commit = False )
310-
311- # Handle TDAMM fields if is_tdamm is True
312- if instance .is_tdamm :
313- # Get values from the form
314- tdamm_tag_ml = self .cleaned_data .get ("tdamm_tag_ml" , [])
315- tdamm_tag_manual = self .cleaned_data .get ("tdamm_tag_manual" , [])
316-
317- # Set the values directly on the instance
318- instance .tdamm_tag_ml = tdamm_tag_ml or None
319- instance .tdamm_tag_manual = tdamm_tag_manual or None
320- else :
321- # Clear TDAMM fields if is_tdamm is False
322- instance .tdamm_tag_ml = None
323- instance .tdamm_tag_manual = None
324-
325- if commit :
326- instance .save ()
327-
328- return instance
329-
330287
331288class CandidateURLAdmin (admin .ModelAdmin ):
332- """Admin View for CandidateURL"""
289+ """Admin view for CandidateURL"""
333290
334291 form = CandidateURLForm
335-
336- def get_list_display (self , request ):
337- list_display = [
338- "url" ,
339- "scraped_title" ,
340- "collection" ,
341- "is_tdamm" ,
342- ]
343- # Add TDAMM-related fields only if any TDAMM-enabled URLs exist
344- if CandidateURL .objects .filter (is_tdamm = True ).exists ():
345- list_display .extend (["tdamm_tag_ml_display" , "tdamm_tag_manual_display" ])
346- return list_display
347-
348- list_filter = ("collection" , "is_tdamm" )
349-
350- @admin .display (description = "TDAMM ML Tags" )
351- def tdamm_tag_ml_display (self , obj ):
352- if obj .is_tdamm and obj .tdamm_tag_ml :
353- readable_tags = [dict (CandidateURL .TDAMM_TAG_CHOICES ).get (tag , tag ) for tag in obj .tdamm_tag_ml ]
354- return ", " .join (readable_tags )
355- return ""
356-
357- @admin .display (description = "TDAMM Manual Tags" )
358- def tdamm_tag_manual_display (self , obj ):
359- if obj .is_tdamm and obj .tdamm_tag_manual :
360- readable_tags = [dict (CandidateURL .TDAMM_TAG_CHOICES ).get (tag , tag ) for tag in obj .tdamm_tag_manual ]
361- return ", " .join (readable_tags )
362- return ""
292+ list_display = ["url" , "collection" , "is_tdamm" , "tdamm_tag_manual" , "tdamm_tag_ml" ]
293+ list_filter = ["collection" , "is_tdamm" ]
294+ search_fields = ("url" , "collection__name" )
363295
364296 def get_fieldsets (self , request , obj = None ):
365297 """Dynamically adjust fieldsets based on is_tdamm"""
@@ -406,13 +338,6 @@ def get_fieldsets(self, request, obj=None):
406338
407339 return fieldsets
408340
409- def save_model (self , request , obj , form , change ):
410- """Ensure proper saving of the model"""
411- if not obj .is_tdamm :
412- obj .tdamm_tag_ml = None
413- obj .tdamm_tag_manual = None
414- super ().save_model (request , obj , form , change )
415-
416341
417342class TitlePatternAdmin (admin .ModelAdmin ):
418343 """Admin View for TitlePattern"""
0 commit comments