Example #1
0
def _delete_and_recreate_tags(slug):
    source_demo = Submission.objects.using('default').get(slug=slug)
    destination_demo = Submission.objects.using('new').get(slug=slug)
    source_type, destination_type = _get_demo_content_types()

    source_tags = source_demo.taggit_tags.all()
    source_tags_names = [tag.name for tag in source_tags]
    destination_tags = destination_demo.taggit_tags.using('new').all()
    destination_tags_names = [tag.name for tag in destination_tags]

    if source_tags_names == destination_tags_names:
        logger.info(
            "%s: Found %s matching tag(s): %s" %
            (source_demo.slug, len(destination_tags), destination_tags_names))
        return destination_tags
    else:
        dest_demo_tagged_items = TaggedItem.objects.using('new').filter(
            tag__in=[tag for tag in destination_tags],
            object_id=destination_demo.id,
            content_type=destination_type)
        dest_demo_tagged_items.using('new').delete()
        logger.info("%s: Migrating %s tag(s): %s" %
                    (source_demo.slug, len(source_tags), source_tags_names))
        for source_tag in source_tags:
            try:
                destination_tag = (Tag.objects.using('new').get(
                    name=source_tag.name))
            except Tag.DoesNotExist:
                destination_tag = Tag(name=source_tag.name)
                destination_tag.save(using='new')
            destination_demo_tag = TaggedItem(content_type=destination_type,
                                              object_id=destination_demo.id,
                                              tag=destination_tag)
            destination_demo_tag.save(using='new')
    return destination_tags
Example #2
0
def _delete_and_recreate_tags(slug):
    source_demo = Submission.objects.using("default").get(slug=slug)
    destination_demo = Submission.objects.using("new").get(slug=slug)
    source_type, destination_type = _get_demo_content_types()

    source_tags = source_demo.taggit_tags.all()
    source_tags_names = [tag.name for tag in source_tags]
    destination_tags = destination_demo.taggit_tags.using("new").all()
    destination_tags_names = [tag.name for tag in destination_tags]

    if source_tags_names == destination_tags_names:
        logger.info(
            "%s: Found %s matching tag(s): %s" % (source_demo.slug, len(destination_tags), destination_tags_names)
        )
        return destination_tags
    else:
        dest_demo_tagged_items = TaggedItem.objects.using("new").filter(
            tag__in=[tag for tag in destination_tags], object_id=destination_demo.id, content_type=destination_type
        )
        dest_demo_tagged_items.using("new").delete()
        logger.info("%s: Migrating %s tag(s): %s" % (source_demo.slug, len(source_tags), source_tags_names))
        for source_tag in source_tags:
            try:
                destination_tag = Tag.objects.using("new").get(name=source_tag.name)
            except Tag.DoesNotExist:
                destination_tag = Tag(name=source_tag.name)
                destination_tag.save(using="new")
            destination_demo_tag = TaggedItem(
                content_type=destination_type, object_id=destination_demo.id, tag=destination_tag
            )
            destination_demo_tag.save(using="new")
    return destination_tags
Example #3
0
 def set_taggeditems(self):
     objects = OldTaggedItem.objects.all()
     print objects.count()
     for old_tagitem in objects:
         old_id = old_tagitem.tag_id
         new_id = self.dupemap.get(old_id, old_id)
         new_tags = NewTag.objects.filter(id=new_id)
         if new_tags:
             new_tag = new_tags[0]
             new_tagitem = NewTaggedItem(
                     id=old_tagitem.id, 
                     tag=new_tag,
                     content_type=old_tagitem.content_type,
                     object_id=old_tagitem.object_id)
             new_tagitem.save()
     print NewTaggedItem.objects.count()
Example #4
0
def _add_tags(obj, tags_str, creator, content_type_name):
    for tag_name in tags_str.split(','):
        tag_name = tag_name.strip()
        # don't recreate the tag if it already exists
        try:
            t = Tag.objects.get(slug=slugify(tag_name))
        except ObjectDoesNotExist as dne:
            t = Tag()
            t.name = tag_name[:99]
            t.slug = slugify(tag_name)
            t.save()
        ti = TaggedItem()
        ti.tag = t
        ti.object_id = obj.id
        ti.tag_creator = creator
        ti.content_type = ContentType.objects.filter(name=content_type_name)[0]
        ti.save()
 def set_taggeditems(self):
     objects = OldTaggedItem.objects.all()
     print objects.count()
     for old_tagitem in objects:
         old_id = old_tagitem.tag_id
         new_id = self.dupemap.get(old_id, old_id)
         new_tags = NewTag.objects.filter(id=new_id)
         if new_tags:
             new_tag = new_tags[0]
             new_tagitem = NewTaggedItem(
                 id=old_tagitem.id,
                 tag=new_tag,
                 content_type=old_tagitem.content_type,
                 object_id=old_tagitem.object_id,
             )
             new_tagitem.save()
     print NewTaggedItem.objects.count()