# Path: lkdcourses/mailing/models.py
# class MailLog(models.Model):
#     to = models.ForeignKey("profiles.UserProfile", related_name="mail_profile")
#     type = models.CharField(choices=MAIL_TEMPLATES, max_length=32)
#     sent_at = models.DateTimeField(default=timezone.now)
#     mandrill_response_id = models.CharField(max_length=255, null=True, blank=True)
#     mandrill_status = models.CharField(max_length=255, null=True, blank=True)


from django.core.mail import EmailMessage
from django.conf import settings
from .models import MailLog

    if not from_email:
        from_email = settings.MAILING_FROM_EMAIL

    if not from_name:
        from_name = settings.MAILING_FROM_NAME

    msg.from_email = from_email
    msg.from_name = from_name

    msg.send()

    mandrill_id, mandrill_status = "-1", "unknown"
    try:
        if msg.mandrill_response and len(msg.mandrill_response):
            mandrill_id, mandrill_status = msg.mandrill_response[0].get("_id"), msg.mandrill_response[0].get("status")
    except Exception as error:
        pass

    mail_log = MailLog(
        to=to_profile,
        type=log_type,
        mandrill_response_id=mandrill_id,
        mandrill_status=mandrill_status,
    )

    mail_log.save()


def mail_is_sent_before(to_profile, template_name):

# Path: lkdcourses/mailing/models.py
# class MailLog(models.Model):
#     to = models.ForeignKey("profiles.UserProfile", related_name="mail_profile")
#     type = models.CharField(choices=MAIL_TEMPLATES, max_length=32)
#     sent_at = models.DateTimeField(default=timezone.now)
#     mandrill_response_id = models.CharField(max_length=255, null=True, blank=True)
#     mandrill_status = models.CharField(max_length=255, null=True, blank=True)


# Path: lkdcourses/mailing/helpers.py
from django.core.mail import EmailMessage
from django.conf import settings
from .models import MailLog


    if not from_email:
        from_email = settings.MAILING_FROM_EMAIL

    if not from_name:
        from_name = settings.MAILING_FROM_NAME

    msg.from_email = from_email
    msg.from_name = from_name

    msg.send()

    mandrill_id, mandrill_status = "-1", "unknown"
    try:
        if msg.mandrill_response and len(msg.mandrill_response):
            mandrill_id, mandrill_status = msg.mandrill_response[0].get("_id"), msg.mandrill_response[0].get("status")
    except Exception as error:
        pass

    mail_log = MailLog(
        to=to_profile,
        type=log_type,
        mandrill_response_id=mandrill_id,
        mandrill_status=mandrill_status,
    )

    mail_log.save()


def mail_is_sent_before(to_profile, template_name):

    return MailLog.objects.filter(to=to_profile, type=template_name).count() > 0