/
app.py
201 lines (155 loc) · 5.66 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from flask import Flask
from flask import request
import os
import datetime
import json
import re
from smtplib import SMTP
from cStringIO import StringIO
from email.mime.multipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.header import Header
from email import Charset
from email.generator import Generator
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
class Member(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
email = db.Column(db.String())
def __init__(self, name, email):
self.name = name
self.email = email
def __repr__(self):
return '<name {}>'.format(self.name)
tags = db.Table('tags',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column('message_id', db.Integer, db.ForeignKey('message.id'))
)
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
def __init__(self, name):
self.name = name
def __repr__(self):
return '<name {}>'.format(self.name)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String())
message = db.Column(db.String())
date_did = db.Column(db.String())
tags = db.relationship('Tag', secondary=tags, backref=db.backref('messages', lazy='dynamic'))
def __init__(self, email, message, date_did):
self.message = message
self.email = email
self.date_did = date_did
def __repr__(self):
return '<id {}>'.format(self.id)
def tag(self,tag):
# It should be a tag object, but if it isn't, we'll sort it out
if type(tag) is unicode:
tag = Tag(tag)
self.tags.append(tag)
return self
def untag(self,tag):
# It should be a tag object, but if it isn't, we'll sort it out
if type(tag) is unicode:
tag = db.session.query(Tag).filter_by(name=tag)
self.tags.remove(tag)
return self
def send_email(to_email,subject,message):
# send the message
smtp = SMTP()
smtp.connect('smtp.mandrillapp.com', 587)
smtp.login(os.environ.get('MANDRILL_USERNAME'), os.environ.get('MANDRILL_APIKEY'))
from_addr = "Tindfell <whatididtoday@tindfell.com>"
to_addr = [to_email]
date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )
Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')
msg = MIMEMultipart("alternative")
msg['From'] = Header(from_addr.encode('utf-8'), 'UTF-8').encode()
msg['To'] = Header(', '.join(to_addr).encode('utf-8'), 'UTF-8').encode()
msg['Subject'] = Header(subject.encode('utf-8'), 'UTF-8').encode()
msg.attach(MIMEText(message.encode('utf-8'),'plain','utf-8'))
#msg.attach(MIMEText(message.encode('utf-8'),'html','utf-8'))
io = StringIO()
g = Generator(io, False) # second argument means "should I mangle From?"
g.flatten(msg)
# For Degubbing
#print io.getvalue()
# send the message!
smtp.sendmail(from_addr, to_addr, io.getvalue())
smtp.quit()
return
@app.route("/webhook", methods=['GET','POST'])
def webhook():
# Get the data from mandrill and save it into the database
if 'mandrill_events' in request.form:
mandrill_events = json.loads(request.form['mandrill_events'])
for inbound in mandrill_events:
if inbound['event'] == u"inbound":
subject = inbound['msg']['subject']
from_email = inbound['msg']['from_email']
message = inbound['msg']['text']
#remove any signatures from the email
signature_pos = message.find('-- ')
message = message[:signature_pos]
# Try to parse this a bit better
date_did = subject
# Save the information
mm = Message(from_email, message, date_did)
# Save everything
# @TODO: Parse the message for tags
tags = re.findall(r'#\w+', message)
for i in tags:
mm.tag(i[1:])
db.session.add(mm)
db.session.commit()
return "Success"
return "Error"
@app.route("/messages", methods=['GET'])
def messages():
# Here we look in the database, loop through messages and display them
html = ''
messages = db.session.query(Message).all()
for i in messages:
html += '<h2>%s</h2><p>%s</p>'%(i.email,i.message)
if len(i.tags) > 0:
html += '<ul>'
for j in i.tags:
html += '<li>%s</li>'%(j.name)
html += '</ul>'
return html
@app.route("/ask", methods=['GET'])
def ask():
# Here we look in the database, loop through users and send them an email asking what they have done today
if 'api_key' in request.args:
if request.args['api_key'] == os.environ.get('API_KEY'):
# Now loop through the DB and send the standard email
subject = "What did you do today %s"%(datetime.datetime.today().strftime("%Y-%m-%d"))
members = db.session.query(Member).all()
for i in members:
# Send an email to each person, extra X-Headers if needed
send_email("%s <%s>"%(i.name,i.email),subject,"")
return "Success"
# If you are getting here, something failed
return "Error"
@app.route("/")
def hello():
return "Welcome to Flask!"
if __name__ == "__main__":
# Set up logging to stdout, which ends up in Heroku logs
#stream_handler = logging.StreamHandler()
#stream_handler.setLevel(logging.WARNING)
#app.logger.addHandler(stream_handler)
#manager.run()
app.debug = True
app.run(host='0.0.0.0', port=flask_config.port)
#app.run(host='0.0.0.0', port=5000)