# Path: app/models.py
# class TheUser(models.Model):
#     """
#     Class for user objects in database.
#     """
#     REMINDER_TEMPLATE = json.dumps({
#         "common": {
#             "fb_page": True,
#             "fb_group": True,
#             "twitter": True,
#             "vk": True,
#             "disabled_all": False
#         },
#         "api": {
#             "app_rate": True
#         },
#         "web": {
#             "app_download": True,
#         },
#     })
# 
#     id_user = models.OneToOneField(User)
#     user_photo = models.ImageField(blank=True, upload_to='user', storage=OverwriteStorage())
#     auth_token = models.CharField(max_length=50, null=True, blank=True)
#     subscription = models.BooleanField(default=True)
#     reminder = models.CharField(max_length=256, null=False, default=REMINDER_TEMPLATE)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def __str__(self):
#         return str(self.id_user)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_api_reminders(self):
#         """
#         Returns the reminders only necessary for API endpoints.
#         """
#         data = json.loads(self.reminder)
# 
#         mobile_data = dict(data['common'])
#         mobile_data.update(data['api'])
# 
#         return mobile_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_web_reminders(self):
#         """
#         Returns the reminders only necessary for web part.
#         """
#         data = json.loads(self.reminder)
#         
#         web_data = dict(data['common'])
#         web_data.update(data['web'])
# 
#         return web_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def update_reminder(self, field, value):
#         """
#         Updates the reminder status.
#         """
#         data = json.loads(self.reminder)
# 
#         for key in data:
#             if field in data[key]:
#                 data[key][field] = value
# 
#         self.reminder = json.dumps(data)
#         self.save()


import logging
import time
from celery import shared_task
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.urls.exceptions import NoReverseMatch
from django.utils.html import strip_tags
from .models import TheUser

# ----------------------------------------------------------------------------------------------------------------------
@shared_task
def changed_password(username, recipient):
    """
    Celery task for sending mail, to notify user about password changed.

    :param str username:   The restored username.
    :param str recipient:  The mail recipient.
    """
    html_content = render_to_string(
        'mails/password_changed.html',
        {'username': username, 'email_host_user': settings.EMAIL_HOST_USER}
    )
    text_content = strip_tags(html_content)
    subject = 'Изменение пароля аккаунта - plamber.com.ua'

    email = EmailMultiAlternatives(subject, text_content, to=[recipient])
    email.attach_alternative(html_content, 'text/html')
    email.send()

    logger.info("Sent changed password message to '{}'.".format(recipient))


# ----------------------------------------------------------------------------------------------------------------------
@shared_task
def email_dispatch(heading, text):
    """
    Dispatches new project post data to multiple subscribed users.
    """

# Path: app/models.py
# class TheUser(models.Model):
#     """
#     Class for user objects in database.
#     """
#     REMINDER_TEMPLATE = json.dumps({
#         "common": {
#             "fb_page": True,
#             "fb_group": True,
#             "twitter": True,
#             "vk": True,
#             "disabled_all": False
#         },
#         "api": {
#             "app_rate": True
#         },
#         "web": {
#             "app_download": True,
#         },
#     })
# 
#     id_user = models.OneToOneField(User)
#     user_photo = models.ImageField(blank=True, upload_to='user', storage=OverwriteStorage())
#     auth_token = models.CharField(max_length=50, null=True, blank=True)
#     subscription = models.BooleanField(default=True)
#     reminder = models.CharField(max_length=256, null=False, default=REMINDER_TEMPLATE)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def __str__(self):
#         return str(self.id_user)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_api_reminders(self):
#         """
#         Returns the reminders only necessary for API endpoints.
#         """
#         data = json.loads(self.reminder)
# 
#         mobile_data = dict(data['common'])
#         mobile_data.update(data['api'])
# 
#         return mobile_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_web_reminders(self):
#         """
#         Returns the reminders only necessary for web part.
#         """
#         data = json.loads(self.reminder)
#         
#         web_data = dict(data['common'])
#         web_data.update(data['web'])
# 
#         return web_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def update_reminder(self, field, value):
#         """
#         Updates the reminder status.
#         """
#         data = json.loads(self.reminder)
# 
#         for key in data:
#             if field in data[key]:
#                 data[key][field] = value
# 
#         self.reminder = json.dumps(data)
#         self.save()


# Path: app/tasks.py
import logging
import time
from celery import shared_task
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.urls.exceptions import NoReverseMatch
from django.utils.html import strip_tags
from .models import TheUser


# ----------------------------------------------------------------------------------------------------------------------
@shared_task
def changed_password(username, recipient):
    """
    Celery task for sending mail, to notify user about password changed.

    :param str username:   The restored username.
    :param str recipient:  The mail recipient.
    """
    html_content = render_to_string(
        'mails/password_changed.html',
        {'username': username, 'email_host_user': settings.EMAIL_HOST_USER}
    )
    text_content = strip_tags(html_content)
    subject = 'Изменение пароля аккаунта - plamber.com.ua'

    email = EmailMultiAlternatives(subject, text_content, to=[recipient])
    email.attach_alternative(html_content, 'text/html')
    email.send()

    logger.info("Sent changed password message to '{}'.".format(recipient))


# ----------------------------------------------------------------------------------------------------------------------
@shared_task
def email_dispatch(heading, text):
    """
    Dispatches new project post data to multiple subscribed users.
    """

    recipients = TheUser.objects.filter(subscription=True)