Bladeren bron

Added how long ago field. (secs/mins/hours/days).

root 4 jaren geleden
bovenliggende
commit
a739b89176
2 gewijzigde bestanden met toevoegingen van 19 en 1 verwijderingen
  1. 17 1
      messages.py
  2. 2 0
      templates/lastcallers.html

+ 17 - 1
messages.py

@@ -45,6 +45,9 @@ cache = Cache(
 
 import sqlite3
 
+# Should this be in the actual calls?
+# (So I don't keep a connection open all the time?)
+
 dbconnect = sqlite3.connect("db/message.sqlite3")
 dbc = dbconnect.cursor()
 
@@ -317,6 +320,16 @@ def display_message(area, msgno):
 
 # LAST CALLERS PROCESSING
 
+def time_duration(time_delta):
+    if (time_delta.in_seconds() < 60):
+        return "{0} seconds ago".format(time_delta.in_seconds())
+    if (time_delta.in_minutes() < 60):
+        return "{0} minutes ago".format(time_delta.in_minutes())
+    if (time_delta.in_hours() < 24):
+        return "{0} hours ago".format(time_delta.in_hours())
+    return "{0} days ago".format(time_delta.in_days())
+    # in_months, in_years ...
+
 @cache.memoize(timeout=60)
 def last_bbs_callers():
     dbsystem = sqlite3.connect("db/system.sqlite3")
@@ -326,11 +339,14 @@ def last_bbs_callers():
     # step 1:  get list of last 25 callers
     users = []
     lookup = set()
+    now = pendulum.now()
     for row in dbsys.execute('select id,timestamp,log_value from system_event_log where log_name="user_login_history" order by id desc limit 25;'):
         # Ok!
         # row[0], row[1], row[2]
         jdata = json.loads(row[2])
-        caller = { 'logid': row[0], 'timestamp': row[1], 'userId': jdata['userId'] }
+        called_when = pendulum.parse(row[1])
+        long_ago = time_duration(now - called_when)
+        caller = { 'logid': row[0], 'timestamp': row[1], 'userId': jdata['userId'], 'ago': long_ago }
         lookup.add(jdata['userId'])
         users.append(caller)
     # Ok, we have a list of userIds to look up.

+ 2 - 0
templates/lastcallers.html

@@ -10,6 +10,7 @@
 
 <div class="grid-x padding-x">
 <div class="large-4 cell head">Timestamp</div>
+<div class="large-2 cell head">Ago</div>
 <div class="large-2 cell head">Username</div>
 <div class="large-2 cell head">Location</div>
 </div>
@@ -17,6 +18,7 @@
 {% for user in users %}
 <div class="grid-x padding-x">
 <div class="large-4 cell">{{ user.timestamp }}</div>
+<div class="large-2 cell">{{ user.ago }}</div>
 <div class="large-2 cell">{{ user.username }}</div>
 <div class="large-2 cell">{{ user.location }}</div>
 </div>