# Path: konfera/models/email_template.py
# class EmailTemplate(KonferaModel):
#     name = models.CharField(max_length=255)
#     text_template = models.TextField()
#     html_template = models.TextField()
#     counter = models.IntegerField(default=0)
# 
#     def __str__(self):
#         return self.name
# 
#     def add_count(self, add=1):
#         self.counter += add
#         self.save()
# 
# Path: konfera/utils.py
# class EmailTemplateError(Exception):
#     pass
# 
# def validate_email_template(raw_template, formatting_dict, required=True):
#     required_keys = set(re.findall('{(.+?)}', raw_template))
#     if not required_keys.issubset(set(formatting_dict.keys())):
#         if required:
#             logger.critical('Not all required fields of the template were found in formatting dictionary.\n'
#                             'required:{} !~ formatting:{}'.format(required_keys, set(formatting_dict)))
#             raise EmailTemplateError('Not all required fields of the template were found in formatting dictionary.\n'
#                                      'required:{} !~ formatting:{}'.format(required_keys, set(formatting_dict)))
#         else:
#             logger.warning('Not all required fields of the template were found in formatting dictionary.')
#             return raw_template
# 
#     return raw_template.format(**formatting_dict)
# 
# def get_email_template(template_name):
#     try:
#         template = EmailTemplate.objects.get(name=template_name)
#         return template
#     except EmailTemplate.DoesNotExist:
#         raise EmailTemplateError('No such template: {}'.format(template_name))
# 
# def send_email(addresses, subject, template_name, formatting_dict=None, **kwargs):
#     formatting_dict = formatting_dict or {}
#     template = get_email_template(template_name)
#     text_template = getattr(template, 'text_template', '')
#     html_template = getattr(template, 'html_template', '')
# 
#     if not text_template:
#         logger.critical('Missing text template (required) for the input {}.'.format(text_template))
#         raise EmailTemplateError("Email template is not valid for the input.")
#     if not html_template:
#         logger.warning('Invalid html template (not required) for the input {}.'.format(html_template))
# 
#     text_content = validate_email_template(text_template, formatting_dict)
#     html_content = validate_email_template(html_template, formatting_dict, False)
# 
#     to = addresses.get('to', [])
#     cc = addresses.get('cc', [])
#     bcc = addresses.get('bcc', EMAIL_NOTIFY_BCC)
# 
#     msg = EmailMultiAlternatives(subject, text_content, to=to, cc=cc, bcc=bcc)
#     if html_content:
#         msg.attach_alternative(html_content, "text/html")
# 
#     try:
#         msg.send()
#     except (SMTPException, ConnectionRefusedError) as e:
#         logger.critical('Sending email raised an exception: %s', e)
#     else:
#         # increase count on email_template
#         template.add_count()
#         if kwargs.get('verbose', 0) > 1:
#             print(msg)
#         return True


from django.test import TestCase
from mock import patch
from konfera.models.email_template import EmailTemplate
from konfera.utils import EmailTemplateError, validate_email_template, get_email_template, send_email




class EmailUtilTest(TestCase):
    def setUp(self):
        self.addresses = {'to': ['noone@example.com']}
        self.subject = 'Subject'
        self.valid_input = {'name': 'Joe'}
        self.text_template = 'Hello {name}'
        self.empty = 'empty_template'

    def test_validate_email_template(self):
        invalid_input = {'Joe': 'name'}
        self.assertEqual(validate_email_template(self.text_template, self.valid_input), 'Hello Joe')
        self.assertRaises(EmailTemplateError, validate_email_template, *(self.text_template, invalid_input))
        self.assertEqual(validate_email_template(self.text_template, {}, False), 'Hello {name}')

    def test_get_email_template(self):
        template = 'Non existing test template'

# Path: konfera/models/email_template.py
# class EmailTemplate(KonferaModel):
#     name = models.CharField(max_length=255)
#     text_template = models.TextField()
#     html_template = models.TextField()
#     counter = models.IntegerField(default=0)
# 
#     def __str__(self):
#         return self.name
# 
#     def add_count(self, add=1):
#         self.counter += add
#         self.save()
# 
# Path: konfera/utils.py
# class EmailTemplateError(Exception):
#     pass
# 
# def validate_email_template(raw_template, formatting_dict, required=True):
#     required_keys = set(re.findall('{(.+?)}', raw_template))
#     if not required_keys.issubset(set(formatting_dict.keys())):
#         if required:
#             logger.critical('Not all required fields of the template were found in formatting dictionary.\n'
#                             'required:{} !~ formatting:{}'.format(required_keys, set(formatting_dict)))
#             raise EmailTemplateError('Not all required fields of the template were found in formatting dictionary.\n'
#                                      'required:{} !~ formatting:{}'.format(required_keys, set(formatting_dict)))
#         else:
#             logger.warning('Not all required fields of the template were found in formatting dictionary.')
#             return raw_template
# 
#     return raw_template.format(**formatting_dict)
# 
# def get_email_template(template_name):
#     try:
#         template = EmailTemplate.objects.get(name=template_name)
#         return template
#     except EmailTemplate.DoesNotExist:
#         raise EmailTemplateError('No such template: {}'.format(template_name))
# 
# def send_email(addresses, subject, template_name, formatting_dict=None, **kwargs):
#     formatting_dict = formatting_dict or {}
#     template = get_email_template(template_name)
#     text_template = getattr(template, 'text_template', '')
#     html_template = getattr(template, 'html_template', '')
# 
#     if not text_template:
#         logger.critical('Missing text template (required) for the input {}.'.format(text_template))
#         raise EmailTemplateError("Email template is not valid for the input.")
#     if not html_template:
#         logger.warning('Invalid html template (not required) for the input {}.'.format(html_template))
# 
#     text_content = validate_email_template(text_template, formatting_dict)
#     html_content = validate_email_template(html_template, formatting_dict, False)
# 
#     to = addresses.get('to', [])
#     cc = addresses.get('cc', [])
#     bcc = addresses.get('bcc', EMAIL_NOTIFY_BCC)
# 
#     msg = EmailMultiAlternatives(subject, text_content, to=to, cc=cc, bcc=bcc)
#     if html_content:
#         msg.attach_alternative(html_content, "text/html")
# 
#     try:
#         msg.send()
#     except (SMTPException, ConnectionRefusedError) as e:
#         logger.critical('Sending email raised an exception: %s', e)
#     else:
#         # increase count on email_template
#         template.add_count()
#         if kwargs.get('verbose', 0) > 1:
#             print(msg)
#         return True


# Path: konfera/tests/test_utils.py
from django.test import TestCase
from mock import patch
from konfera.models.email_template import EmailTemplate
from konfera.utils import EmailTemplateError, validate_email_template, get_email_template, send_email





class EmailUtilTest(TestCase):
    def setUp(self):
        self.addresses = {'to': ['noone@example.com']}
        self.subject = 'Subject'
        self.valid_input = {'name': 'Joe'}
        self.text_template = 'Hello {name}'
        self.empty = 'empty_template'

    def test_validate_email_template(self):
        invalid_input = {'Joe': 'name'}
        self.assertEqual(validate_email_template(self.text_template, self.valid_input), 'Hello Joe')
        self.assertRaises(EmailTemplateError, validate_email_template, *(self.text_template, invalid_input))
        self.assertEqual(validate_email_template(self.text_template, {}, False), 'Hello {name}')

    def test_get_email_template(self):
        template = 'Non existing test template'

        self.assertRaises(EmailTemplateError, get_email_template, template)