from tagging.models import Tag, TaggedItem
from django.contrib.contenttypes.models import ContentType
from auxiliary.models import TagSuggestion
from django.db import IntegrityError

def approve(admin, request, tag_suggestions):
    for tag_suggestion in tag_suggestions:
        object = tag_suggestion.object
        try:
            tag = Tag.objects.create(name=tag_suggestion.name)
            TaggedItem.objects.create(tag=tag, object=object)
        except IntegrityError as e:
            if str(e) != 'column name is not unique':
                raise
        tag_suggestion.delete()

from tagging.models import Tag, TaggedItem
from django.contrib.contenttypes.models import ContentType


def approve(admin, request, tag_suggestions):
    for tag_suggestion in tag_suggestions:
        obj = tag_suggestion.object

        ct = ContentType.objects.get_for_model(obj)

        tag, t_created = Tag.objects.get_or_create(name=tag_suggestion.name)
        ti, ti_created = TaggedItem.objects.get_or_create(
            tag=tag, object_id=obj.pk, content_type=ct)

        tag_suggestion.delete()

Make tag_suggestions test less flaky
Make tag_suggestions test less flaky

Failed on Python 2.7.6 as it was dependant on an error string returned

  from tagging.models import Tag, TaggedItem
  from django.contrib.contenttypes.models import ContentType
+ 
- from auxiliary.models import TagSuggestion
- from django.db import IntegrityError
  
  def approve(admin, request, tag_suggestions):
      for tag_suggestion in tag_suggestions:
-         object = tag_suggestion.object
+         obj = tag_suggestion.object
-         try:
+ 
+         ct = ContentType.objects.get_for_model(obj)
+ 
-             tag = Tag.objects.create(name=tag_suggestion.name)
+         tag, t_created = Tag.objects.get_or_create(name=tag_suggestion.name)
+         ti, ti_created = TaggedItem.objects.get_or_create(
+             tag=tag, object_id=obj.pk, content_type=ct)
+ 
-             TaggedItem.objects.create(tag=tag, object=object)
-         except IntegrityError as e:
-             if str(e) != 'column name is not unique':
-                 raise
          tag_suggestion.delete()
  
Make tag_suggestions test less flaky
## Code Before:
from tagging.models import Tag, TaggedItem
from django.contrib.contenttypes.models import ContentType
from auxiliary.models import TagSuggestion
from django.db import IntegrityError

def approve(admin, request, tag_suggestions):
    for tag_suggestion in tag_suggestions:
        object = tag_suggestion.object
        try:
            tag = Tag.objects.create(name=tag_suggestion.name)
            TaggedItem.objects.create(tag=tag, object=object)
        except IntegrityError as e:
            if str(e) != 'column name is not unique':
                raise
        tag_suggestion.delete()

## Instruction:
Make tag_suggestions test less flaky
## Code After:
from tagging.models import Tag, TaggedItem
from django.contrib.contenttypes.models import ContentType


def approve(admin, request, tag_suggestions):
    for tag_suggestion in tag_suggestions:
        obj = tag_suggestion.object

        ct = ContentType.objects.get_for_model(obj)

        tag, t_created = Tag.objects.get_or_create(name=tag_suggestion.name)
        ti, ti_created = TaggedItem.objects.get_or_create(
            tag=tag, object_id=obj.pk, content_type=ct)

        tag_suggestion.delete()
