def test_makeTags(self):
        # before
        wlib = self.store.wlib
        self.assertEqual(len(wlib.tags), 6)

        # makeTags() with ''
        tags = weblib.makeTags(self.store, '')
        self.assert_(not tags)
        self.assertEqual(len(wlib.tags), 6)

        # makeTags() with spaces
        tags = weblib.makeTags(self.store, '    ')
        self.assert_(not tags)
        self.assertEqual(len(wlib.tags), 6)

        # makeTags() with existing tags
        tags = weblib.makeTags(self.store, 'English,Kremlin')
        self.assertEqual(len(tags), 2)
        self.assertEqual(len(wlib.tags), 6)

        # makeTags() with new tags
        tags = weblib.makeTags(self.store, 'T1,T2,T3')
        self.assertEqual(len(tags), 3)
        self.assertEqual(len(wlib.tags), 9)

        # makeTags() with existing and new tags
        tags = weblib.makeTags(self.store, 'T4,T5,English,Kremlin')
        self.assertEqual(len(tags), 4)
        self.assertEqual(len(wlib.tags), 11)
    def test_makeTags_order(self):
        wlib = self.store.wlib

        # makeTags() duplicated (both old and new)
        tags = weblib.makeTags(self.store, 'A,a,A,English,English,b,B,b')

        # should come out in same order as user entered
        self.assertEqual(tags,[
            wlib.tags.getByName('a'),
            wlib.tags.getByName('English'),
            wlib.tags.getByName('b'),
            ])
    def test_makeTags_duplicated(self):
        # before
        wlib = self.store.wlib
        self.assertEqual(len(wlib.tags), 6)

        # makeTags() duplicated (both old and new)
        tags = weblib.makeTags(self.store, 'A,a,A,English,English,b,B,b')

        self.assertEqual(len(tags), 3)          # 5 duplicates dropped

        self.assertEqual(len(wlib.tags), 8)     # only 2 new tags (a,b)
        self.assert_(wlib.tags.getByName('a'))
        self.assert_(wlib.tags.getByName('b'))
def doPutResource(wfile, req, bean):
    wlib = store.getWeblib()

    if not bean.validate():
        FormRenderer(wfile).output(bean)
        return

    item = bean.item
    if bean.newTags:
        assert bean.create_tags
        item.tags = weblib.makeTags(store.getStore(), item.tags_description)

    if item.id < 0:
        log.info('Adding WebPage: %s' % unicode(item))
        store.getStore().writeWebPage(item)
    else:
        log.info('Updating WebPage: %s' % unicode(item))
        store.getStore().writeWebPage(item)

    response.redirect(wfile, '/updateParent')
def doPost(wfile, req):
    wlib = store.getWeblib()
    entries = _buildEntries(req)
    checklist = _buildChecklist(req)
    errors = []

    # parse add tags
    tags_description = req.param('add_tags')
    if weblib.Tag.hasIllegalChar(tags_description.replace(',',' ')):
        errors.append('These characters are not allowed in tag name: ' + weblib.Tag.ILLEGAL_CHARACTERS)
        tags_description = ''

    # check for new tags and the create_tags flag
    _, unknown = weblib.parseTags(wlib, tags_description)
    if unknown and (not req.param('create_tags')):
        tags = u', '.join(unknown)
        errors.append('These tags are not previous used: ' + tags)
        tags_description = ''

    # note: validation should be done, new tags will be created here
    set_tags = weblib.makeTags(store.getStore(), tags_description)
    remove_tags = []

    # going through checklist, add to set_tags, delete_tags
    for tag, flag in checklist:
        if flag:
            if tag not in set_tags:
                set_tags.append(tag)
        else:
            remove_tags.append(tag)

    if errors:
        doShowForm(wfile, req, errors, checklist=checklist, new_tags=unknown)
        return

    log.debug('EditTags for %s entries set(%s) remove(%s).', len(entries), set_tags, remove_tags)
    wlib.editTags(entries, [], set_tags, remove_tags)
    store.getStore().refresh_when_needed()

    response.redirect(wfile, '/updateParent')