django-orchestra/orchestra/utils/options.py

67 lines
2.0 KiB
Python
Raw Normal View History

2014-07-09 16:17:43 +00:00
import sys
2015-04-02 16:14:55 +00:00
from urllib.parse import urlparse
2014-05-08 16:59:35 +00:00
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.template import Context
2014-09-04 15:55:43 +00:00
def send_email_template(template, context, to, email_from=None, html=None, attachments=[]):
2014-05-08 16:59:35 +00:00
"""
Renders an email template with this format:
{% if subject %}Subject{% endif %}
{% if message %}Email body{% endif %}
2014-09-04 15:55:43 +00:00
2014-05-08 16:59:35 +00:00
context can be a dictionary or a template.Context instance
"""
2014-09-04 15:55:43 +00:00
2014-05-08 16:59:35 +00:00
if isinstance(context, dict):
context = Context(context)
2015-04-02 16:14:55 +00:00
if isinstance(to, str):
2014-05-08 16:59:35 +00:00
to = [to]
2014-09-04 15:55:43 +00:00
2014-05-08 16:59:35 +00:00
if not 'site' in context:
from orchestra import settings
url = urlparse(settings.ORCHESTRA_SITE_URL)
2014-05-08 16:59:35 +00:00
context['site'] = {
'name': settings.ORCHESTRA_SITE_NAME,
2014-05-08 16:59:35 +00:00
'scheme': url.scheme,
'domain': url.netloc,
}
#subject cannot have new lines
subject = render_to_string(template, {'subject': True}, context).strip()
message = render_to_string(template, {'message': True}, context)
2014-09-04 15:55:43 +00:00
msg = EmailMultiAlternatives(subject, message, email_from, to, attachments=attachments)
2014-05-08 16:59:35 +00:00
if html:
html_message = render_to_string(html, {'message': True}, context)
msg.attach_alternative(html_message, "text/html")
msg.send()
2014-07-09 16:17:43 +00:00
def running_syncdb():
2014-09-26 15:05:20 +00:00
return 'migrate' in sys.argv or 'syncdb' in sys.argv or 'makemigrations' in sys.argv
2014-10-04 14:19:29 +00:00
def database_ready():
2015-04-30 09:51:55 +00:00
return (not running_syncdb() and
'setuppostgres' not in sys.argv and
'test' not in sys.argv and
'celerybeat' not in sys.argv and
len(sys.argv) <= 1 and
'--help' in sys.argv)
2014-10-24 10:16:46 +00:00
def dict_setting_to_choices(choices):
return sorted(
2015-04-02 16:14:55 +00:00
[ (name, opt.get('verbose_name', 'name')) for name, opt in choices.items() ],
2014-10-24 10:16:46 +00:00
key=lambda e: e[0]
)
def tuple_setting_to_choices(choices):
return sorted(
2015-04-02 16:14:55 +00:00
tuple((name, opt[0]) for name, opt in choices.items()),
2014-10-24 10:16:46 +00:00
key=lambda e: e[0]
)