from django.contrib.auth.models import User

from nightreads.posts.models import Tag
from .models import UserTag


def update_user_tags(user, tags):
    tags_objs = Tag.objects.filter(name__in=tags)
    user.usertag.tags.add(*tags_objs)
    user.save()


def get_user(email):
    user, created = User.objects.get_or_create(username=email)
    if created:
        UserTag.objects.create(user=user)
    return user

from django.contrib.auth.models import User

from nightreads.posts.models import Tag
from .models import UserTag


def update_user_tags(user, tags):
    tags_objs = Tag.objects.filter(name__in=tags)
    if tags_objs:
        user.usertag.tags.clear()
        user.usertag.tags.add(*tags_objs)
    user.save()


def get_user(email):
    user, created = User.objects.get_or_create(username=email)
    if created:
        UserTag.objects.create(user=user)
    return user

Clear existing tags before updating
Clear existing tags before updating

  from django.contrib.auth.models import User
  
  from nightreads.posts.models import Tag
  from .models import UserTag
  
  
  def update_user_tags(user, tags):
      tags_objs = Tag.objects.filter(name__in=tags)
+     if tags_objs:
+         user.usertag.tags.clear()
-     user.usertag.tags.add(*tags_objs)
+         user.usertag.tags.add(*tags_objs)
      user.save()
  
  
  def get_user(email):
      user, created = User.objects.get_or_create(username=email)
      if created:
          UserTag.objects.create(user=user)
      return user
  
Clear existing tags before updating
## Code Before:
from django.contrib.auth.models import User

from nightreads.posts.models import Tag
from .models import UserTag


def update_user_tags(user, tags):
    tags_objs = Tag.objects.filter(name__in=tags)
    user.usertag.tags.add(*tags_objs)
    user.save()


def get_user(email):
    user, created = User.objects.get_or_create(username=email)
    if created:
        UserTag.objects.create(user=user)
    return user

## Instruction:
Clear existing tags before updating
## Code After:
from django.contrib.auth.models import User

from nightreads.posts.models import Tag
from .models import UserTag


def update_user_tags(user, tags):
    tags_objs = Tag.objects.filter(name__in=tags)
    if tags_objs:
        user.usertag.tags.clear()
        user.usertag.tags.add(*tags_objs)
    user.save()


def get_user(email):
    user, created = User.objects.get_or_create(username=email)
    if created:
        UserTag.objects.create(user=user)
    return user
