Commit 2311ed28 by Alex Gaynor

Added TaggableForm, and tests

parent 46a4e80a
from django import forms
from taggit.utils import parse_tags
class TaggableForm(forms.ModelForm):
tags = forms.CharField(help_text="A comma seperated list of tags.")
def save(self, commit=True):
obj = super(TaggableForm, self).save(commit=False)
def save_tags():
# TODO: Remove the assumption that the manager is named 'tags'
obj.tags.set(parse_tags(self.cleaned_data['tags']))
if commit:
obj.save()
save_tags()
else:
obj.save_tags = save_tags
return obj
from taggit.tests.tests import (AddTagTestCase)
from taggit.forms import TaggableForm
from taggit.tests.models import Food
class FoodForm(TaggableForm):
class Meta:
model = Food
from django.test import TestCase
from taggit.models import Tag
from taggit.tests.forms import FoodForm
from taggit.tests.models import Food
class BaseTaggingTest(TestCase):
......@@ -32,3 +33,13 @@ class AddTagTestCase(BaseTaggingTest):
apple.tags.remove('green')
self.assert_tags_equal(apple.tags.all(), ['red'])
self.assert_tags_equal(Food.tags.all(), ['green', 'red'])
class TaggableFormTestCase(BaseTaggingTest):
def test_form(self):
self.assertEqual(FoodForm.base_fields.keys(), ['name', 'tags'])
f = FoodForm({'name': 'apple', 'tags': 'green, red, yummy'})
f.save()
apple = Food.objects.get(name='apple')
self.assert_tags_equal(apple.tags.all(), ['green', 'red', 'yummy'])
from functools import wraps
def split_tags(tags):
def parse_tags(tags):
return [o.strip() for o in tags.split(',') if o.strip()]
def require_instance_manager(func):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment