Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear how to easily add multiple tags to object #9

Open
mozrat opened this issue Nov 24, 2012 · 2 comments
Open

Unclear how to easily add multiple tags to object #9

mozrat opened this issue Nov 24, 2012 · 2 comments

Comments

@mozrat
Copy link

mozrat commented Nov 24, 2012

I'm playing with django-tagging-ng and I've written a view function to add new tags to an object

def add(request):
    if request.method == 'POST':
        form = TagForm(request.POST)
        if form.is_valid():
            model = models.get_model(request.GET.get('app_label'), request.GET.get('object_name'))            
            record = model.objects.get(id=request.GET.get('id', None))
            tags = form.cleaned_data['tags']
            Tag.objects.add_tag(record, tags)
    referer = request.META.get('HTTP_REFERER', '/')
    return HttpResponseRedirect(referer)

This is supposed to add a tag to an object. It works fine in the case that form.cleaned_data['tags'] contains a single tag.

I've also succeeded in adding multiple tags by iterating over parse_tag_input(form.cleaned_data['tags']) and adding a single tag in each iteration

BUT.. When the tag input is like '''one two "three four"''' I fail because parse_tag_input is treating "three four" as a single tag.

Inside add_tag it fails if a tag has multiple elements.

What is the most convenient way to append tags to a list of existing tags for an object and to handle inputs like ''' one two "three four"'''

Sorry if this isn't clear - it actually makes no sense as I read it back to myself :(

@svetlyak40wt
Copy link
Owner

Use TagField.

@mozrat
Copy link
Author

mozrat commented Nov 25, 2012

Hello,

I've written a failing test that shows where I'm getting stuck. Hope this makes sense

# pylint: disable=R0904

import logging
log = logging.getLogger('django')

from django.test import TestCase
from django import forms

from widget.models import Widget
from tagging.models import Tag
from tagging.utils import edit_string_for_tags
from tagging.forms import TagField

class TagForm(forms.Form):
    tags = TagField(max_length=255)


class TaggingTest(TestCase):

    def test_widget_tags(self):

        widg1 = Widget(name='widg1', colour='red', size='small')
        widg1.save()
        log.debug("created widg1")

        Tag.objects.add_tag(widg1, 'foo')
        log.debug("added tag foo to widg1")
        log.debug("widg1.tags = %s" % widg1.tags)      
        self.assertEqual(len(widg1.tags), 1) # foo

        Tag.objects.add_tag(widg1, 'bar')
        log.debug("added tag bar to widg1")
        log.debug("widg1.tags = %s" % widg1.tags)       
        self.assertEqual(len(widg1.tags), 2) # foo bar

        more_tags = {'tags': 'one two three four'}
        form = TagForm(more_tags)
        self.assertTrue(form.is_valid())

        existing_tags = edit_string_for_tags(widg1.tags)
        new_tags = "%s %s" % (form.cleaned_data['tags'], existing_tags)

        Tag.objects.update_tags(widg1, new_tags)
        log.debug("added 4 more tags to widg1")
        log.debug("widg1.tags = %s" % widg1.tags) 

        self.assertEqual(len(widg1.tags), 6) # foo bar one two three four

        # Here is where it goes wrong...
        more_tags2 = {'tags': 'five, six, seven, eight'}
        form2 = TagForm(more_tags2)
        self.assertTrue(form2.is_valid())        
        existing_tags = edit_string_for_tags(widg1.tags)
        log.debug("Trying to add 4 more comma separated tags now")
        new_tags2 = "%s %s" % (form2.cleaned_data['tags'], existing_tags)
        Tag.objects.update_tags(widg1, new_tags2)

        log.debug("I've only got 4 tags now :( %s" % widg1.tags)
        self.assertEqual(len(widg1.tags), 10) # foo bar one two three four five six seven eight

The issue is in accepting multiple tags in a string from a user and adding them all to an existing set of tags. I am using TagField in my form already

[25/Nov/2012 13:51:28] DEBUG [django:24] created widg1
[25/Nov/2012 13:51:29] DEBUG [django:27] added tag foo to widg1
[25/Nov/2012 13:51:29] DEBUG [django:28] widg1.tags = [<Tag: foo>]
[25/Nov/2012 13:51:29] DEBUG [django:32] added tag bar to widg1
[25/Nov/2012 13:51:29] DEBUG [django:33] widg1.tags = [<Tag: bar>, <Tag: foo>]
[25/Nov/2012 13:51:29] DEBUG [django:42] form.cleaned_data=one two three four
[25/Nov/2012 13:51:29] DEBUG [django:45] added 4 more tags to widg1
[25/Nov/2012 13:51:29] DEBUG [django:46] widg1.tags = [<Tag: bar>, <Tag: foo>, <Tag: four>, <Tag: one>, <Tag: three>, <Tag: two>]
[25/Nov/2012 13:51:29] DEBUG [django:55] Trying to add 4 more comma separated tags now
[25/Nov/2012 13:51:29] DEBUG [django:57] form2.cleaned_data=five, six, seven, eight
[25/Nov/2012 13:51:29] DEBUG [django:61] I've only got 4 tags now :( [<Tag: eight bar foo four one three two>, <Tag: five>, <Tag: seven>, <Tag: six>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants