Skip to content

Commit 36efa3b

Browse files
committed
Fix twitter example app.
1 parent 3a6243d commit 36efa3b

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

examples/twitter/app.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import operator
3+
from functools import reduce
34

45
from flask import abort
56
from flask import flash
@@ -45,12 +46,14 @@ class User(BaseModel):
4546
def get_followers(self):
4647
# Because all users are added to the `followers` sorted-set with the
4748
# same score, when retrieved they will be sorted by key (username).
48-
return [User.load(username) for username in self.followers]
49+
return [User.load(username.decode('utf8'))
50+
for username, _ in self.followers]
4951

5052
def get_following(self):
5153
# Because all users are added to the `following` sorted-set with the
5254
# same score, when retrieved they will be sorted by key (username).
53-
return [User.load(username) for username in self.following]
55+
return [User.load(username.decode('utf8'))
56+
for username, _ in self.following]
5457

5558
def is_following(self, user):
5659
# We can use Pythonic operators when working with Walrus containers.
@@ -156,7 +159,7 @@ def join():
156159
except KeyError:
157160
user = User.create(
158161
username=username,
159-
password=md5(request.form['password']).hexdigest(),
162+
password=md5(request.form['password'].encode('utf8')).hexdigest(),
160163
email=request.form['email'])
161164
auth_user(user)
162165
return redirect(url_for('homepage'))
@@ -171,7 +174,7 @@ def login():
171174
try:
172175
user = User.get(
173176
(User.username == request.form['username']) &
174-
(User.password == md5(request.form['password']).hexdigest()))
177+
(User.password == md5(request.form['password'].encode('utf8')).hexdigest()))
175178
except ValueError:
176179
flash('The password entered is incorrect')
177180
else:
@@ -191,14 +194,14 @@ def logout():
191194
def following():
192195
# Get the list of user objects the current user is following.
193196
user = get_current_user()
194-
return render_template('user_following', user_list=user.get_following())
197+
return render_template('user_following.html', user_list=user.get_following())
195198

196199
@app.route('/followers/')
197200
@login_required
198201
def followers():
199202
# Get the list of user objects the current user is followed by.
200203
user = get_current_user()
201-
return render_template('user_following', user_list=user.get_followers())
204+
return render_template('user_followers.html', user_list=user.get_followers())
202205

203206
@app.route('/users/')
204207
def user_list():
@@ -226,8 +229,8 @@ def user_detail(username):
226229
def user_follow(username):
227230
current_user = get_current_user()
228231
user = get_object_or_404(User, username)
229-
current_user.following.add(user.username, 0)
230-
user.followers.add(current_user.username, 0)
232+
current_user.following.add({user.username: 0})
233+
user.followers.add({current_user.username: 0})
231234

232235
flash('You are following %s' % user.username)
233236
return redirect(url_for('user_detail', username=user.username))

examples/twitter/templates/layout.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ <h1><a href="{{ url_for('homepage') }}">Twalrus</a></h1>
99
<a href="{{ url_for('join') }}">join</a>
1010
{% else %}
1111
<a href="{{ url_for('public_timeline') }}">public timeline</a>
12+
<a href="{{ url_for('following') }}">following</a>
13+
<a href="{{ url_for('followers') }}">followers</a>
1214
<a href="{{ url_for('create') }}">create</a>
1315
<a href="{{ url_for('logout') }}">log out</a>
1416
{% endif %}

examples/twitter/templates/user_followers.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{% block body %}
33
<h2>Followers</h2>
44
<ul>
5-
{% for user in current_user.followers() %}
5+
{% for user in user_list %}
66
<li><a href="{{ url_for('user_detail', username=user.username) }}">{{ user.username }}</a></li>
77
{% endfor %}
88
</ul>

examples/twitter/templates/user_following.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{% block body %}
33
<h2>Following</h2>
44
<ul>
5-
{% for user in current_user.following() %}
5+
{% for user in user_list %}
66
<li><a href="{{ url_for('user_detail', username=user.username) }}">{{ user.username }}</a></li>
77
{% endfor %}
88
</ul>

0 commit comments

Comments
 (0)