This repository has been archived by the owner on Dec 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minitwit.py
347 lines (309 loc) · 11.8 KB
/
minitwit.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# -*- coding: utf-8 -*-
from datetime import datetime
from hashlib import md5
import pytz
from flask import Flask, request, session, url_for, redirect, \
render_template, abort, g, flash
from werkzeug.security import check_password_hash, generate_password_hash
from flask.ext.pymongo import PyMongo
from bson.objectid import ObjectId
from random import shuffle
import portraitdomain
import logging
# create our little application :)
app = Flask(__name__)
# Load default config and override config from an environment variable
app.config.update(dict(
DEBUG=True,
SECRET_KEY='development key'))
app.config.from_envvar('MINITWIT_SETTINGS', silent=True)
app.config.from_object('config')
# setup mongodb
mongo = PyMongo(app)
# divert logging
print "Logging to %s" % app.config['LOG_FILE']
logging.basicConfig(filename=app.config['LOG_FILE'], level=logging.INFO, format='%(asctime)s %(message)s')
def get_user_id(username):
"""Convenience method to look up the id for a username."""
rv = mongo.db.user.find_one({'username': username}, {'_id': 1})
return rv['_id'] if rv else None
def format_datetime(timestamp):
"""Format a timestamp for display."""
return timestamp.replace(tzinfo=pytz.utc).strftime('%Y-%m-%d @ %H:%M')
def gravatar_url(email, size=80):
"""Return the gravatar image for the given email address."""
return 'http://www.gravatar.com/avatar/%s?d=identicon&s=%d' % \
(md5(email.strip().lower().encode('utf-8')).hexdigest(), size)
@app.before_request
def before_request():
g.user = None
if 'user_id' in session:
g.user = mongo.db.user.find_one({'_id': ObjectId(session['user_id'])})
@app.route('/')
def timeline():
"""Shows a users timeline or if no user is logged in it will
redirect to the public timeline. This timeline shows the user's
messages as well as all the messages of followed users.
"""
if not g.user:
return redirect(url_for('public_timeline'))
return redirect(url_for('user_timeline', username=g.user['username']))
@app.route('/public')
def public_timeline():
"""Displays the latest messages of all users."""
past = request.args.get('past')
if past is None:
messages = mongo.db.message.find().sort('pub_date', -1).limit(10)
else:
past = datetime.strptime(past.split('.')[0], "%Y-%m-%d %H:%M:%S")
messages = mongo.db.message.find({ 'pub_date': { '$lt': past } }).sort('pub_date', -1).limit(10)
return render_template('timeline.html', messages=messages)
@app.route('/search')
def search_result():
"""Searches user and post data"""
query = request.args.get('q')
if len(query) < 3:
flash('Search query too short to be of much use ...')
return redirect(url_for('public_timeline'))
regex = ".*%s.*" % query
messages = mongo.db.message.find({
"text": { "$regex": query, "$options":"i" }
}).sort('pub_date', -1)
portraits = mongo.db.portrait.find({
"name": { "$regex": query, "$options":"i" }
}).sort('name', 1)
return render_template(
'timeline.html',
messages=messages,
portraits=portraits,
query=query
)
@app.route('/go/<shortname>')
def go_link(shortname):
""" Expands shortlink to portrait """
p = mongo.db.portrait.find_one({
'shortname': shortname })
if p is None:
logging.error("'%s' shortlink not resolved" % shortname)
abort(404)
pid = p['_id']
logging.info("'%s' was shortlinked from '%s'" % (p['name'], shortname))
return redirect(url_for('face_timeline', pid=pid))
@app.route('/<username>')
def user_timeline(username):
"""Display's a users tweets."""
profile_user = mongo.db.user.find_one({
'username': username })
if profile_user is None:
abort(404)
# Work out followers
followed = False
if g.user:
followed = mongo.db.follower.find_one(
{'who_id': ObjectId(session['user_id']),
'whom_id': {'$in': [ObjectId(profile_user['_id'])]}}) is not None
# Get messages
messages = mongo.db.message.find(
{'author_id': ObjectId(profile_user['_id'])}).sort('pub_date', -1)
# Get portrait if selected
portrait = None
if 'portrait_id' in profile_user:
portrait = mongo.db.portrait.find_one(
{'_id': ObjectId(profile_user['portrait_id'])})
logging.info("'%s' was profiled as user (%s)" % (portrait['name'], username))
return render_template('timeline.html', messages=messages,
followed=followed, profile_user=profile_user,
portrait=portrait)
@app.route('/face/<pid>')
def face_timeline(pid):
"""Displays tweets by portrait."""
profile_user = mongo.db.user.find_one({'portrait_id': pid})
# Get messages
messages = mongo.db.message.find(
{'portrait_id': pid}).sort('pub_date', -1)
# Get portrait
portrait = mongo.db.portrait.find_one(
{'_id': ObjectId(pid)})
logging.info("'%s' was profiled" % portrait['name'])
return render_template('timeline.html', messages=messages,
profile_user=profile_user,
portrait=portrait)
@app.route('/<username>/follow')
def follow_user(username):
"""Adds the current user as follower of the given user."""
if not g.user:
abort(401)
whom_id = get_user_id(username)
if whom_id is None:
abort(404)
mongo.db.follower.update(
{'who_id': ObjectId(session['user_id'])},
{'$push': {'whom_id': whom_id}}, upsert=True)
flash('You are now following "%s"' % username)
return redirect(url_for('user_timeline', username=username))
@app.route('/<username>/unfollow')
def unfollow_user(username):
"""Removes the current user as follower of the given user."""
if not g.user:
abort(401)
whom_id = get_user_id(username)
if whom_id is None:
abort(404)
mongo.db.follower.update(
{'who_id': ObjectId(session['user_id'])},
{'$pull': {'whom_id': whom_id}})
flash('You are no longer following "%s"' % username)
return redirect(url_for('user_timeline', username=username))
@app.route('/add_message', methods=['POST'])
def add_message():
"""Registers a new message for the user."""
if 'user_id' not in session:
abort(401)
if request.form['text']:
user = mongo.db.user.find_one(
{'_id': ObjectId(session['user_id'])})
pid = None
pname = ""
pfile = ""
if 'portrait_file' in user:
pid = user['portrait_id']
pname = user['portrait_name']
pfile = user['portrait_file']
mongo.db.message.insert(
{'author_id': ObjectId(session['user_id']),
'email': user['email'],
'username': user['username'],
'text': request.form['text'],
'pub_date': datetime.utcnow(),
'portrait_id': pid,
'portrait_file': pfile,
'portrait_name': pname
})
flash('Your message was recorded')
logging.info("(%s) posted as '%s'" % (user['username'], user['portrait_name']))
return redirect(url_for('timeline'))
@app.route('/login', methods=['GET', 'POST'])
def login():
"""Logs the user in."""
if g.user:
return redirect(url_for('timeline'))
error = None
if request.method == 'POST':
user = mongo.db.user.find_one({'username': request.form['username']})
if user is None:
error = 'Invalid username'
logging.error('Attempted log in with invalid username')
elif not check_password_hash(user['pw_hash'], request.form['password']):
error = 'Invalid password'
logging.error('Attempted log in with invalid password')
else:
flash('You were logged in')
session['user_id'] = str(user['_id'])
logging.info("(%s) has logged in" % user['username'])
return redirect(url_for('timeline'))
return render_template('login.html', error=error)
@app.route('/register', methods=['GET', 'POST'])
def register():
"""Registers the user."""
if g.user:
return redirect(url_for('timeline'))
error = None
if request.method == 'POST':
if not request.form['username']:
error = 'You have to enter a username'
elif not request.form['email'] or '@' not in request.form['email']:
error = 'You have to enter a valid email address'
elif not request.form['password']:
error = 'You have to enter a password'
elif request.form['password'] != request.form['password2']:
error = 'The two passwords do not match'
elif get_user_id(request.form['username']) is not None:
error = 'The username is already taken'
else:
mongo.db.user.insert(
{'username': request.form['username'],
'email': request.form['email'],
'pw_hash': generate_password_hash(request.form['password'])})
flash('You were successfully registered and can login now')
return redirect(url_for('login'))
return render_template('register.html', error=error)
@app.route('/logout')
def logout():
"""Logs the user out."""
flash('You were logged out')
session.pop('user_id', None)
return redirect(url_for('public_timeline'))
@app.route('/portraits/update')
def portraits_update():
""" Reloads portraits database """
count = portraitdomain.update(mongo.db.portrait)
return "Count: %d" % count
@app.route('/portraits/get/<userid>')
def portraits_get(userid):
""" Fetches filename of portrait for user """
user = mongo.db.user.find_one(
{'_id': ObjectId(userid)})
if user is None:
abort(404)
if 'portrait_file' in user:
pfile = user['portrait_file']
return redirect('/static/portraits-images/%s' % pfile)
else:
pfile = "default.png"
return redirect('/static/res/%s' % pfile)
@app.route('/portraits/select/<pid>')
def portraits_select(pid):
""" Assign portrait to user """
if 'user_id' not in session:
flash('Please login or register, then go back to adopt the portrait.')
return redirect('/login')
portrait = mongo.db.portrait.find_one(
{'_id': ObjectId(pid)})
if portrait is None:
abort(404)
# Unselect previous portrait
user = mongo.db.user.find_one(
{'_id': ObjectId(session['user_id'])})
if 'portrait_id' in user:
mongo.db.portrait.update(
{ '_id': ObjectId(user['portrait_id']) },
{ '$set': {
'user': None
}})
# Update portrait
mongo.db.portrait.update(
{ '_id': ObjectId(pid) },
{ '$set': {
'user': session['user_id']
}})
# Update user
mongo.db.user.update(
{ '_id': ObjectId(session['user_id']) },
{ '$set': {
'portrait_id': pid,
'portrait_file': portrait['imagefile'],
'portrait_name': portrait['name']
}})
logging.info("'%s' was adopted" % portrait['name'])
flash('Your identity has been changed: you are now %s' % portrait['name'])
return redirect(url_for('user_timeline', username=user['username']))
@app.route('/portraits')
def portraits():
""" Browse the portraits """
if 'user_id' not in session:
abort(401)
# Todo: check g.user for selected portrait
portraits = list(mongo.db.portrait.find(
{'user': None, 'imagefile': { '$ne': '' }}
).limit(300))
shuffle(portraits)
return render_template('portraits.html', portraits=portraits[:21])
@app.route('/about')
def about_page():
""" Some information aboot this project """
return render_template('about.html')
# add some filters to jinja
app.jinja_env.filters['datetimeformat'] = format_datetime
app.jinja_env.filters['gravatar'] = gravatar_url
if __name__ == '__main__':
app.run(port=1852)