from django.contrib.auth.models import User

from nightreads.posts.models import Tag
from .models import (Subscription, SubscriptionActivation,
                     UnsubscriptionActivation)


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


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


def generate_subscribe_key(user):
    subscribe_key = User.objects.make_random_password(length=80)
    SubscriptionActivation.objects.update_or_create(
        user=user, defaults={'subscribe_key': subscribe_key})
    return subscribe_key


def generate_unsubscribe_key(user):
    unsubscribe_key = User.objects.make_random_password(length=80)
    UnsubscriptionActivation.objects.update_or_create(
        user=user, defaults={'unsubscribe_key': unsubscribe_key})
    return unsubscribe_key

@@ -441,16 +441,26 @@
 def get_
+or_create_
 user(ema
@@ -608,16 +608,94 @@
  user%0A%0A%0A
+def get_user(email):%0A    return User.objects.filter(username=email).first()%0A%0A%0A
 def gene
@@ -1166,20 +1166,122 @@
 urn unsubscribe_key%0A
+%0A%0Adef update_subscription(user, status):%0A    user.subscription.is_subscribed = status%0A    user.save()%0A

Rename `get_user` to `get_or_create_user` and add a new `get_user`