From e8b0cf034ecb11d0550c9aee1262b01762d4dba9 Mon Sep 17 00:00:00 2001 From: Kiran Dawadi Date: Wed, 20 Nov 2024 00:43:45 -0600 Subject: [PATCH] delete is_tdamm switch functionality --- sde_collections/admin.py | 31 +++++++------------ ...candidateurl_tdamm_tag_manual_and_more.py} | 9 +----- sde_collections/models/candidate_url.py | 3 +- sde_collections/serializers.py | 9 +----- .../utils/paired_field_descriptor.py | 17 ++-------- 5 files changed, 18 insertions(+), 51 deletions(-) rename sde_collections/migrations/{0059_candidateurl_is_tdamm_candidateurl_tdamm_tag_manual_and_more.py => 0059_candidateurl_tdamm_tag_manual_and_more.py} (95%) diff --git a/sde_collections/admin.py b/sde_collections/admin.py index ba0b477b..02d78434 100644 --- a/sde_collections/admin.py +++ b/sde_collections/admin.py @@ -290,12 +290,11 @@ class CandidateURLAdmin(admin.ModelAdmin): """Admin view for CandidateURL""" form = CandidateURLForm - list_display = ["url", "collection", "is_tdamm", "tdamm_tag_manual", "tdamm_tag_ml"] - list_filter = ["collection", "is_tdamm"] + list_display = ["url", "collection", "tdamm_tag_manual", "tdamm_tag_ml"] + list_filter = ["collection"] search_fields = ("url", "collection__name") def get_fieldsets(self, request, obj=None): - """Dynamically adjust fieldsets based on is_tdamm""" fieldsets = [ ( "Essential Information", @@ -316,27 +315,21 @@ def get_fieldsets(self, request, obj=None): "is_pdf", "present_on_test", "present_on_prod", - "is_tdamm", ) }, ), + ( + "TDAMM Tags", + { + "fields": ( + "tdamm_tag_ml", + "tdamm_tag_manual", + ), + "classes": ("collapse",), + }, + ), ] - # Add TDAMM fields only if is_tdamm is True - if obj and obj.is_tdamm: - fieldsets.append( - ( - "TDAMM Tags", - { - "fields": ( - "tdamm_tag_ml", - "tdamm_tag_manual", - ), - "classes": ("collapse",), - }, - ) - ) - return fieldsets diff --git a/sde_collections/migrations/0059_candidateurl_is_tdamm_candidateurl_tdamm_tag_manual_and_more.py b/sde_collections/migrations/0059_candidateurl_tdamm_tag_manual_and_more.py similarity index 95% rename from sde_collections/migrations/0059_candidateurl_is_tdamm_candidateurl_tdamm_tag_manual_and_more.py rename to sde_collections/migrations/0059_candidateurl_tdamm_tag_manual_and_more.py index c635c833..16cf4219 100644 --- a/sde_collections/migrations/0059_candidateurl_is_tdamm_candidateurl_tdamm_tag_manual_and_more.py +++ b/sde_collections/migrations/0059_candidateurl_tdamm_tag_manual_and_more.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.9 on 2024-11-13 08:01 +# Generated by Django 4.2.9 on 2024-11-20 06:39 import django.contrib.postgres.fields from django.db import migrations, models @@ -11,13 +11,6 @@ class Migration(migrations.Migration): ] operations = [ - migrations.AddField( - model_name="candidateurl", - name="is_tdamm", - field=models.BooleanField( - default=False, help_text="Enable TDAMM tagging for this URL", verbose_name="Is TDAMM?" - ), - ), migrations.AddField( model_name="candidateurl", name="tdamm_tag_manual", diff --git a/sde_collections/models/candidate_url.py b/sde_collections/models/candidate_url.py index 87bfbbaf..f274e839 100644 --- a/sde_collections/models/candidate_url.py +++ b/sde_collections/models/candidate_url.py @@ -81,11 +81,10 @@ class CandidateURL(models.Model): default=False, help_text="Helps keep track if the Current URL is present in production or not", ) - is_tdamm = models.BooleanField("Is TDAMM?", default=False, help_text="Enable TDAMM tagging for this URL") + # is_tdamm = models.BooleanField("Is TDAMM?", default=False, help_text="Enable TDAMM tagging for this URL") tdamm_tag = PairedFieldDescriptor( field_name="tdamm_tag", field_type=ArrayField(models.CharField(max_length=255, choices=TDAMMTags.choices), blank=True, null=True), - switch="is_tdamm", verbose_name="TDAMM Tags", ) diff --git a/sde_collections/serializers.py b/sde_collections/serializers.py index 06227b31..4aec4233 100644 --- a/sde_collections/serializers.py +++ b/sde_collections/serializers.py @@ -116,14 +116,7 @@ class CandidateURLAPISerializer(serializers.ModelSerializer): class Meta: model = CandidateURL - fields = ("url", "title", "document_type", "hash", "file_extension", "tree_root", "is_tdamm", "tdamm_tag") - - def to_representation(self, instance): - """Remove tdamm_tag field if is_tdamm is False""" - representation = super().to_representation(instance) - if not instance.is_tdamm: - representation.pop("tdamm_tag", None) - return representation + fields = ("url", "title", "document_type", "hash", "file_extension", "tree_root", "tdamm_tag") def get_tdamm_tag(self, obj): tags = obj.tdamm_tag diff --git a/sde_collections/utils/paired_field_descriptor.py b/sde_collections/utils/paired_field_descriptor.py index 8fdb2a2f..afebc35a 100644 --- a/sde_collections/utils/paired_field_descriptor.py +++ b/sde_collections/utils/paired_field_descriptor.py @@ -6,13 +6,12 @@ class PairedFieldDescriptor: - Getting the main field returns manual if present, otherwise ML """ - def __init__(self, field_name, field_type, switch, verbose_name=""): + def __init__(self, field_name, field_type, verbose_name=""): self.field_name = field_name self.manual_field_name = f"{field_name}_manual" self.ml_field_name = f"{field_name}_ml" self.field_type = field_type self.verbose_name = verbose_name or field_name.replace("_", " ").title() - self.switch = switch def contribute_to_class(self, cls, name): """Called by Django when the descriptor is added to the model class.""" @@ -48,14 +47,10 @@ def __get__(self, instance, owner): Get the value of the main field: - Returns manual tags if they exist - Otherwise returns ML tags - - Returns None if switch is False """ if instance is None: return self - if not getattr(instance, self.switch, False): - return None - manual_value = getattr(instance, self.manual_field_name, None) ml_value = getattr(instance, self.ml_field_name, None) @@ -69,16 +64,10 @@ def __set__(self, instance, value): Set only the manual field when setting the field. ML field must be set explicitly. """ - if getattr(instance, self.switch, False): - # Only set the manual field - setattr(instance, self.manual_field_name, value) + + setattr(instance, self.manual_field_name, value) def __delete__(self, instance): """Delete both manual and ML fields""" setattr(instance, self.manual_field_name, None) setattr(instance, self.ml_field_name, None) - - def set_ml(self, instance, value): - """Explicit method to set ML tags""" - if getattr(instance, self.switch, False): - setattr(instance, self.ml_field_name, value)