Skip to content

Commit

Permalink
delete is_tdamm switch functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirandawadi committed Nov 20, 2024
1 parent a5a408c commit e8b0cf0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 51 deletions.
31 changes: 12 additions & 19 deletions sde_collections/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions sde_collections/models/candidate_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
9 changes: 1 addition & 8 deletions sde_collections/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 3 additions & 14 deletions sde_collections/utils/paired_field_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)

Expand All @@ -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)

0 comments on commit e8b0cf0

Please sign in to comment.