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-08-23 15:11:19 +02:00
# from fotokiosk import model
2008-06-09 00:58:23 +02:00
# import logging
2008-08-23 15:11:19 +02:00
# log = logging.getLogger("fotokiosk.controllers")
2008-06-09 00:58:23 +02:00
2008-06-09 04:29:18 +02:00
2008-06-26 16:09:04 +02:00
class SamplePicture ( object ) :
2008-06-09 04:29:18 +02:00
title = " leer "
url = " nichts "
import os
2008-06-15 19:37:08 +02:00
import urllib
import tempfile
import cherrypy
import re
import datetime
2008-06-26 16:09:04 +02:00
import random
2008-08-23 15:11:19 +02:00
IMAGE_GALLERY_PATH = os . path . join ( os . getcwd ( ) , ' fotokiosk ' , ' static ' , ' gallery ' )
2008-06-26 16:09:04 +02:00
IMAGE_SUFFIX = " .jpg "
2008-08-23 15:11:19 +02:00
IMAGE_STORE = os . path . join ( tempfile . gettempdir ( ) , " fotokiosk " + IMAGE_SUFFIX )
2008-06-26 16:09:04 +02:00
GALLERY_NUM = 12
2008-08-23 15:11:19 +02:00
VIDEO_URL = " http://fotokiosk:8081/?action=stream "
SNAPSHOT_URL = " http://fotokiosk:8081/?action=snapshot "
2008-06-15 19:37:08 +02:00
ALLOWED_MAILADDRESS_CHARACTERS = " \ w._ % @- "
ALLOWED_MAILTEXT_CHARACTERS = " \ w@_ \ - \ . \ s \n \ # \ ( \ ) \ [ \ ] \ { \ } \ | \ > \ < \ , \ +/ \' \" \ ? \ ! \ := % \ $^& \ * "
2008-06-16 01:47:44 +02:00
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!
2008-06-26 16:09:04 +02:00
MAIL_SIGNATURE_FILE = os . path . join ( os . getcwd ( ) , " mail_signature.txt " )
2008-08-23 15:11:19 +02:00
MAIL_FROM_ADDRESS = ' " Frieda-Fotokiosk " <fotokiosk@glasmensch.org> '
2008-07-01 22:50:14 +02:00
MAIL_SUBJECT = " Ein überwachungskritisches Foto aus der Frieda! "
2008-07-01 22:55:45 +02:00
MAIL_ATTACHMENT_FILENAME = " frieda " + IMAGE_SUFFIX
2008-06-26 16:09:04 +02:00
SMTP_HOST = " localhost "
2008-06-15 19:37:08 +02:00
SMTP_PORT = " 25 "
MAIL_MAX_LENGTH = 5000
2008-06-16 01:47:44 +02:00
ERRORS_MAX_LENGTH = 25
2008-06-15 19:37:08 +02:00
2008-06-16 01:47:44 +02:00
errors = [ ]
2008-06-15 19:37:08 +02:00
DEFAULT_DICT = { " video_url " : VIDEO_URL }
def debug ( message ) :
2008-06-16 01:47:44 +02:00
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
2008-06-15 19:37:08 +02:00
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 ) :
2008-06-16 01:47:44 +02:00
if re . match ( MAIL_ADDRESS_REGEX , address ) :
2008-06-15 19:37:08 +02:00
return True
else :
return False
2008-06-26 16:09:04 +02:00
def store_picture ( ) :
if not os . path . isdir ( IMAGE_GALLERY_PATH ) :
os . mkdir ( IMAGE_GALLERY_PATH )
import shutil
timestamp = datetime . datetime . now ( ) . strftime ( " % Y % m %d _ % H % M % S " )
filename = os . path . join ( IMAGE_GALLERY_PATH , " %s %s " % ( timestamp , IMAGE_SUFFIX ) )
shutil . copy ( IMAGE_STORE , filename )
def get_random_pictures ( ) :
default = [ " " ] * GALLERY_NUM
try :
result = [ ]
all_files = [ fname for fname in os . listdir ( IMAGE_GALLERY_PATH )
if os . path . isfile ( os . path . join ( IMAGE_GALLERY_PATH , fname ) )
and fname . endswith ( IMAGE_SUFFIX ) ]
if not all_files :
return default
all_files . sort ( )
print all_files
# the last five pictures
result . extend ( all_files [ - 5 : ] )
while len ( result ) < GALLERY_NUM :
result . append ( all_files [ random . randint ( 0 , len ( all_files ) - 1 ) ] )
return result
except OSError :
return default
2008-06-15 19:37:08 +02:00
def send_mail ( address , text ) :
import smtplib
import StringIO
2008-06-16 01:47:44 +02:00
import email . Generator
import email . MIMEMultipart
import email . MIMEText
import email . MIMEImage
import rfc822
2008-06-15 19:37:08 +02:00
# 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 ) )
2008-06-16 01:47:44 +02:00
return False
2008-06-15 19:37:08 +02:00
try :
2008-06-16 01:47:44 +02:00
picture = file ( IMAGE_STORE ) . read ( )
2008-06-15 19:37:08 +02:00
except IOError , msg :
debug ( " failed to open the image file ( %s ): %s " % \
( IMAGE_STORE , msg ) )
2008-06-16 01:47:44 +02:00
return False
2008-06-15 19:37:08 +02:00
# prepare the message
2008-06-16 01:47:44 +02:00
#mail_flat = StringIO.StringIO()
#mail_gen = email.Generator.Generator(mail_flat)
#mail_gen.flatten(body)
mail = email . MIMEMultipart . MIMEMultipart ( )
2008-06-20 04:28:36 +02:00
mail_text = email . MIMEText . MIMEText ( ( unicode ( text ) +
unicode ( signature , ' latin-1 ' ) ) . encode ( ' utf-8 ' ) , _charset = ' utf-8 ' )
2008-06-16 01:47:44 +02:00
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 )
# send the mail
try :
con = smtplib . SMTP ( SMTP_HOST , SMTP_PORT )
2008-07-01 22:50:14 +02:00
con . sendmail ( MAIL_FROM_ADDRESS , address , writer . getvalue ( ) )
2008-06-16 01:47:44 +02:00
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
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
2008-08-23 15:11:19 +02:00
@expose ( template = " fotokiosk.templates.start " )
2008-06-09 04:29:18 +02:00
def index ( self , * * kargs ) :
2008-06-16 01:47:44 +02:00
# TODO: this should generate a selection of random pictures
2008-06-26 16:09:04 +02:00
pic_names = get_random_pictures ( )
2008-06-09 04:29:18 +02:00
gallery = [ ]
2008-06-26 16:09:04 +02:00
for i in range ( len ( pic_names ) ) :
obj = SamplePicture ( )
obj . title = " pic %d " % ( i + 1 )
obj . url = " %s " % pic_names [ i ]
2008-06-09 04:29:18 +02:00
gallery . append ( obj )
2008-06-16 01:47:44 +02:00
# remove old picture
2008-06-09 04:29:18 +02:00
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
2008-08-23 15:11:19 +02:00
@expose ( template = " fotokiosk.templates.ausloeser " )
2008-06-09 04:29:18 +02:00
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
2008-06-16 01:47:44 +02:00
2008-08-23 15:11:19 +02:00
@expose ( template = " fotokiosk.templates.mailtext " )
2008-06-26 16:09:04 +02:00
def mailtext ( self , mailaddress = " " , mailtext = " " , already_captured = " no " , already_stored = " no " , * * kargs ) :
2008-06-15 19:37:08 +02:00
# store the picture if necessary
2008-06-26 16:09:04 +02:00
if already_captured != " yes " :
2008-07-01 22:50:14 +02:00
try :
urllib . urlretrieve ( SNAPSHOT_URL , IMAGE_STORE )
except :
#os.system("reboot")
flash ( " Achtung: dein Foto enthaelt moeglicherweise menschenverachtende Inhalte. Bitte wende dich fuer einen umfassenden Persoenlichkeitstest an der Bar. Danke fuer deine Kooperation! " )
redirect ( " index " , DEFAULT_DICT )
2008-06-26 16:09:04 +02:00
already_captured = " no "
if already_stored != " yes " :
already_stored = " no "
2008-06-15 19:37:08 +02:00
# 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
2008-06-16 01:47:44 +02:00
2008-08-23 15:11:19 +02:00
@expose ( template = " fotokiosk.templates.senden " )
2008-06-26 16:09:04 +02:00
def senden ( self , mailaddress = " " , mailtext = " " , already_stored = " no " , senden = None ) :
2008-06-15 19:37:08 +02:00
# filter input
mailaddress = filter_mailaddress ( mailaddress )
mailtext = filter_mailtext ( mailtext )
2008-06-26 16:09:04 +02:00
if already_stored != " yes " :
already_stored = " no "
2008-06-15 19:37:08 +02:00
return_dict = merged_dicts ( {
" mailaddress " : mailaddress ,
" mailtext " : mailtext ,
2008-06-26 16:09:04 +02:00
" already_stored " : already_stored ,
2008-06-15 19:37:08 +02:00
} , 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 :
2008-06-16 01:47:44 +02:00
# send the mail
2008-06-26 16:09:04 +02:00
if already_stored != " yes " :
store_picture ( )
2008-06-16 01:47:44 +02:00
if send_mail ( mailaddress , mailtext ) :
return return_dict
else :
2008-07-01 22:50:14 +02:00
flash ( " Fehler beim Senden der Mail! Ist die Adresse korrekt? " )
redirect ( " mailtext " , return_dict )
2008-06-15 19:37:08 +02:00
@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