Skip to content

Commit bbbb4cc

Browse files
committed
refactor: moves object_tag_class to the ObjectTag class
and removes the Closed vs Open ObjectTag subclasses, and registry. Tests are passing, but coverage isn't at 100%
1 parent 98daf95 commit bbbb4cc

File tree

7 files changed

+177
-321
lines changed

7 files changed

+177
-321
lines changed

openedx_tagging/core/tagging/api.py

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
from django.db.models import QuerySet
1616
from 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

2221
def 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

9378
def 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

141126
def 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)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 3.2.19 on 2023-07-17 07:00
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("oel_tagging", "0005_auto_20230710_0140"),
9+
]
10+
11+
operations = [
12+
migrations.DeleteModel(
13+
name="ClosedObjectTag",
14+
),
15+
migrations.DeleteModel(
16+
name="OpenObjectTag",
17+
),
18+
migrations.RemoveField(
19+
model_name="taxonomy",
20+
name="_object_tag_class",
21+
),
22+
migrations.AddField(
23+
model_name="objecttag",
24+
name="_object_tag_class",
25+
field=models.CharField(
26+
help_text="Overrides the ObjectTag subclass used to instantiate this ObjectTag.Must be a fully-qualified module and class name.",
27+
max_length=255,
28+
null=True,
29+
),
30+
),
31+
]

0 commit comments

Comments
 (0)