change some settings for the demo environment
add error checks improve mail generation (date, to, from, ...)
This commit is contained in:
parent
806c07015b
commit
bfe5e3549f
2 changed files with 62 additions and 36 deletions
|
@ -19,22 +19,34 @@ import re
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
IMAGE_STORE = os.path.join(tempfile.gettempdir(), "fotokiste.jpg")
|
IMAGE_STORE = os.path.join(tempfile.gettempdir(), "fotokiste.jpg")
|
||||||
VIDEO_URL = "http://localhost:8081/?action=stream"
|
VIDEO_URL = "http://krake:8081/?action=stream"
|
||||||
SNAPSHOT_URL = "http://localhost:8081/?action=snapshot"
|
# 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_MAILADDRESS_CHARACTERS = "\w._%@-"
|
||||||
ALLOWED_MAILTEXT_CHARACTERS = "\w@_\-\.\s\n\#\(\)\[\]\{\}\|\>\<\,\+/\'\"\?\!\:=%\$^&\*"
|
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_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!"
|
MAIL_SUBJECT = "Ein Foto von der Fusion!"
|
||||||
SMTP_HOST = "localhost"
|
MAIL_ATTACHMENT_FILENAME = "fusion.jpeg"
|
||||||
|
SMTP_HOST = "192.168.1.31"
|
||||||
SMTP_PORT = "25"
|
SMTP_PORT = "25"
|
||||||
MAIL_MAX_LENGTH = 5000
|
MAIL_MAX_LENGTH = 5000
|
||||||
|
ERRORS_MAX_LENGTH = 25
|
||||||
|
|
||||||
|
errors = []
|
||||||
|
|
||||||
DEFAULT_DICT = { "video_url": VIDEO_URL }
|
DEFAULT_DICT = { "video_url": VIDEO_URL }
|
||||||
|
|
||||||
def debug(message):
|
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):
|
def merged_dicts(dict_a, dict_b):
|
||||||
return dict(dict_a.items() + dict_b.items())
|
return dict(dict_a.items() + dict_b.items())
|
||||||
|
@ -50,51 +62,62 @@ def filter_mailtext(text):
|
||||||
return filtered
|
return filtered
|
||||||
|
|
||||||
def check_mailaddress(address):
|
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
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def send_mail(address, text):
|
def send_mail(address, text):
|
||||||
import smtplib
|
import smtplib
|
||||||
import MimeWriter
|
|
||||||
import StringIO
|
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)
|
# read the additional mail parts (signature and picture)
|
||||||
try:
|
try:
|
||||||
signature = file(MAIL_SIGNATURE_FILE).read()
|
signature = file(MAIL_SIGNATURE_FILE).read()
|
||||||
except IOError, msg:
|
except IOError, msg:
|
||||||
debug("failed to open the signature file (%s): %s" % \
|
debug("failed to open the signature file (%s): %s" % \
|
||||||
(MAIL_SIGNATURE_FILE, msg))
|
(MAIL_SIGNATURE_FILE, msg))
|
||||||
|
return False
|
||||||
try:
|
try:
|
||||||
picture = StringIO.StringIO(file(IMAGE_STORE).read())
|
picture = file(IMAGE_STORE).read()
|
||||||
except IOError, msg:
|
except IOError, msg:
|
||||||
debug("failed to open the image file (%s): %s" % \
|
debug("failed to open the image file (%s): %s" % \
|
||||||
(IMAGE_STORE, msg))
|
(IMAGE_STORE, msg))
|
||||||
|
return False
|
||||||
|
|
||||||
# prepare the message
|
# prepare the message
|
||||||
message = StringIO.StringIO()
|
#mail_flat = StringIO.StringIO()
|
||||||
writer = MimeWriter.MimeWriter(message)
|
#mail_gen = email.Generator.Generator(mail_flat)
|
||||||
writer.addheader("Subject", MAIL_SUBJECT)
|
#mail_gen.flatten(body)
|
||||||
# the picture should be shown inline by the mail clients
|
mail = email.MIMEMultipart.MIMEMultipart()
|
||||||
writer.addheader("Content-Disposition", "inline")
|
mail_text = email.MIMEText.MIMEText(text + signature)
|
||||||
writer.startmultipartbody('mixed')
|
mail_pict = email.MIMEImage.MIMEImage(picture, "jpeg")
|
||||||
# start off with a text/plain part
|
mail_pict.add_header("Content-Disposition",
|
||||||
part = writer.nextpart()
|
'attachment; filename="%s"' % MAIL_ATTACHMENT_FILENAME)
|
||||||
body = part.startbody('text/plain')
|
mail.add_header("Subject", MAIL_SUBJECT)
|
||||||
body.write(text + signature)
|
mail.add_header("To", address)
|
||||||
# now add an attachment
|
mail.add_header("From", MAIL_FROM_ADDRESS)
|
||||||
part = writer.nextpart()
|
mail.add_header("Date", rfc822.formatdate())
|
||||||
# the 'Content-Disposition' is necessary for displaying the image inline
|
mail.add_header("Content-Disposition", "inline")
|
||||||
part.addheader('Content-Disposition', 'attachment; filename="fusion.jpeg"')
|
mail.attach(mail_text)
|
||||||
part.addheader('Content-Transfer-Encoding', 'base64')
|
mail.attach(mail_pict)
|
||||||
body = part.startbody('image/jpeg')
|
writer = StringIO.StringIO()
|
||||||
base64.encode(picture, body)
|
email.Generator.Generator(writer).flatten(mail)
|
||||||
# finish off
|
|
||||||
writer.lastpart()
|
|
||||||
|
|
||||||
|
# send the mail
|
||||||
|
try:
|
||||||
con = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
|
con = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
|
||||||
con.sendmail(address, MAIL_FROM_ADDRESS, message.getvalue())
|
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):
|
class Root(controllers.RootController):
|
||||||
|
@ -102,8 +125,7 @@ class Root(controllers.RootController):
|
||||||
@expose(template="fotokiste.templates.start")
|
@expose(template="fotokiste.templates.start")
|
||||||
def index(self, **kargs):
|
def index(self, **kargs):
|
||||||
|
|
||||||
# Bilder zufaellig aus der Datenbank auswaehlen
|
# TODO: this should generate a selection of random pictures
|
||||||
# eine andere Funktion muss die Bilder ausliefern
|
|
||||||
gallery = []
|
gallery = []
|
||||||
for i in range(22):
|
for i in range(22):
|
||||||
obj = DummyPicture()
|
obj = DummyPicture()
|
||||||
|
@ -111,7 +133,7 @@ class Root(controllers.RootController):
|
||||||
obj.url = "URL: %d" % i
|
obj.url = "URL: %d" % i
|
||||||
gallery.append(obj)
|
gallery.append(obj)
|
||||||
|
|
||||||
# alte Bild-Datei loeschen
|
# remove old picture
|
||||||
if os.path.isfile(IMAGE_STORE):
|
if os.path.isfile(IMAGE_STORE):
|
||||||
os.unlink(IMAGE_STORE)
|
os.unlink(IMAGE_STORE)
|
||||||
|
|
||||||
|
@ -123,6 +145,7 @@ class Root(controllers.RootController):
|
||||||
flash("Das Bild wird in 5 Sekunden aufgenommen!")
|
flash("Das Bild wird in 5 Sekunden aufgenommen!")
|
||||||
return merged_dicts({}, DEFAULT_DICT)
|
return merged_dicts({}, DEFAULT_DICT)
|
||||||
|
|
||||||
|
|
||||||
@expose(template="fotokiste.templates.mailtext")
|
@expose(template="fotokiste.templates.mailtext")
|
||||||
def mailtext(self, mailaddress="", mailtext="", already_stored="no", **kargs):
|
def mailtext(self, mailaddress="", mailtext="", already_stored="no", **kargs):
|
||||||
# store the picture if necessary
|
# store the picture if necessary
|
||||||
|
@ -141,6 +164,7 @@ class Root(controllers.RootController):
|
||||||
"already_stored": already_stored,
|
"already_stored": already_stored,
|
||||||
}, DEFAULT_DICT)
|
}, DEFAULT_DICT)
|
||||||
|
|
||||||
|
|
||||||
@expose(template="fotokiste.templates.senden")
|
@expose(template="fotokiste.templates.senden")
|
||||||
def senden(self, mailaddress="", mailtext="", senden=None):
|
def senden(self, mailaddress="", mailtext="", senden=None):
|
||||||
# filter input
|
# filter input
|
||||||
|
@ -159,9 +183,11 @@ class Root(controllers.RootController):
|
||||||
flash("Die Mailadresse scheint nicht ungueltig zu sein.")
|
flash("Die Mailadresse scheint nicht ungueltig zu sein.")
|
||||||
redirect("mailtext", return_dict)
|
redirect("mailtext", return_dict)
|
||||||
else:
|
else:
|
||||||
# Mail versenden
|
# send the mail
|
||||||
send_mail(mailaddress, mailtext)
|
if send_mail(mailaddress, mailtext):
|
||||||
return return_dict
|
return return_dict
|
||||||
|
else:
|
||||||
|
redirect("error")
|
||||||
|
|
||||||
@expose()
|
@expose()
|
||||||
def get_current_shot(self):
|
def get_current_shot(self):
|
||||||
|
|
BIN
fotokiste/fotokiste/static/images/fotokiste-default.jpg
Normal file
BIN
fotokiste/fotokiste/static/images/fotokiste-default.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.6 KiB |
Loading…
Reference in a new issue