# Path: della/email_service/core.py
# def send_email(subject, message, recipient_list):
#     send_mail(
#         subject=subject,
#         message=message,
#         html_message=message.replace('\n', '<br />'),
#         from_email=settings.SENDER_EMAIL,
#         recipient_list=recipient_list,
#     )
# 
# Path: della/user_manager/models.py
# class UserProfile(TimeStampMixin):
#     """
#     To manage users and stuff. From default `User` we will use `username`,
#     `email`, `password`, `first_name` and `last_name`.
#     """
#     bio = models.TextField(null=True)
#     address = models.TextField(null=True)
#     shipping_instructions = models.TextField(null=True)
#     is_enabled_exchange = models.BooleanField(default=False)
#     preferences = models.TextField(null=True)
#     fb_profile_url = models.URLField(null=True)
#     twitter_profile_url = models.URLField(null=True)
#     website_url = models.URLField(null=True)
#     wishlist_url = models.URLField(null=True)
#     avatar = models.ImageField(
#         upload_to=avatar_file_name, default='avatar.png')
# 
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     santee = models.OneToOneField(User, on_delete=models.CASCADE,  related_name='santa', null=True)
# 
#     def __str__(self):
#         return self.user.username


from django.template.loader import render_to_string
from django.urls import reverse
from della.email_service import send_email
from .models import UserProfile
from . import activation_service


def create_user_profile(user):
    UserProfile.objects.create(user=user, is_enabled_exchange=False)


def activate_user(user):
    if user.is_active:
        return True
    user.is_active = True
    user.save()
    return True


def enable_for_exchange(user):
    if user.userprofile.is_enabled_exchange:
        return True
    user.userprofile.is_enabled_exchange = True
    user.userprofile.save()
    return True


def send_activation_email(request, user):
    message_template = 'user_manager/account_activation_email.html'
    subject_temaplte = 'user_manager/account_activation_subject.txt'
    code = activation_service.generate_key(user)
    path_params = {'username': user.username, 'code': code}
    activation_url = request.build_absolute_uri(reverse(
        'user_manager:activate-user', kwargs=path_params))
    context = {'url': activation_url, 'username': user.username}

# Path: della/email_service/core.py
# def send_email(subject, message, recipient_list):
#     send_mail(
#         subject=subject,
#         message=message,
#         html_message=message.replace('\n', '<br />'),
#         from_email=settings.SENDER_EMAIL,
#         recipient_list=recipient_list,
#     )
# 
# Path: della/user_manager/models.py
# class UserProfile(TimeStampMixin):
#     """
#     To manage users and stuff. From default `User` we will use `username`,
#     `email`, `password`, `first_name` and `last_name`.
#     """
#     bio = models.TextField(null=True)
#     address = models.TextField(null=True)
#     shipping_instructions = models.TextField(null=True)
#     is_enabled_exchange = models.BooleanField(default=False)
#     preferences = models.TextField(null=True)
#     fb_profile_url = models.URLField(null=True)
#     twitter_profile_url = models.URLField(null=True)
#     website_url = models.URLField(null=True)
#     wishlist_url = models.URLField(null=True)
#     avatar = models.ImageField(
#         upload_to=avatar_file_name, default='avatar.png')
# 
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     santee = models.OneToOneField(User, on_delete=models.CASCADE,  related_name='santa', null=True)
# 
#     def __str__(self):
#         return self.user.username


# Path: della/user_manager/user_service.py
from django.template.loader import render_to_string
from django.urls import reverse
from della.email_service import send_email
from .models import UserProfile
from . import activation_service



def create_user_profile(user):
    UserProfile.objects.create(user=user, is_enabled_exchange=False)


def activate_user(user):
    if user.is_active:
        return True
    user.is_active = True
    user.save()
    return True


def enable_for_exchange(user):
    if user.userprofile.is_enabled_exchange:
        return True
    user.userprofile.is_enabled_exchange = True
    user.userprofile.save()
    return True


def send_activation_email(request, user):
    message_template = 'user_manager/account_activation_email.html'
    subject_temaplte = 'user_manager/account_activation_subject.txt'
    code = activation_service.generate_key(user)
    path_params = {'username': user.username, 'code': code}
    activation_url = request.build_absolute_uri(reverse(
        'user_manager:activate-user', kwargs=path_params))
    context = {'url': activation_url, 'username': user.username}

    message = render_to_string(template_name=message_template, context=context)