change some settings for the demo environment

add error checks
improve mail generation (date, to, from, ...)
This commit is contained in:
lars 2008-06-15 23:47:44 +00:00
parent 806c07015b
commit bfe5e3549f
2 changed files with 62 additions and 36 deletions

View file

@ -19,22 +19,34 @@ import re
import datetime
IMAGE_STORE = os.path.join(tempfile.gettempdir(), "fotokiste.jpg")
VIDEO_URL = "http://localhost:8081/?action=stream"
SNAPSHOT_URL = "http://localhost:8081/?action=snapshot"
VIDEO_URL = "http://krake:8081/?action=stream"
# Netzwerk-Problem beim xen-Switch ... - erstmal nur ein Standardbild
#SNAPSHOT_URL = "http://krake:8081/?action=snapshot"
SNAPSHOT_URL = "http://localhost:8080/static/images/fotokiste-default.jpg"
ALLOWED_MAILADDRESS_CHARACTERS = "\w._%@-"
ALLOWED_MAILTEXT_CHARACTERS = "\w@_\-\.\s\n\#\(\)\[\]\{\}\|\>\<\,\+/\'\"\?\!\:=%\$^&\*"
MAIL_ADDRESS_REGEX = r"^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,6}$"
# TODO: this path is relative - to be fixed!
MAIL_SIGNATURE_FILE = "mail_signature.txt"
MAIL_FROM_ADDRESS = "fotokiste@glasmensch.org"
MAIL_FROM_ADDRESS = '"Fusion-Fotokiste" <fotokiste@glasmensch.org>'
MAIL_SUBJECT = "Ein Foto von der Fusion!"
SMTP_HOST = "localhost"
MAIL_ATTACHMENT_FILENAME = "fusion.jpeg"
SMTP_HOST = "192.168.1.31"
SMTP_PORT = "25"
MAIL_MAX_LENGTH = 5000
ERRORS_MAX_LENGTH = 25
errors = []
DEFAULT_DICT = { "video_url": VIDEO_URL }
def debug(message):
print "%s: %s" % (datetime.datetime.now().isoformat(), message)
log_msg = "%s: %s" % (datetime.datetime.now().isoformat(), message)
errors.append(log_msg)
# remove old messages, if necessary
while len(errors) > ERRORS_MAX_LENGTH:
del errors[0]
print log_msg
def merged_dicts(dict_a, dict_b):
return dict(dict_a.items() + dict_b.items())
@ -50,51 +62,62 @@ def filter_mailtext(text):
return filtered
def check_mailaddress(address):
if re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,6}$", address):
if re.match(MAIL_ADDRESS_REGEX, address):
return True
else:
return False
def send_mail(address, text):
import smtplib
import MimeWriter
import StringIO
import base64
import email.Generator
import email.MIMEMultipart
import email.MIMEText
import email.MIMEImage
import rfc822
# read the additional mail parts (signature and picture)
try:
signature = file(MAIL_SIGNATURE_FILE).read()
except IOError, msg:
debug("failed to open the signature file (%s): %s" % \
(MAIL_SIGNATURE_FILE, msg))
return False
try:
picture = StringIO.StringIO(file(IMAGE_STORE).read())
picture = file(IMAGE_STORE).read()
except IOError, msg:
debug("failed to open the image file (%s): %s" % \
(IMAGE_STORE, msg))
return False
# prepare the message
message = StringIO.StringIO()
writer = MimeWriter.MimeWriter(message)
writer.addheader("Subject", MAIL_SUBJECT)
# the picture should be shown inline by the mail clients
writer.addheader("Content-Disposition", "inline")
writer.startmultipartbody('mixed')
# start off with a text/plain part
part = writer.nextpart()
body = part.startbody('text/plain')
body.write(text + signature)
# now add an attachment
part = writer.nextpart()
# the 'Content-Disposition' is necessary for displaying the image inline
part.addheader('Content-Disposition', 'attachment; filename="fusion.jpeg"')
part.addheader('Content-Transfer-Encoding', 'base64')
body = part.startbody('image/jpeg')
base64.encode(picture, body)
# finish off
writer.lastpart()
#mail_flat = StringIO.StringIO()
#mail_gen = email.Generator.Generator(mail_flat)
#mail_gen.flatten(body)
mail = email.MIMEMultipart.MIMEMultipart()
mail_text = email.MIMEText.MIMEText(text + signature)
mail_pict = email.MIMEImage.MIMEImage(picture, "jpeg")
mail_pict.add_header("Content-Disposition",
'attachment; filename="%s"' % MAIL_ATTACHMENT_FILENAME)
mail.add_header("Subject", MAIL_SUBJECT)
mail.add_header("To", address)
mail.add_header("From", MAIL_FROM_ADDRESS)
mail.add_header("Date", rfc822.formatdate())
mail.add_header("Content-Disposition", "inline")
mail.attach(mail_text)
mail.attach(mail_pict)
writer = StringIO.StringIO()
email.Generator.Generator(writer).flatten(mail)
con = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
con.sendmail(address, MAIL_FROM_ADDRESS, message.getvalue())
# send the mail
try:
con = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
con.sendmail(address, MAIL_FROM_ADDRESS, writer.getvalue())
con.quit()
except (smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused,
smtplib.SMTPSenderRefused, smtplib.SMTPDataError), msg:
debug("an error occoured while sending the mail: %s" % msg)
return False
return True
class Root(controllers.RootController):
@ -102,8 +125,7 @@ class Root(controllers.RootController):
@expose(template="fotokiste.templates.start")
def index(self, **kargs):
# Bilder zufaellig aus der Datenbank auswaehlen
# eine andere Funktion muss die Bilder ausliefern
# TODO: this should generate a selection of random pictures
gallery = []
for i in range(22):
obj = DummyPicture()
@ -111,7 +133,7 @@ class Root(controllers.RootController):
obj.url = "URL: %d" % i
gallery.append(obj)
# alte Bild-Datei loeschen
# remove old picture
if os.path.isfile(IMAGE_STORE):
os.unlink(IMAGE_STORE)
@ -123,6 +145,7 @@ class Root(controllers.RootController):
flash("Das Bild wird in 5 Sekunden aufgenommen!")
return merged_dicts({}, DEFAULT_DICT)
@expose(template="fotokiste.templates.mailtext")
def mailtext(self, mailaddress="", mailtext="", already_stored="no", **kargs):
# store the picture if necessary
@ -141,6 +164,7 @@ class Root(controllers.RootController):
"already_stored": already_stored,
}, DEFAULT_DICT)
@expose(template="fotokiste.templates.senden")
def senden(self, mailaddress="", mailtext="", senden=None):
# filter input
@ -159,9 +183,11 @@ class Root(controllers.RootController):
flash("Die Mailadresse scheint nicht ungueltig zu sein.")
redirect("mailtext", return_dict)
else:
# Mail versenden
send_mail(mailaddress, mailtext)
return return_dict
# send the mail
if send_mail(mailaddress, mailtext):
return return_dict
else:
redirect("error")
@expose()
def get_current_shot(self):

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB