-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathutils.py
103 lines (91 loc) · 3.25 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import re
import django
from django.utils.text import wrap
from django.utils.translation import ugettext, ugettext_lazy as _
from django.template.loader import render_to_string
from django.conf import settings
# favour django-mailer but fall back to django.core.mail
if "mailer" in settings.INSTALLED_APPS:
from mailer import send_mail
else:
from django.core.mail import send_mail
def format_quote(sender, body):
"""
Wraps text at 55 chars and prepends each
line with `> `.
Used for quoting messages in replies.
"""
lines = wrap(body, 55).split('\n')
for i, line in enumerate(lines):
lines[i] = "> %s" % line
quote = '\n'.join(lines)
return ugettext(u"%(sender)s wrote:\n%(body)s") % {
'sender': sender,
'body': quote
}
def format_subject(subject):
"""
Prepends 'Re:' to the subject. To avoid multiple 'Re:'s
a counter is added.
NOTE: Currently unused. First step to fix Issue #48.
FIXME: Any hints how to make this i18n aware are very welcome.
"""
subject_prefix_re = r'^Re\[(\d*)\]:\ '
m = re.match(subject_prefix_re, subject, re.U)
prefix = u""
if subject.startswith('Re: '):
prefix = u"[2]"
subject = subject[4:]
elif m is not None:
try:
num = int(m.group(1))
prefix = u"[%d]" % (num+1)
subject = subject[6+len(str(num)):]
except:
# if anything fails here, fall back to the old mechanism
pass
return ugettext(u"Re%(prefix)s: %(subject)s") % {
'subject': subject,
'prefix': prefix
}
def new_message_email(sender, instance, signal,
subject_prefix=_(u'New Message: %(subject)s'),
template_name="django_messages/new_message.html",
default_protocol=None,
*args, **kwargs):
"""
This function sends an email and is called via Django's signal framework.
Optional arguments:
``template_name``: the template to use
``subject_prefix``: prefix for the email subject.
``default_protocol``: default protocol in site URL passed to template
"""
if default_protocol is None:
default_protocol = getattr(settings, 'DEFAULT_HTTP_PROTOCOL', 'http')
if 'created' in kwargs and kwargs['created']:
try:
from django.contrib.sites.models import Site
current_domain = Site.objects.get_current().domain
subject = subject_prefix % {'subject': instance.subject}
message = render_to_string(template_name, {
'site_url': '%s://%s' % (default_protocol, current_domain),
'message': instance,
})
if instance.recipient.email != "":
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
[instance.recipient.email,])
except Exception as e:
#print e
pass #fail silently
def get_user_model():
if django.VERSION[:2] >= (1, 5):
from django.contrib.auth import get_user_model
return get_user_model()
else:
from django.contrib.auth.models import User
return User
def get_username_field():
if django.VERSION[:2] >= (1, 5):
return get_user_model().USERNAME_FIELD
else:
return 'username'