

def handle(self, *args, **options):
    self._options = options
    for ts in TagSynonym.objects.all():
        if ((ts.tag != ts.synonym_tag) and (ts.synonym_tag.items.count() > 0)):
            self.stdout.write('')
            self.stdout.write('copying tagged items from tag:')
            self.stdout.write(((str(ts.synonym_tag.id) + ': ') + ts.synonym_tag.name))
            self.stdout.write('to tag:')
            self.stdout.write(((str(ts.tag.id) + ': ') + ts.tag.name))
            self.stdout.write('objects:')
            for ti in ts.synonym_tag.items.all():
                self.stdout.write(((ti.content_type.name + ': ') + unicode(ti.object_id)))
                ti.tag = ts.tag
                ok = True
                delti = False
                try:
                    ti.full_clean()
                except ValidationError as e:
                    if (e.message_dict == {
                        '__all__': ['Tagged item with this Tag, Content type and Object id already exists.'],
                    }):
                        self.stdout.write('object is already tagged on proper tag, delete from synonym')
                        delti = True
                    else:
                        self.stdout.write('validation error:')
                        self.stdout.write(str(e.message_dict))
                        ok = False
                if (ok and options['nodryrun']):
                    try:
                        if delti:
                            ti.delete()
                        else:
                            ti.save()
                        self.stdout.write('done')
                    except IntegrityError as e:
                        self.stdout.write('IntegrityError:')
                        self.stdout.write(str(e))

dataset/ETHPy150Open ofri/Open-Knesset/auxiliary/management/commands/tags_synonyms_copy_tagged_items.py Command.handle/original