Skip to content

Commit

Permalink
views.py: Add last generated timestamp
Browse files Browse the repository at this point in the history
This adds a UTC last generated timestamp to the view as well as how
long ago it was updated.

Closes #7
  • Loading branch information
andrewda committed Dec 4, 2017
1 parent b951ad9 commit 29723c1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .coafile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ bears = LineLengthBear
[all.links]
bears = InvalidLinkBear

[javascript]
files = static/**/*.js
bears = JSHintBear
allow_unused_variables = True
javascript_strictness = False

[generalization]
# Do not allow the word "coala" to ensure the repository can be generalized out
# for use by other organizations.
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ env:

before_script:
- pip install coala-bears
- npm i -g jshint

script:
- coala --non-interactive -V

after_success:
- mkdir _site public
- python manage.py collectstatic --noinput
- python manage.py distill-local public --force
- ./.ci/deploy.sh
1 change: 1 addition & 0 deletions community/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@

STATIC_URL = '/static/'
STATIC_ROOT = '_site/'
STATICFILES_DIRS = ['static/']
13 changes: 13 additions & 0 deletions gci/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.http import HttpResponse
from datetime import datetime
from calendar import timegm

from .students import get_students, get_linked_students

Expand Down Expand Up @@ -28,4 +30,15 @@ def index(request):
'<a href="https://github.com/{username}">{username}</a>'
.format(student_url=student_url, student_id=student_id,
username=username))

timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
s.append('</ul><i id="time" class="timestamp" data-time="{unix}">'
'Last updated: {timestamp} '
'(<span id="ago" class="timeago"></span>)</i>'
.format(unix=timegm(datetime.utcnow().utctimetuple()),
timestamp=timestamp))

s.append('<script src="static/timeago.js"></script>')
s.append('<script>loadTimeElements()</script>')

return HttpResponse('\n'.join(s))
30 changes: 30 additions & 0 deletions static/timeago.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function generateTimeString(timestamp) {
var sec = ((new Date()).getTime() / 1000) - parseInt(timestamp);
var min = sec / 60;
var hour = min / 60;
var day = hour / 24;

var timeString = '';
if (day >= 1) {
timeString = Math.round(day) + ' days ago';
} else if (hour >= 1) {
timeString = Math.round(hour) + ' hours ago';
} else if (min >= 1) {
timeString = Math.round(min) + ' minutes ago';
} else {
timeString = Math.round(sec) + ' seconds ago';
}

return timeString;
}

function updateTimeAgo(time, ago) {
ago.innerHTML = generateTimeString(time.getAttribute('data-time'));
}

function loadTimeElements() {
var time = document.getElementById('time');
var ago = document.getElementById('ago');

updateTimeAgo(time, ago);
}

0 comments on commit 29723c1

Please sign in to comment.