Skip to content

Commit a8566d3

Browse files
committed
refactor: adds _check_taxonomy and _check_tag methods to the ObjectTag subclasses
1 parent c35cb6a commit a8566d3

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

openedx_tagging/core/tagging/models.py

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ def copy(self, object_tag: "ObjectTag") -> "ObjectTag":
379379
self.id = object_tag.id
380380
self.object_id = object_tag.object_id
381381
self.object_type = object_tag.object_type
382-
self.taxonomy_id = object_tag.taxonomy_id
383-
self.tag_id = object_tag.tag_id
382+
self.taxonomy = object_tag.taxonomy
383+
self.tag = object_tag.tag
384384
self._name = object_tag._name
385385
self._value = object_tag._value
386386
return self
@@ -490,6 +490,24 @@ def valid_for(cls, taxonomy: Taxonomy = None, **kwargs) -> bool:
490490
"""
491491
return taxonomy and taxonomy.allow_free_text
492492

493+
def _check_taxonomy(self):
494+
"""
495+
Returns True if this ObjectTag has a valid taxonomy.
496+
497+
Subclasses should override this method to perform any additional validation for the particular type of object tag.
498+
"""
499+
# Must be linked to a free-text taxonomy
500+
return self.taxonomy_id and self.taxonomy.allow_free_text
501+
502+
def _check_tag(self):
503+
"""
504+
Returns True if this ObjectTag has a valid tag value.
505+
506+
Subclasses should override this method to perform any additional validation for the particular type of object tag.
507+
"""
508+
# Open taxonomies don't need an associated tag, but we need a value.
509+
return bool(self._value)
510+
493511
def _check_object(self):
494512
"""
495513
Returns True if this ObjectTag has a valid object.
@@ -509,14 +527,10 @@ def is_valid(self, check_taxonomy=True, check_tag=True, check_object=True) -> bo
509527
If `check_tag` is False, then we skip validating the object tag's tag reference.
510528
If `check_object` is False, then we skip validating the object ID/type.
511529
"""
512-
# Must be linked to a free-text taxonomy
513-
if check_taxonomy and (
514-
not self.taxonomy_id or not self.taxonomy.allow_free_text
515-
):
530+
if check_taxonomy and not self._check_taxonomy():
516531
return False
517532

518-
# Open taxonomies don't need an associated tag, but we need a value.
519-
if check_tag and not self.value:
533+
if check_tag and not self._check_tag():
520534
return False
521535

522536
if check_object and not self._check_object():
@@ -540,6 +554,24 @@ def valid_for(cls, taxonomy: Taxonomy = None, tag: Tag = None, **kwargs) -> bool
540554
"""
541555
return tag and taxonomy and not taxonomy.allow_free_text
542556

557+
def _check_taxonomy(self):
558+
"""
559+
Returns True if this ObjectTag has a valid taxonomy.
560+
561+
Subclasses should override this method to perform any additional validation for the particular type of object tag.
562+
"""
563+
# Must be linked to a closed taxonomy
564+
return self.taxonomy_id and not self.taxonomy.allow_free_text
565+
566+
def _check_tag(self):
567+
"""
568+
Returns True if this ObjectTag has a valid tag value.
569+
570+
Subclasses should override this method to perform any additional validation for the particular type of object tag.
571+
"""
572+
# Closed taxonomies require a Tag
573+
return bool(self.tag_id)
574+
543575
def is_valid(self, check_taxonomy=True, check_tag=True, check_object=True) -> bool:
544576
"""
545577
Returns True if this ObjectTag is valid for use with a closed taxonomy.
@@ -550,18 +582,14 @@ def is_valid(self, check_taxonomy=True, check_tag=True, check_object=True) -> bo
550582
If `check_tag` is False, then we skip validating the object tag's tag reference.
551583
If `check_object` is False, then we skip validating the object ID/type.
552584
"""
553-
# Must be linked to a closed taxonomy
554-
if check_taxonomy and (not self.taxonomy_id or self.taxonomy.allow_free_text):
555-
return False
556-
557-
# Closed taxonomies require a Tag
558-
if check_tag and not self.tag_id:
559-
return False
560-
561-
if check_tag and check_taxonomy and (self.tag.taxonomy != self.taxonomy):
585+
if not super().is_valid(
586+
check_taxonomy=check_taxonomy,
587+
check_tag=check_tag,
588+
check_object=check_object,
589+
):
562590
return False
563591

564-
if check_object and not self._check_object():
592+
if check_tag and check_taxonomy and (self.tag.taxonomy_id != self.taxonomy_id):
565593
return False
566594

567595
return True

0 commit comments

Comments
 (0)