2008-06-15 19:37:08 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2008-06-09 00:58:23 +02:00
|
|
|
import turbogears as tg
|
2008-06-15 19:37:08 +02:00
|
|
|
from turbogears import controllers, expose, flash, redirect
|
2008-06-09 00:58:23 +02:00
|
|
|
# from fotokiste import model
|
|
|
|
# import logging
|
|
|
|
# log = logging.getLogger("fotokiste.controllers")
|
|
|
|
|
2008-06-09 04:29:18 +02:00
|
|
|
|
|
|
|
class DummyPicture(object):
|
|
|
|
title = "leer"
|
|
|
|
url = "nichts"
|
|
|
|
|
|
|
|
import os
|
2008-06-15 19:37:08 +02:00
|
|
|
import urllib
|
|
|
|
import tempfile
|
|
|
|
import cherrypy
|
|
|
|
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"
|
|
|
|
ALLOWED_MAILADDRESS_CHARACTERS = "\w._%@-"
|
|
|
|
ALLOWED_MAILTEXT_CHARACTERS = "\w@_\-\.\s\n\#\(\)\[\]\{\}\|\>\<\,\+/\'\"\?\!\:=%\$^&\*"
|
|
|
|
MAIL_SIGNATURE_FILE = "mail_signature.txt"
|
|
|
|
MAIL_FROM_ADDRESS = "fotokiste@glasmensch.org"
|
|
|
|
MAIL_SUBJECT = "Ein Foto von der Fusion!"
|
|
|
|
SMTP_HOST = "localhost"
|
|
|
|
SMTP_PORT = "25"
|
|
|
|
MAIL_MAX_LENGTH = 5000
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_DICT = { "video_url": VIDEO_URL }
|
|
|
|
|
|
|
|
def debug(message):
|
|
|
|
print "%s: %s" % (datetime.datetime.now().isoformat(), message)
|
|
|
|
|
|
|
|
def merged_dicts(dict_a, dict_b):
|
|
|
|
return dict(dict_a.items() + dict_b.items())
|
|
|
|
|
|
|
|
def filter_mailaddress(address):
|
|
|
|
return re.sub("[^%s]" % ALLOWED_MAILADDRESS_CHARACTERS, "", address)
|
|
|
|
|
|
|
|
def filter_mailtext(text):
|
|
|
|
filtered = re.sub("[^%s]" % ALLOWED_MAILTEXT_CHARACTERS, "", text)
|
|
|
|
if len(filtered) > MAIL_MAX_LENGTH:
|
|
|
|
return filtered[:MAIL_MAX_LENGTH-1]
|
|
|
|
else:
|
|
|
|
return filtered
|
|
|
|
|
|
|
|
def check_mailaddress(address):
|
|
|
|
if re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,6}$", address):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def send_mail(address, text):
|
|
|
|
import smtplib
|
|
|
|
import MimeWriter
|
|
|
|
import StringIO
|
|
|
|
import base64
|
|
|
|
# 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))
|
|
|
|
try:
|
|
|
|
picture = StringIO.StringIO(file(IMAGE_STORE).read())
|
|
|
|
except IOError, msg:
|
|
|
|
debug("failed to open the image file (%s): %s" % \
|
|
|
|
(IMAGE_STORE, msg))
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
|
|
|
con = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
|
|
|
|
con.sendmail(address, MAIL_FROM_ADDRESS, message.getvalue())
|
2008-06-09 04:29:18 +02:00
|
|
|
|
|
|
|
|
2008-06-09 00:58:23 +02:00
|
|
|
class Root(controllers.RootController):
|
2008-06-09 04:29:18 +02:00
|
|
|
|
|
|
|
@expose(template="fotokiste.templates.start")
|
|
|
|
def index(self, **kargs):
|
|
|
|
|
|
|
|
# Bilder zufaellig aus der Datenbank auswaehlen
|
|
|
|
# eine andere Funktion muss die Bilder ausliefern
|
|
|
|
gallery = []
|
|
|
|
for i in range(22):
|
|
|
|
obj = DummyPicture()
|
|
|
|
obj.title = "test%d" % i
|
|
|
|
obj.url = "URL: %d" % i
|
|
|
|
gallery.append(obj)
|
|
|
|
|
|
|
|
# alte Bild-Datei loeschen
|
|
|
|
if os.path.isfile(IMAGE_STORE):
|
|
|
|
os.unlink(IMAGE_STORE)
|
|
|
|
|
2008-06-15 19:37:08 +02:00
|
|
|
return merged_dicts({ "gallery": gallery }, DEFAULT_DICT)
|
2008-06-09 04:29:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
@expose(template="fotokiste.templates.ausloeser")
|
|
|
|
def ausloeser(self, **kargs):
|
|
|
|
flash("Das Bild wird in 5 Sekunden aufgenommen!")
|
2008-06-15 19:37:08 +02:00
|
|
|
return merged_dicts({}, DEFAULT_DICT)
|
2008-06-09 04:29:18 +02:00
|
|
|
|
|
|
|
@expose(template="fotokiste.templates.mailtext")
|
2008-06-15 19:37:08 +02:00
|
|
|
def mailtext(self, mailaddress="", mailtext="", already_stored="no", **kargs):
|
|
|
|
# store the picture if necessary
|
|
|
|
if already_stored != "yes":
|
|
|
|
urllib.urlretrieve(SNAPSHOT_URL, IMAGE_STORE)
|
|
|
|
# filter input
|
|
|
|
mailaddress = filter_mailaddress(mailaddress)
|
|
|
|
mailtext = filter_mailtext(mailtext)
|
|
|
|
|
|
|
|
#if not check_mailaddress(mailaddress):
|
|
|
|
# turbogears.flash(
|
|
|
|
|
|
|
|
return merged_dicts({
|
|
|
|
"mailaddress": mailaddress,
|
2008-06-09 04:29:18 +02:00
|
|
|
"mailtext": mailtext,
|
|
|
|
"already_stored": already_stored,
|
2008-06-15 19:37:08 +02:00
|
|
|
}, DEFAULT_DICT)
|
2008-06-09 04:29:18 +02:00
|
|
|
|
|
|
|
@expose(template="fotokiste.templates.senden")
|
2008-06-15 19:37:08 +02:00
|
|
|
def senden(self, mailaddress="", mailtext="", senden=None):
|
|
|
|
# filter input
|
|
|
|
mailaddress = filter_mailaddress(mailaddress)
|
|
|
|
mailtext = filter_mailtext(mailtext)
|
|
|
|
|
|
|
|
return_dict = merged_dicts({
|
|
|
|
"mailaddress": mailaddress,
|
|
|
|
"mailtext": mailtext,
|
|
|
|
}, DEFAULT_DICT)
|
|
|
|
# check for a valid mail address and redirect if necessary
|
|
|
|
if not check_mailaddress(mailaddress):
|
|
|
|
if not mailaddress:
|
|
|
|
flash("Gib bitte eine Ziel-Mailadresse an!")
|
|
|
|
else:
|
|
|
|
flash("Die Mailadresse scheint nicht ungueltig zu sein.")
|
|
|
|
redirect("mailtext", return_dict)
|
|
|
|
else:
|
|
|
|
# Mail versenden
|
|
|
|
send_mail(mailaddress, mailtext)
|
|
|
|
return return_dict
|
|
|
|
|
|
|
|
@expose()
|
|
|
|
def get_current_shot(self):
|
|
|
|
if os.path.exists(IMAGE_STORE):
|
|
|
|
return cherrypy.lib.cptools.serveFile(IMAGE_STORE,
|
|
|
|
"image/jpeg")
|
|
|
|
else:
|
|
|
|
return ""
|
2008-06-09 04:29:18 +02:00
|
|
|
|