def process_merge(ctype, to_tag, from_tag): logger = logging.getLogger('antichaos.utils') try: to_tag = Tag.objects.get(id=to_tag) except Tag.DoesNotExist: logger.error('skip merge from_tag=%(from_tag)s to_tag=%(to_tag)s cause to_tag is missing.' % locals()) return try: from_tag = Tag.objects.get(id=from_tag) except Tag.DoesNotExist: logger.error('skip merge from_tag=%(from_tag)s to_tag=%(to_tag)s cause from_tag is missing.' % locals()) return logger.debug('merging tag "%s" to tag "%s"' % (from_tag.name, to_tag.name)) if hasattr(settings, 'MULTILINGUAL_TAGS'): """Specialization for tagging-ng's delete method.""" from tagging.utils import merge merge(to_tag, from_tag, ctype) return for item in from_tag.items.filter(content_type = ctype): if to_tag.items.filter( content_type = ctype, object_id = item.object_id).count() != 0: logger.debug('item "%s" already binded to tag "%s"' % (item, to_tag)) item.delete() else: item.tag = to_tag item.save() logger.debug('item "%s" merged' % item) update_objects_tags(item.object)
def testMergeTagsCreatesSynonyms(self): first = TestItem(title = 'first', tags = 'one, two') first.save() second = TestItem(title = 'second', tags = 'second') second.save() self.assertEqual([], [s.name for s in get_tag('two').synonyms.all()]) ctype = model_to_ctype(TestItem) merge('two', 'second', ctype) self.assertEqual(['second'], [s.name for s in get_tag('two').synonyms.all()])
def join(self, query): """This method joins multiple tags together.""" from tagging.utils import merge logger.info('Joining %s' % ','.join([unicode(obj) for obj in query])) tags = list(query) if len(tags) < 2: return first = tags[0] tags = tags[1:] for t in tags: merge(first, t)
def testMergeTagsWhenSynonymAlreadyExists(self): first = TestItem(title = 'first', tags = 'one, two, blah') first.save() second = TestItem(title = 'second', tags = 'second') second.save() blah = get_tag('blah') blah.synonyms.create(name='second') self.assertEqual([], [s.name for s in get_tag('two').synonyms.all()]) ctype = model_to_ctype(TestItem) merge('two', 'second', ctype) self.assertEqual([], [s.name for s in get_tag('two').synonyms.all()]) self.assertEqual(['second'], [s.name for s in get_tag('blah').synonyms.all()])
def testMergeTagsDeletesSecondTagIfNoMoreTaggedItems(self): self.assertEqual(0, Tag.objects.count()) self.assertEqual(0, TaggedItem.objects.count()) self.assertEqual(0, Synonym.objects.count()) first = TestItem(title = 'first', tags = 'one, two') first.save() second = TestItem(title = 'second', tags = 'second') second.save() self.assertEqual(1, len(with_tag('one'))) self.assertEqual(1, len(with_tag('two'))) self.assertEqual(1, len(with_tag('second'))) ctype = model_to_ctype(TestItem) merge('two', 'second', ctype) self.assertEqual(1, len(with_tag('one'))) self.assertEqual(2, len(with_tag('two'))) self.assertRaises(Tag.DoesNotExist, Tag.objects.get, name = 'second')
def merge_tags(modeladmin, request, queryset): if request.POST.get('master_tag'): print 'POST!!!!!!!!!!' master_tag = Tag.objects.get(pk=int(request.POST.get('master_tag'))) tag_ids = request.POST.get('tag_ids').split(',') for t in Tag.objects.exclude(pk=master_tag.pk).filter(pk__in=tag_ids): print 'slave: %s' % t for i in t.items.all(): merge(master_tag, t) else: context = { 'title': "Are you sure?", 'queryset': queryset, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, } return TemplateResponse(request, 'admin/confirm_merge.html', context, current_app=modeladmin.admin_site.name)
def testMergeTagsNotDeletesSecondTag(self): self.assertEqual(0, Tag.objects.count()) self.assertEqual(0, TaggedItem.objects.count()) self.assertEqual(0, Synonym.objects.count()) first = TestItem(title = 'first', tags = 'one, two') first.save() second = TestItem(title = 'second', tags = 'second') second.save() third = TestItemWithCallback(title = 'third', tags = 'second') third.save() self.assertEqual(1, len(with_tag('second'))) self.assertEqual(1, len(with_tag('second', TestItemWithCallback))) ctype = model_to_ctype(TestItem) merge('two', 'second', ctype) self.assertEqual(0, len(with_tag('second'))) self.assertEqual(1, len(with_tag('second', TestItemWithCallback))) self.assert_(Tag.objects.get(name = 'second'))