# Path: utils/mail.py
# def send_mail(to_emails, from_email=settings.DEFAULT_FROM_EMAIL, subject='No subject',
#               message=None, template=None, context=None, fail_silently=True):
#     """Send email using Django's send_email function but setting a number of defaults and adding a
#     prefix in the subject. It also renders a template with a context if provided."""
#     if not subject.startswith(settings.EMAIL_SUBJECT_PREFIX):
#         subject = settings.EMAIL_SUBJECT_PREFIX + subject
#     if template is not None and context is not None:
#         message = render_to_string(template, context)
#     if type(to_emails) is not list and type(to_emails) is not tuple:
#         to_emails = (to_emails, )
#     django_send_email(subject, message, from_email, to_emails, fail_silently)
# 
# Path: utils/encryption.py
# def create_hash(data, add_secret=True, limit=16):
#     m = hashlib.sha256()
#     if add_secret:
#         m.update((str(data) + settings.SECRET_KEY).encode('utf-8'))
#     else:
#         m.update(str(data).encode('utf-8'))
#     return m.hexdigest()[0:limit]


from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import JSONField
from django.utils.translation import ugettext_lazy as _
from utils.mail import send_mail
from utils.encryption import create_hash


class Account(AbstractUser):
    """
    This is our custom 'User' model.
    """

    is_developer = models.BooleanField(default=False, blank=False)
    accepted_tos = models.BooleanField(default=False, blank=False)
    #  Add more custom fields here like avatar...

    def send_activation_email(self):
        tvars = {
            'user': self,
            'username': self.username,
            'hash': create_hash(self.id)
        }

# Path: utils/mail.py
# def send_mail(to_emails, from_email=settings.DEFAULT_FROM_EMAIL, subject='No subject',
#               message=None, template=None, context=None, fail_silently=True):
#     """Send email using Django's send_email function but setting a number of defaults and adding a
#     prefix in the subject. It also renders a template with a context if provided."""
#     if not subject.startswith(settings.EMAIL_SUBJECT_PREFIX):
#         subject = settings.EMAIL_SUBJECT_PREFIX + subject
#     if template is not None and context is not None:
#         message = render_to_string(template, context)
#     if type(to_emails) is not list and type(to_emails) is not tuple:
#         to_emails = (to_emails, )
#     django_send_email(subject, message, from_email, to_emails, fail_silently)
# 
# Path: utils/encryption.py
# def create_hash(data, add_secret=True, limit=16):
#     m = hashlib.sha256()
#     if add_secret:
#         m.update((str(data) + settings.SECRET_KEY).encode('utf-8'))
#     else:
#         m.update(str(data).encode('utf-8'))
#     return m.hexdigest()[0:limit]


# Path: accounts/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import JSONField
from django.utils.translation import ugettext_lazy as _
from utils.mail import send_mail
from utils.encryption import create_hash



class Account(AbstractUser):
    """
    This is our custom 'User' model.
    """

    is_developer = models.BooleanField(default=False, blank=False)
    accepted_tos = models.BooleanField(default=False, blank=False)
    #  Add more custom fields here like avatar...

    def send_activation_email(self):
        tvars = {
            'user': self,
            'username': self.username,
            'hash': create_hash(self.id)
        }

        send_mail(self.email, subject='Activate your Audio Commons user account',