diasporadiaries

a platform for writing stories with personal accounts and messages
git clone git://parazyd.org/diasporadiaries.git
Log | Files | Refs | Submodules | README | LICENSE

commit 603b30cdca6f0021dcaa82706bbc7ca5f05be8e9
parent f682744a9cb9c5443eabecc0ed1df2ae224234d6
Author: parazyd <parazyd@dyne.org>
Date:   Wed, 23 Jan 2019 15:07:37 +0100

Implement parsetime for better time printing.

Diffstat:
Mdiaspora.py | 3++-
Mtemplates/gallery.html | 2+-
Mtemplates/messages.html | 4++--
Mtemplates/profile.html | 2+-
Mtemplates/users.html | 2+-
Mtemplates/view.html | 2+-
Mutils.py | 60+++++++++++++++++++++++++++++++++++++-----------------------
7 files changed, 45 insertions(+), 30 deletions(-)

diff --git a/diaspora.py b/diaspora.py @@ -37,12 +37,13 @@ from utils import (get_story, makenav, randomstring, getcountryname, get_latest_messages, send_message, delete_user, get_recent_stories, follow_user, unfollow_user, is_following, get_following, send_shout, get_shouts, - get_profiles_from_stories) + get_profiles_from_stories, parsetime) app = Flask(__name__) app.config['SECRET_KEY'] = SECRET_KEY app.add_template_global(makenav) +app.add_template_global(parsetime) login_manager = LoginManager() login_manager.init_app(app) diff --git a/templates/gallery.html b/templates/gallery.html @@ -44,7 +44,7 @@ {% else %} by {{ i['name'] }}<br> {% endif %} - {{ i['date'] }} {{ i['time'] }} + {{ parsetime(i['time']) }} </p> </div> </div> diff --git a/templates/messages.html b/templates/messages.html @@ -34,7 +34,7 @@ {% else %} <td><span class="fa fa-envelope-open" style="font-size: 1.5em;" title="Read"></span></td> {% endif %} - <td>{{ i[2]['time'] }}</td> + <td>{{ parsetime(i[2]['time']) }}</td> </tr> {% endfor %} </tbody> @@ -61,7 +61,7 @@ <hr> <blockquote class="blockquote"> <p>{{ i['message'] }}</p> - <footer class="blockquote-footer">{{ i['from'] }} - {{ i['time'] }}</footer> + <footer class="blockquote-footer">{{ i['from'] }} - {{ parsetime(i['time']) }}</footer> </blockquote> {% endfor %} diff --git a/templates/profile.html b/templates/profile.html @@ -77,7 +77,7 @@ <hr> <blockquote class="blockquote"> <p>{{ i['message'] }}</p> - <footer class="blockquote-footer">{{ i['from'] }} - {{ i['time'] }}</footer> + <footer class="blockquote-footer">{{ i['from'] }} - {{ parsetime(i['time']) }}</footer> </blockquote> {% endfor %} <hr> diff --git a/templates/users.html b/templates/users.html @@ -38,7 +38,7 @@ <td>{{ i['email'] }}</td> <td><input form="form_{{ i['id'] }}" class="form-control" type="text" name="password" placeholder="New password"></td> <td><input form="form_{{ i['id'] }}" class="form-control" type="text" name="cap" placeholder="{{ i['cap'] }} (0=writer, 2=admin)"></td> - <td>{{ i['first_seen'] }}</td> + <td>{{ parsetime(i['first_seen']) }}</td> <td> <button type="submit" form="form_{{ i['id'] }}" class="btn btn-outline-success"> <span class="fa fa-check"></span> diff --git a/templates/view.html b/templates/view.html @@ -12,7 +12,7 @@ <hr> <p> - by {{ story['name'] }}, {{ story['date'] }} {{ story['time'] }} + by {{ story['name'] }}, {{ parsetime(story['time']) }} {% if story['city'] %} <br>in story['city'] {% endif %} diff --git a/utils.py b/utils.py @@ -70,8 +70,7 @@ def fill_story(row): 'email': row[4], 'city': row[5], 'story': row[6], - 'date': strftime('%d.%m.%Y.', gmtime(row[7])), - 'time': strftime('%H:%M UTC', gmtime(row[7])), + 'time': row[7], 'visible': row[8], 'deletekey': row[9], 'abstract': row[10], @@ -182,8 +181,8 @@ def fill_user_dict(row): 'name': row[2], 'password': row[3], 'cap': row[4], - 'first_seen': strftime('%d.%m.%Y. %H:%M UTC', gmtime(row[5])), - 'is_active': row[6], + 'first_seen': row[5], + 'is_active': row[7], } @@ -282,12 +281,8 @@ def get_latest_messages(user_id): data[-1]['message'] = data[-1]['message'][:64] + "..." latest.append([user['name'], user['id'], data[-1]]) - sorted_latest = [] - for i in sorted(latest, key=lambda x: x[2]['time'], reverse=True): - i[2]['time'] = strftime('%d.%m.%Y. %H:%M UTC', gmtime(i[2]['time'])) - sorted_latest.append(i) - return sorted_latest + return sorted(latest, key=lambda x: x[2]['time'], reverse=True) def get_messages(user_id, id_from): @@ -311,18 +306,13 @@ def get_messages(user_id, id_from): json.dump(data, msgfile) return [] - marked = [] + messages = [] for i in data: i['unread'] = 0 - marked.append(i) + messages.append(i) with open(msgpath, 'w') as msgfile: - json.dump(marked, msgfile) - - messages = [] - for j in marked: - j['time'] = strftime('%d.%m.%Y. %H:%M UTC', gmtime(j['time'])) - messages.append(j) + json.dump(messages, msgfile) return messages @@ -412,14 +402,10 @@ def get_shouts(email): if not isfile(join('shouts', email)): return [] - theirdata = [] + shouts = [] with open(join('shouts', email)) as shouts: - theirdata = json.load(shouts) + shouts = json.load(shouts) - shouts = [] - for i in theirdata: - i['time'] = strftime('%d.%m.%Y. %H:%M UTC', gmtime(i['time'])) - shouts.append(i) return shouts[::-1] @@ -511,3 +497,31 @@ def get_profiles_from_stories(stories): profiles[i['email']] = uid[0][0] return profiles + + +def parsetime(then): + """ + Parse epoch to return human-readable time + """ + ds = int(time()) - then + dm = int(ds / 60) + + def f(x): + if int(x) != 1: + return 's' + return '' + + if ds < 60: + return '%d second%s ago' % (ds, f(ds)) + elif dm < 60: + return '%d minute%s ago' % (dm, f(dm)) + elif dm < (24 * 60): + return '%d hour%s ago' % (int(dm / 60), f(dm/60)) + elif dm < (24 * 60 * 7): + return '%d day%s ago' % (int(dm / (60*24)), f(dm/(60*24))) + # elif dm < (24 * 60 * 31): + # return '%d week%s ago' % (int(dm / (60*24*7)), f(dm/(60*24*7))) + # elif dm < (24 * 60 * 365.25): + # return '%d month%s ago' % (int(dm / (60*24*30), f(dm/60*24*30))) + # return '%d year%s ago' % (int(dm / (60*24*365)), f(dm/(60*24*365))) + return strftime('%d.%m.%Y. %H:%M UTC', gmtime(then))