commit 2a4b2ba7f5b822b1556bb247a2b9ef2d9330722b
parent 1d55c5b1343242e0c9efaa25c5296f6abc64e95b
Author: parazyd <parazyd@dyne.org>
Date: Wed, 23 Jan 2019 13:29:42 +0100
Implement get_profile_from stories to reduce duplicated code.
Diffstat:
2 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/diaspora.py b/diaspora.py
@@ -36,7 +36,8 @@ from utils import (get_story, makenav, randomstring, getcountryname,
validate_user, get_multiple_users, get_messages,
get_latest_messages, send_message, delete_user,
get_recent_stories, follow_user, unfollow_user,
- is_following, get_following, send_shout, get_shouts)
+ is_following, get_following, send_shout, get_shouts,
+ get_profiles_from_stories)
app = Flask(__name__)
@@ -336,18 +337,11 @@ def country():
('embark', ccfrom))
stories = get_multiple_stories('disembark', ccode)
+ profiles = get_profiles_from_stories(stories)
clist = [(i['embark'], i['embarkname']) for i in stories]
clist = list(set(clist))
- profiles = {}
- for i in stories:
- if i['email']:
- uid = sql_select_col_where('id', 'email', i['email'],
- table='users')
- if uid:
- profiles[i['email']] = uid[0][0]
-
return render_template('country.html', country=getcountryname(ccode),
cc=ccode, stories=stories, clist=clist,
filtered_stories=filtered, profiles=profiles)
@@ -426,18 +420,11 @@ def dashboard():
('disembark', ccto))
stories = get_multiple_stories('visible', fparam[act])
+ profiles = get_profiles_from_stories(stories)
clist = [(i['disembark'], i['disembarkname']) for i in stories]
clist = list(set(clist))
- profiles = {}
- for i in stories:
- if i['email']:
- uid = sql_select_col_where('id', 'email', i['email'],
- table='users')
- if uid:
- profiles[i['email']] = uid[0][0]
-
return render_template('dashboard.html', stories=stories,
filtered_stories=filtered, profiles=profiles,
listtype=act, clist=clist, ccto=ccto)
@@ -486,7 +473,8 @@ def main():
Main route, the homepage.
"""
stories = get_recent_stories()
- return render_template('index.html', stories=stories, profiles={})
+ profiles = get_profiles_from_stories(stories)
+ return render_template('index.html', stories=stories, profiles=profiles)
if __name__ == '__main__':
diff --git a/utils.py b/utils.py
@@ -484,3 +484,18 @@ def delete_user(user_id):
rmtree(join('messages', user['email']), ignore_errors=True)
remove(join('follows', user['email']))
sql_delete_row_where('id', user_id, table='users')
+
+
+def get_profiles_from_stories(stories):
+ """
+ Map emails to ids from given stories.
+ """
+ profiles = {}
+ for i in stories:
+ if i['email']:
+ uid = sql_select_col_where('id', 'email', i['email'],
+ table='users')
+ if uid:
+ profiles[i['email']] = uid[0][0]
+
+ return profiles