1515from django .db .models import QuerySet
1616from django .utils .translation import gettext_lazy as _
1717
18- from .models import ClosedObjectTag , ObjectTag , OpenObjectTag , Tag , Taxonomy
19- from .registry import cast_object_tag as _cast_object_tag
18+ from .models import ObjectTag , Tag , Taxonomy
2019
2120
2221def create_taxonomy (
@@ -26,22 +25,18 @@ def create_taxonomy(
2625 required = False ,
2726 allow_multiple = False ,
2827 allow_free_text = False ,
29- object_tag_class : Type = None ,
3028) -> Taxonomy :
3129 """
3230 Creates, saves, and returns a new Taxonomy with the given attributes.
3331 """
34- taxonomy = Taxonomy (
32+ taxonomy = Taxonomy . objects . create (
3533 name = name ,
3634 description = description ,
3735 enabled = enabled ,
3836 required = required ,
3937 allow_multiple = allow_multiple ,
4038 allow_free_text = allow_free_text ,
4139 )
42- if object_tag_class :
43- taxonomy .object_tag_class = object_tag_class
44- taxonomy .save ()
4540 return taxonomy
4641
4742
@@ -73,21 +68,11 @@ def get_tags(taxonomy: Taxonomy) -> List[Tag]:
7368 return taxonomy .get_tags ()
7469
7570
76- def cast_object_tag (
77- object_tag : ObjectTag , return_subclass = False
78- ) -> Union [ObjectTag , None ]:
71+ def cast_object_tag (object_tag : ObjectTag ) -> ObjectTag :
7972 """
8073 Casts/copies the given object tag data into the ObjectTag subclass most appropriate for this tag.
81-
82- If ``return_subclass``, this method may return None if it doesn't find a valid subclass of ObjectTag for the
83- given object_tag.
84-
85- If not ``return_subclass``, then the base ObjectTag class may be returned.
8674 """
87- new_object_tag = _cast_object_tag (object_tag )
88- if not new_object_tag and not return_subclass :
89- new_object_tag = ObjectTag ().copy (object_tag )
90- return new_object_tag
75+ return object_tag .cast_object_tag ()
9176
9277
9378def resync_object_tags (object_tags : QuerySet = None ) -> int :
@@ -133,13 +118,17 @@ def get_object_tags(
133118 tags = tags .filter (taxonomy = taxonomy )
134119
135120 for tag in tags :
136- object_tag = cast_object_tag (tag , return_subclass = valid_only )
137- if object_tag :
121+ object_tag = cast_object_tag (tag )
122+ if not valid_only or object_tag . is_valid () :
138123 yield object_tag
139124
140125
141126def tag_object (
142- taxonomy : Taxonomy , tags : List , object_id : str , object_type : str
127+ taxonomy : Taxonomy ,
128+ tags : List ,
129+ object_id : str ,
130+ object_type : str ,
131+ object_tag_class : Type = None ,
143132) -> List [ObjectTag ]:
144133 """
145134 Replaces the existing ObjectTag entries for the given taxonomy + object_id with the given list of tags.
@@ -181,16 +170,17 @@ def tag_object(
181170 tag = None
182171 value = tag_ref
183172
184- object_tag = cast_object_tag (
185- ObjectTag (
186- taxonomy = taxonomy ,
187- object_id = object_id ,
188- object_type = object_type ,
189- tag = tag ,
190- value = value ,
191- name = taxonomy .name ,
192- )
173+ object_tag = ObjectTag (
174+ taxonomy = taxonomy ,
175+ object_id = object_id ,
176+ object_type = object_type ,
177+ tag = tag ,
178+ value = value ,
179+ name = taxonomy .name ,
193180 )
181+ if object_tag_class :
182+ object_tag .object_tag_class = object_tag_class
183+ object_tag = cast_object_tag (object_tag )
194184
195185 object_tag .resync ()
196186 updated_tags .append (object_tag )
0 commit comments