<commit_before>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<commit_msg>Rename `get_user` to `get_or_create_user` and add a new `get_user`<commit_after>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_or_create_user(email):
    user, created = User.objects.get_or_create(username=email)
    if created:
        Subscription.objects.create(user=user)
    return user


def get_user(email):
    return User.objects.filter(username=email).first()


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


def update_subscription(user, status):
    user.subscription.is_subscribed = status
    user.save()
{'fraction_non_alphanumeric': 0.08798714343109683, 'fraction_numerical': 0.0032141422257934912, 'mean_word_length': 4.1234567901234565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '512138', 'n_tokens_mistral': 747, 'n_tokens_neox': 738, 'n_words': 133}