Skip to content

Commit 9d07be9

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

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

openedx_tagging/core/tagging/models.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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 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 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,20 +582,16 @@ 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:
585+
if not super().is_valid(
586+
check_taxonomy=check_taxonomy,
587+
check_tag=check_tag,
588+
check_object=check_object,
589+
):
559590
return False
560591

561592
if check_tag and check_taxonomy and (self.tag.taxonomy != self.taxonomy):
562593
return False
563594

564-
if check_object and not self._check_object():
565-
return False
566-
567595
return True
568596

569597

0 commit comments

Comments
 (0)