Skip to content

Commit 13649ae

Browse files
committed
refactor code for admin panel handling TDAMM
1 parent 3ee8845 commit 13649ae

File tree

3 files changed

+15
-87
lines changed

3 files changed

+15
-87
lines changed

sde_collections/admin.py

Lines changed: 9 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -265,101 +265,33 @@ def exclude_and_delete_children(modeladmin, request, queryset):
265265

266266

267267
class 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

331288
class 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

417342
class TitlePatternAdmin(admin.ModelAdmin):
418343
"""Admin View for TitlePattern"""

sde_collections/serializers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ def to_representation(self, instance):
126126
return representation
127127

128128
def get_tdamm_tag(self, obj):
129-
return obj.tdamm_tag
129+
tags = obj.tdamm_tag
130+
return tags if tags is not None else []
130131

131132
def get_document_type(self, obj):
132133
if obj.document_type is not None:

sde_collections/utils/paired_field_descriptor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ def __get__(self, instance, owner):
5959
manual_value = getattr(instance, self.manual_field_name, None)
6060
ml_value = getattr(instance, self.ml_field_name, None)
6161

62-
# Return manual if it exists, otherwise ML
63-
return manual_value if manual_value is not None else ml_value
62+
# Return manual value only if it exists and is not empty
63+
if manual_value and len(manual_value) > 0:
64+
return manual_value
65+
return ml_value
6466

6567
def __set__(self, instance, value):
6668
"""

0 commit comments

Comments
 (0)