1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-20 19:08:21 +00:00

[html email] use 'send_html_email()' for all emails

This commit is contained in:
llj
2014-02-19 20:03:55 +08:00
parent e4e11e9792
commit e0903ffc60
10 changed files with 27 additions and 98 deletions

View File

@@ -1,5 +1,4 @@
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.template import Context, loader
from django import forms from django import forms
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.http import int_to_base36 from django.utils.http import int_to_base36
@@ -7,7 +6,7 @@ from django.utils.http import int_to_base36
from seahub.base.accounts import User from seahub.base.accounts import User
from seahub.auth import authenticate from seahub.auth import authenticate
from seahub.auth.tokens import default_token_generator from seahub.auth.tokens import default_token_generator
from seahub.utils import IS_EMAIL_CONFIGURED, get_service_url from seahub.utils import IS_EMAIL_CONFIGURED, send_html_email
from captcha.fields import CaptchaField from captcha.fields import CaptchaField
@@ -86,7 +85,6 @@ class PasswordResetForm(forms.Form):
""" """
Generates a one-use only link for resetting password and sends to the user Generates a one-use only link for resetting password and sends to the user
""" """
from django.core.mail import EmailMessage
user = self.users_cache user = self.users_cache
if not domain_override: if not domain_override:
@@ -95,24 +93,15 @@ class PasswordResetForm(forms.Form):
else: else:
site_name = domain_override site_name = domain_override
service_url = get_service_url()
t = loader.get_template(email_template_name)
c = { c = {
'email': user.username, 'email': user.username,
'site_name': site_name,
'uid': int_to_base36(user.id), 'uid': int_to_base36(user.id),
'user': user, 'user': user,
'token': token_generator.make_token(user), 'token': token_generator.make_token(user),
'service_url': service_url,
'media_url': settings.MEDIA_URL,
'logo_path': settings.LOGO_PATH,
} }
msg = EmailMessage(_("Reset Password on %s") % site_name, send_html_email(_("Reset Password on %s") % site_name,
t.render(Context(c)), None, [user.username]) email_template_name, c, None, [user.username])
msg.content_subtype = "html" # Main content is now text/html
msg.send()
class SetPasswordForm(forms.Form): class SetPasswordForm(forms.Form):
""" """

View File

@@ -6,7 +6,6 @@ import simplejson as json
import urllib2 import urllib2
from django.conf import settings from django.conf import settings
from django.core.mail import EmailMessage
from django.core.paginator import EmptyPage, InvalidPage from django.core.paginator import EmptyPage, InvalidPage
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.contrib import messages from django.contrib import messages
@@ -50,11 +49,11 @@ from seahub.wiki import get_group_wiki_repo, get_group_wiki_page, convert_wiki_l
get_wiki_pages get_wiki_pages
from seahub.wiki.models import WikiDoesNotExist, WikiPageMissing, GroupWiki from seahub.wiki.models import WikiDoesNotExist, WikiPageMissing, GroupWiki
from seahub.wiki.utils import clean_page_name, get_wiki_dirent from seahub.wiki.utils import clean_page_name, get_wiki_dirent
from seahub.settings import SITE_ROOT, SITE_NAME, MEDIA_URL, LOGO_PATH from seahub.settings import SITE_ROOT, SITE_NAME
from seahub.shortcuts import get_first_object_or_none from seahub.shortcuts import get_first_object_or_none
from seahub.utils import render_error, render_permission_error, string2list, \ from seahub.utils import render_error, render_permission_error, string2list, \
check_and_get_org_by_group, gen_file_get_url, get_file_type_and_ext, \ check_and_get_org_by_group, gen_file_get_url, get_file_type_and_ext, \
calc_file_path_hash, is_valid_username, get_service_url, send_html_email calc_file_path_hash, is_valid_username, send_html_email
from seahub.utils.file_types import IMAGE from seahub.utils.file_types import IMAGE
from seahub.utils.paginator import Paginator from seahub.utils.paginator import Paginator
from seahub.views import is_registered_user from seahub.views import is_registered_user

View File

@@ -3,13 +3,12 @@ import datetime
import logging import logging
import string import string
from django.core.mail import EmailMessage
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from seahub.base.models import CommandsLastCheck from seahub.base.models import CommandsLastCheck
from seahub.notifications.models import UserNotification from seahub.notifications.models import UserNotification
from seahub.utils import get_service_url from seahub.utils import send_html_email
import seahub.settings as settings import seahub.settings as settings
from django.template import Context, loader from django.template import Context, loader
@@ -19,9 +18,6 @@ logger = logging.getLogger(__name__)
site_name = settings.SITE_NAME site_name = settings.SITE_NAME
subjects = (u'New notice on %s' % site_name, u'New notices on %s' % site_name) subjects = (u'New notice on %s' % site_name, u'New notices on %s' % site_name)
service_url = get_service_url()
media_url = settings.MEDIA_URL
logo_path = settings.LOGO_PATH
class Command(BaseCommand): class Command(BaseCommand):
help = 'Send Email notifications to user if he/she has an unread notices every period of seconds .' help = 'Send Email notifications to user if he/she has an unread notices every period of seconds .'
@@ -63,21 +59,15 @@ class Command(BaseCommand):
for to_user, count in email_ctx.items(): for to_user, count in email_ctx.items():
subject = subjects[1] if count > 1 else subjects[0] subject = subjects[1] if count > 1 else subjects[0]
t = loader.get_template('notifications/notice_email.html')
c = { c = {
'site_name': site_name,
'media_url': media_url,
'logo_path': logo_path,
'service_url': service_url,
'to_user': to_user, 'to_user': to_user,
'notice_count': count, 'notice_count': count,
} }
try: try:
msg = EmailMessage(subject, t.render(Context(c)), settings.DEFAULT_FROM_EMAIL, send_html_email(subject, 'notifications/notice_email.html', c,
[to_user]) settings.DEFAULT_FROM_EMAIL, [to_user])
msg.content_subtype = "html"
msg.send()
logger.info('Successfully sent email to %s' % to_user) logger.info('Successfully sent email to %s' % to_user)
except Exception, e: except Exception, e:

View File

@@ -17,7 +17,7 @@ You've got {{num}} new notices on {{ site_name }}.
<p style="font-size:14px;color:#434144;"> <p style="font-size:14px;color:#434144;">
{% trans "Go check out at the following page:" %}<br /> {% trans "Go check out at the following page:" %}<br />
{{ service_url }}{% url 'user_notification_list' %} {{ url_base }}{% url 'user_notification_list' %}
</p> </p>
{% endautoescape %} {% endautoescape %}

View File

@@ -2,7 +2,6 @@
import os import os
import logging import logging
import simplejson as json import simplejson as json
from django.core.mail import EmailMessage
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db import IntegrityError from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect, Http404, \ from django.http import HttpResponse, HttpResponseRedirect, Http404, \
@@ -37,7 +36,7 @@ from seahub.views import validate_owner, is_registered_user
from seahub.utils import render_permission_error, string2list, render_error, \ from seahub.utils import render_permission_error, string2list, render_error, \
gen_token, gen_shared_link, gen_shared_upload_link, gen_dir_share_link, \ gen_token, gen_shared_link, gen_shared_upload_link, gen_dir_share_link, \
gen_file_share_link, IS_EMAIL_CONFIGURED, check_filename_with_rename, \ gen_file_share_link, IS_EMAIL_CONFIGURED, check_filename_with_rename, \
get_repo_last_modify, is_valid_username, get_service_url get_repo_last_modify, is_valid_username, send_html_email
import seahub.settings as settings import seahub.settings as settings
try: try:
@@ -793,29 +792,21 @@ def send_shared_link(request):
email = form.cleaned_data['email'] email = form.cleaned_data['email']
file_shared_link = form.cleaned_data['file_shared_link'] file_shared_link = form.cleaned_data['file_shared_link']
t = loader.get_template('shared_link_email.html')
to_email_list = string2list(email) to_email_list = string2list(email)
for to_email in to_email_list: for to_email in to_email_list:
# Add email to contacts. # Add email to contacts.
mail_sended.send(sender=None, user=request.user.username, mail_sended.send(sender=None, user=request.user.username,
email=to_email) email=to_email)
service_url = get_service_url()
c = { c = {
'email': request.user.username, 'email': request.user.username,
'to_email': to_email, 'to_email': to_email,
'file_shared_link': file_shared_link, 'file_shared_link': file_shared_link,
'site_name': SITE_NAME,
'service_url': service_url,
'media_url': settings.MEDIA_URL,
'logo_path': settings.LOGO_PATH,
} }
try: try:
msg = EmailMessage(_(u'A file is shared to you on s%') % SITE_NAME, send_html_email(_(u'A file is shared to you on %s') % SITE_NAME,
t.render(Context(c)), None, [to_email]) 'shared_link_email.html', c, None, [to_email])
msg.content_subtype = "html"
msg.send()
except Exception, e: except Exception, e:
logger.error(str(e)) logger.error(str(e))
data = json.dumps({'error':_(u'Internal server error. Send failed.')}) data = json.dumps({'error':_(u'Internal server error. Send failed.')})
@@ -1072,29 +1063,21 @@ def send_shared_upload_link(request):
email = form.cleaned_data['email'] email = form.cleaned_data['email']
shared_upload_link = form.cleaned_data['shared_upload_link'] shared_upload_link = form.cleaned_data['shared_upload_link']
t = loader.get_template('shared_upload_link_email.html')
to_email_list = string2list(email) to_email_list = string2list(email)
for to_email in to_email_list: for to_email in to_email_list:
# Add email to contacts. # Add email to contacts.
mail_sended.send(sender=None, user=request.user.username, mail_sended.send(sender=None, user=request.user.username,
email=to_email) email=to_email)
service_url = get_service_url()
c = { c = {
'email': request.user.username, 'email': request.user.username,
'to_email': to_email, 'to_email': to_email,
'shared_upload_link': shared_upload_link, 'shared_upload_link': shared_upload_link,
'site_name': SITE_NAME,
'service_url': service_url,
'media_url': settings.MEDIA_URL,
'logo_path': settings.LOGO_PATH,
} }
try: try:
msg = EmailMessage(_(u'An upload link is shared to you on s%') % SITE_NAME, send_html_email(_(u'An upload link is shared to you on %s') % SITE_NAME,
t.render(Context(c)), None, [to_email]) 'shared_upload_link_email.html', c, None, [to_email])
msg.content_subtype = "html"
msg.send()
except Exception, e: except Exception, e:
logger.error(str(e)) logger.error(str(e))
data = json.dumps({'error':_(u'Internal server error. Send failed.')}) data = json.dumps({'error':_(u'Internal server error. Send failed.')})

View File

@@ -8,7 +8,7 @@
{% if validlink %} {% if validlink %}
<div class="new-narrow-panel"> <div class="new-narrow-panel">
<h2 class="hd">{% trans "Input new password" %}</h2> <h2 class="hd">{% trans "Reset Password" %}</h2>
<form action="" method="post" class="con">{% csrf_token %} <form action="" method="post" class="con">{% csrf_token %}
<label for="id_new_password1">{% trans "New Password: " %}</label> <label for="id_new_password1">{% trans "New Password: " %}</label>
{{ form.new_password1 }} {{ form.new_password1 }}

View File

@@ -10,7 +10,7 @@
<p style="font-size:14px;color:#434144;"> <p style="font-size:14px;color:#434144;">
{% blocktrans with account=user.username %}To reset the password of your account {{ account }} on {{ site_name }}, please click the following link: {% endblocktrans %}<br /> {% blocktrans with account=user.username %}To reset the password of your account {{ account }} on {{ site_name }}, please click the following link: {% endblocktrans %}<br />
{{ service_url }}{% url 'auth_password_reset_confirm' uidb36=uid token=token %} {{ url_base }}{% url 'auth_password_reset_confirm' uidb36=uid token=token %}
</p> </p>
<p style="font-size:14px;color:#434144;"> <p style="font-size:14px;color:#434144;">

View File

@@ -12,7 +12,7 @@
{% blocktrans %}Your account {{ username }} on {{ site_name }} has been activated.{% endblocktrans %} {% blocktrans %}Your account {{ username }} on {{ site_name }} has been activated.{% endblocktrans %}
</p> </p>
<a href="{{ service_url }}{% url 'auth_login' %}" target="_blank">{% trans "Log In" %}</a> <a href="{{ url_base }}{% url 'auth_login' %}" target="_blank">{% trans "Log In" %}</a>
{% endautoescape %} {% endautoescape %}

View File

@@ -22,7 +22,7 @@
{% blocktrans %}Password: {{ password }}{% endblocktrans %} {% blocktrans %}Password: {{ password }}{% endblocktrans %}
</p> </p>
<a href="{{ service_url }}{% url 'auth_login' %}" target="_blank">{% trans "Log In" %}</a> <a href="{{ url_base }}{% url 'auth_login' %}" target="_blank">{% trans "Log In" %}</a>
{% endautoescape %} {% endautoescape %}

View File

@@ -6,7 +6,6 @@ import logging
import simplejson as json import simplejson as json
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.mail import EmailMessage
from django.contrib import messages from django.contrib import messages
from django.http import HttpResponse, Http404, HttpResponseRedirect from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
@@ -31,9 +30,9 @@ from seahub.profile.models import Profile, DetailedProfile
from seahub.share.models import FileShare from seahub.share.models import FileShare
import seahub.settings as settings import seahub.settings as settings
from seahub.settings import INIT_PASSWD, \ from seahub.settings import INIT_PASSWD, SITE_NAME, \
SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER, SEND_EMAIL_ON_RESETTING_USER_PASSWD SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER, SEND_EMAIL_ON_RESETTING_USER_PASSWD
from seahub.utils import get_site_scheme_and_netloc, get_service_url from seahub.utils import send_html_email
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -407,20 +406,11 @@ def user_deactivate(request, user_id):
def email_user_on_activation(user): def email_user_on_activation(user):
"""Send an email to user when admin activate his/her account. """Send an email to user when admin activate his/her account.
""" """
service_url = get_service_url()
site_name = settings.SITE_NAME
t = loader.get_template('sysadmin/user_activation_email.html')
c = { c = {
'site_name': site_name,
'media_url': settings.MEDIA_URL,
'logo_path': settings.LOGO_PATH,
'service_url': service_url,
'username': user.email, 'username': user.email,
} }
msg = EmailMessage(_(u'Your account on %s is activated') % site_name, t.render(Context(c)), send_html_email(_(u'Your account on %s is activated') % SITE_NAME,
None, [user.email]) 'sysadmin/user_activation_email.html', c, None, [user.email])
msg.content_subtype = "html"
msg.send()
@login_required @login_required
@sys_staff_required @sys_staff_required
@@ -459,22 +449,12 @@ def send_user_reset_email(request, email, password):
Send email when reset user password. Send email when reset user password.
""" """
service_url = get_service_url()
site_name = settings.SITE_NAME
t = loader.get_template('sysadmin/user_reset_email.html')
c = { c = {
'email': email, 'email': email,
'password': password, 'password': password,
'site_name': site_name,
'media_url': settings.MEDIA_URL,
'logo_path': settings.LOGO_PATH,
'service_url': service_url,
} }
msg = EmailMessage(_(u'Password has been reset on %s') % site_name, t.render(Context(c)), send_html_email(_(u'Password has been reset on %s') % SITE_NAME,
None, [email]) 'sysadmin/user_reset_email.html', c, None, [email])
msg.content_subtype = "html"
msg.send()
@login_required @login_required
@sys_staff_required @sys_staff_required
@@ -515,26 +495,14 @@ def user_reset(request, user_id):
def send_user_add_mail(request, email, password): def send_user_add_mail(request, email, password):
"""Send email when add new user.""" """Send email when add new user."""
service_url = get_service_url()
site_name = settings.SITE_NAME
t = loader.get_template('sysadmin/user_add_email.html')
c = { c = {
'user': request.user.username, 'user': request.user.username,
'org': request.user.org, 'org': request.user.org,
'email': email, 'email': email,
'password': password, 'password': password,
'service_url': service_url,
'site_name': site_name,
'media_url': settings.MEDIA_URL,
'logo_path': settings.LOGO_PATH,
} }
msg = EmailMessage(_(u'You are invited to join %s') % site_name, t.render(Context(c)), send_html_email(_(u'You are invited to join %s') % SITE_NAME,
None, [email]) 'sysadmin/user_add_email.html', c, None, [email])
msg.content_subtype = "html"
msg.send()
@login_required @login_required
def user_add(request): def user_add(request):