fix: using MEDIA_URL inside templates.

wip: #36, using tinyMCE (django-tinymce) to write messages in HTML. Currently, compose a new message is working pretty well. Answering or forwarding a message is still a mess...
This commit is contained in:
tonio
2010-08-05 17:17:50 +02:00
parent 7629aca34c
commit 01fda22180
38 changed files with 191 additions and 78 deletions
+36 -1
View File
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import sys
import sys, os
reload(sys)
sys.setdefaultencoding("utf-8")
@@ -11,7 +11,9 @@ import ssl
from datetime import datetime, timedelta
from email.header import decode_header
import email.utils
import lxml
from django.utils.translation import ugettext as _
from django.conf import settings
from modoboa.lib import decode, tables, imap_utf7, Singleton
from modoboa.lib.email_listing import MBconnector, EmailListing, Email
from modoboa.lib import tables, imap_utf7, parameters
@@ -458,6 +460,39 @@ class ImapEmail(Email):
"attachments" : attachments != [] and attachments or None
})
def find_images_in_body(body):
"""Looks for images inside a HTML body
Before sending a message in HTML format, it is necessary to find
all img tags contained in the body in order to rewrite them. For
example, icons provided by tinyMCE are stored on the server
filesystem and not accessible from the outside. We must embark
them as parts off the MIME message if we want recipients to
display them correctly.
:param body: the HTML body to parse
"""
from email.mime.image import MIMEImage
html = lxml.html.fromstring(body)
parts = []
for tag in html.iter("img"):
src = tag.get("src")
fname = os.path.basename(src)
cid = "%s@modoboa" % os.path.splitext(fname)[0]
tag.set("src", "cid:%s" % cid)
path = "%s/%s" % (settings.MODOBOA_DIR, re.sub("\.\./", "", src))
fp = open(path, "rb")
p = MIMEImage(fp.read())
fp.close()
p["Content-ID"] = "<%s>" % cid
ct = p["Content-Type"]
p.replace_header("Content-Type", '%s; name="%s"' % (ct, fname))
parts.append(p)
return lxml.html.tostring(html), parts
def encrypt(clear):
from Crypto.Cipher import AES
import base64
+3
View File
@@ -40,6 +40,9 @@ def init():
help=_("Folder where copies of sent messages go"))
parameters.register_user("webmail", "SIGNATURE", type="text", default="",
help=_("User defined email signature"))
parameters.register_user("webmail", "EDITOR", type="list", deflt="text",
values=[("html", "html"), ("plain", "text")],
help=_("The default editor to use when composing a message"))
def destroy():
events.unregister("UserMenuDisplay", menu)
+2 -2
View File
@@ -5,11 +5,11 @@
{% include "common/genericform.html" %}
</div>
{% if body %}
<textarea id="id_body" name="id_body">{% if bodyheader %}{{ bodyheader }}{% endif %}
<textarea id="id_body" name="id_body" class="editor">{% if bodyheader %}{{ bodyheader }}{% endif %}
{{ body }}
</textarea>
{% else %}
<textarea id="id_body" name="id_body"></textarea>
<textarea id="id_body" name="id_body" class="editor"></textarea>
{% endif %}
</div>
</form>
+7 -1
View File
@@ -1,7 +1,7 @@
{% extends "common/email_listing.html" %}
{% load i18n %}
{% block css %}
<link rel="stylesheet" type="text/css" href="/static/css/webmail.css" />
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}/css/webmail.css" />
{% endblock %}
{% block js %}
quota_pb = new ProgressBar({
@@ -86,8 +86,14 @@
current_anchor.update();
}
});
tinyMCE.triggerSave();
$("composemail").send();
});
if (resp.editor == "html") {
new Request({url: "{% url tinymce-js 'common' %}",
onSuccess: function(resp) { eval(resp); }
}).get();
}
window.fireEvent("resize");
}
+18 -4
View File
@@ -211,12 +211,14 @@ def render_compose(request, form, posturl, bodyheader=None, body=None,
insert_signature=False):
menu = compose_menu("", __get_current_url(request),
request.user.get_all_permissions())
editor = parameters.get_user(request.user, "webmail", "EDITOR")
if insert_signature:
if body == None:
body = ""
signature = parameters.get_user(request.user, "webmail", "SIGNATURE")
if signature != "":
body += "\n---\n%s" % signature
body += editor == "plain" and "\n---\n%s" % signature \
or "<br/>---<br/>%s" % re.sub("\n", "<br/>", signature)
content = render_to_string("webmail/compose.html", {
"form" : form, "bodyheader" : bodyheader, "body" : body,
@@ -225,7 +227,8 @@ def render_compose(request, form, posturl, bodyheader=None, body=None,
mbc = IMAPconnector(user=request.user.username,
password=request.session["password"])
ctx = getctx("ok", level=2, menu=menu, listing=content,
folders=__render_folders(mbc, request))
folders=__render_folders(mbc, request),
editor=editor)
return HttpResponse(simplejson.dumps(ctx), mimetype="application/json")
def send_mail(request, withctx=False, origmsg=None, posturl=None):
@@ -237,8 +240,19 @@ def send_mail(request, withctx=False, origmsg=None, posturl=None):
from email.utils import make_msgid, formatdate
import smtplib
msg = MIMEText(request.POST["id_body"].encode("utf-8"),
_charset="utf-8")
subtype = parameters.get_user(request.user, "webmail", "EDITOR")
body = request.POST["id_body"]
if subtype == "html":
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart(_subtype="related")
body, images = find_images_in_body(body)
msg.attach(MIMEText(body.encode("utf-8"), _subtype=subtype,
_charset="utf-8"))
for img in images:
msg.attach(img)
else:
msg = MIMEText(body.encode("utf-8"), _subtype=subtype)
msg["Subject"] = request.POST["subject"]
msg["From"] = request.POST["from_"]
msg["To"] = request.POST["to"]