#!/usr/bin/env python # SecretSnowflakeOrganizer.py # Web app that automatically coordinates a Secret Snowflake Gift Exchange COORDINATOR_PASSWORD = 'COORDINATOR_PASSWORD' DB_HOST = 'DB_HOST' DB_USER = 'DB_USER' DB_PASSWORD = 'DB_PASSWORD' DB_NAME = 'DB_NAME' GMAIL_USER = 'EMAIL' GMAIL_PASSWORD = 'EMAIL_PASSWORD' ORGANIZATION = 'ORGANIZATION' import MySQLdb class DB: def connect(self): self.db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASSWORD, db=DB_NAME) return self.db.cursor() def commit(self): self.db.commit() def close(self): self.db.close() db = DB() def is_open(): sql = db.connect() sql.execute("""SELECT open FROM status WHERE 1 LIMIT 1""") for status in sql.fetchall(): db.close() return bool(int(status[0])) # email stuff import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText def mail(to, subject, text): msg = MIMEMultipart() msg['From'] = GMAIL_USER msg['To'] = to msg['Subject'] = subject msg.attach(MIMEText(text)) mailServer = smtplib.SMTP("smtp.gmail.com", 587) mailServer.ehlo() mailServer.starttls() mailServer.ehlo() mailServer.login(GMAIL_USER, GMAIL_PASSWORD) mailServer.sendmail(GMAIL_USER, to, msg.as_string()) mailServer.close() import web urls = ( '/', 'index', '/signup', 'signup', '/assign', 'assign', '/open', 'open' ) class index: def signup(self,open): if open: return """
Name:
Email:
Additional Info (interests/favorites/allergies/etc.):


Coordinator Only
Password:
""" else: return """

Sign-up is currently closed.

Coordinator Only
Password:
""" def GET(self): web.header('Content-Type','text/html; charset=utf-8', unique=True) try: message = self.signup(is_open()) except: message = '
Currently experiencing problems, but hoping to be back up shortly. Sorry!
' return """ {0} | Secret Snowflake | Sign-Up

{0}'s Secret Snowflake Sign-Up

""".format(ORGANIZATION) \ + message + \ """""" class signup: def POST(self): import re data = web.input() message = '' if data.name == '': message += 'Please enter your name.
' if not re.match(r"[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Za-z]{2}|com|org|net|edu|info)\b", data['email']): message += 'Please enter your email.
' if not hasattr(data, 'promise'): message += 'Please make the promise.
' if message == '': message += 'You have signed up! Please look for a email confirming your participation in the gift exchange.' sql = db.connect() sql.execute("""INSERT INTO snowflakes VALUES (%s, %s, %s)""", (data.name, data.email, data.info)) db.commit() db.close() mail(data.email, '{} Secret Snowflake'.format(ORGANIZATION), 'Hi {},\n\nJust an email confirmation that you have succesfully signed up for {}\'s Secret Snowflake gift exchange! Expect\n\nHappy Holidays!\nYour Secret Snowflake Coordinator'.format(data.name.split()[0], ORGANIZATION)) else: message += 'Back to sign-up.' web.header('Content-Type','text/html; charset=utf-8', unique=True) return """ {0} | Secret Snowflake | Sign-Up

{0}'s Secret Snowflake Sign-Up

""".format(ORGANIZATION) \ + message + \ """
""" class Snowflake: def __init__(self,name,email,info): self.name = name self.email = email self.info = info class assign: def POST(self): data = web.input() if not is_open(): message = 'You can only assign Secret Snowflakes once!' elif data.password == PASSWORD: message = 'Secret Snowflake assignments have been emailed successfully!' sql = db.connect() sql.execute("""UPDATE status SET open=0 WHERE 1 LIMIT 1""") sql.execute("""SELECT name, email, info FROM snowflakes WHERE 1""") db.commit() from collections import deque from random import shuffle snowflakes = deque([]) for sf in sql.fetchall(): snowflakes.append(Snowflake(sf[0], sf[1], sf[2])) db.close() shuffle(snowflakes) shuffle(snowflakes) shuffle(snowflakes) snowflakes2 = deque(snowflakes) snowflakes2.rotate(1) for i in xrange(len(snowflakes)): a = snowflakes[i] b = snowflakes2[i] mail(a.email, 'Your {} Snowflake'.format(ORGANIZATION), 'Hi {},\n\nThanks for participating in {}\'s Secret Snowflake gift exchange! Your Secret Snowflake is {}. They would like you to know the following about themselves:\n{}\n\nHappy Holidays!\nYour Secret Snowflake Coordinator'.format(a.name.split()[0], ORGANIZATION, b.name, b.info if b.info else '[Nothing]')) else: message = 'Incorrect password.
Back.' web.header('Content-Type','text/html; charset=utf-8', unique=True) return """ {0} | Secret Snowflake | Sign-Up

{0}'s Secret Snowflake Sign-Up

""".format(ORGANIZATION) + message + """
""" class open: def POST(self): data = web.input() if data.password == PASSWORD: message = 'Sign-up is now open!
Go to sign-up.' sql = db.connect() sql.execute("""UPDATE status SET open=1 WHERE 1 LIMIT 1""") sql.execute("""DELETE FROM snowflakes WHERE 1""") db.commit() db.close() else: message = 'Incorrect password.
Back.' web.header('Content-Type','text/html; charset=utf-8', unique=True) return """ {0} | Secret Snowflake | Sign-Up

{0}'s Secret Snowflake Sign-Up

""".format(ORGANIZATION) + message + """
""" app = web.application(urls, globals()) application = app.wsgifunc()