Skip to content
Snippets Groups Projects
Commit 0f8885dc authored by Mark Skipper's avatar Mark Skipper
Browse files

Refactor to use Term.objects.by_taxonomy

To create "tagged" items with terms
to add tags to items via the api
parent f6622509
No related branches found
No related tags found
No related merge requests found
......@@ -20,7 +20,6 @@ class TaxonomySerializer(serializers.ModelSerializer):
)
class TermSerializer(serializers.ModelSerializer):
class Meta:
......@@ -33,7 +32,6 @@ class TermSerializer(serializers.ModelSerializer):
)
class ItemSerializer(serializers.ModelSerializer):
class Meta:
......@@ -44,13 +42,12 @@ class ItemSerializer(serializers.ModelSerializer):
def create(self, validated_data):
""" Create an item with nested metadata terms
The validated data looks something like this:
{ "body": "some text",
{ "body": "some text",
"terms": [
{"taxonomy": "animal", "name": "Dog"},
{"taxonomy": "thing", "name": "foo"}
],
}
"""
# find all terms listed in term_data and link to item
# in future, we might theoreteically be adding new tags here too,
......@@ -60,11 +57,9 @@ class ItemSerializer(serializers.ModelSerializer):
term_list = validated_data.pop('terms', [])
item = Item.objects.create(**validated_data)
for term_data in term_list:
term = Term.objects.get(
term = Term.objects.by_taxonomy(
taxonomy=term_data['taxonomy'],
name=term_data['name'],
)
item.terms.add(term)
return item
......@@ -35,8 +35,8 @@ class ItemViewSet(viewsets.ModelViewSet, BulkDestroyModelMixin):
def add_term(self, request, item_pk):
item = Item.objects.get(pk=item_pk)
term_data = request.data
term = Term.objects.get(
taxonomy__slug=term_data['taxonomy'],
term = Term.objects.by_taxonomy(
taxonomy=term_data['taxonomy'],
name=term_data['name'],
)
item.terms.add(term)
......
......@@ -52,7 +52,7 @@ class Taxonomy(models.Model):
class TermManager(models.Manager):
def by_taxonomy(self, taxonomy, term_name):
def by_taxonomy(self, taxonomy, name):
""" Fetch an existing Term by its name and its
Taxonomy slug which, together should be unique together.
......@@ -60,7 +60,7 @@ class TermManager(models.Manager):
taxonomy: [Taxonomy|string]
Taxonomy instance or taxonomy slug.
term_name: string: the name of an existing term
name: string: the name of an existing term
returns:
The term object with the given name in the given Taxonomy.
......@@ -77,7 +77,7 @@ class TermManager(models.Manager):
raise ValueError(
"taxonomy must be a Taxonomy instance "
"or a valid taxonomy slug")
return self.get(taxonomy__slug=taxonomy_slug, name=term_name)
return self.get(taxonomy__slug=taxonomy_slug, name=name)
class Term(models.Model):
......
......@@ -19,7 +19,7 @@ def term_with_context():
def test_term_by_taxonomy_with_taxonomies_with_slug(term_with_context):
term = Term.objects.by_taxonomy(
taxonomy=term_with_context.taxonomy.slug,
term_name=term_with_context.name,
name=term_with_context.name,
)
assert term.name == term_with_context.name
......@@ -30,7 +30,7 @@ def test_term_by_taxonomy_with_taxonomies_with_slug(term_with_context):
def test_term_by_taxonomy_with_taxonomies_with_taxonomy(term_with_context):
term = Term.objects.by_taxonomy(
taxonomy=term_with_context.taxonomy,
term_name=term_with_context.name,
name=term_with_context.name,
)
assert term.name == term_with_context.name
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment