Skip to content

Commit

Permalink
Fix timezones with SQL/JS changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tavinathanson committed Apr 2, 2015
1 parent 15050aa commit 2d2390d
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 30 deletions.
6 changes: 5 additions & 1 deletion cycledash/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flask import jsonify, request
from functools import wraps, partial
from sqlalchemy import exc, select, func, desc
from pytz import utc

from common.helpers import tables, CRUDError
from cycledash import db
Expand Down Expand Up @@ -253,7 +254,10 @@ def convert_col_name(name, user_comments):
def date_as_int(dt):
"""Get dt represented as microseconds since 1970. jsonify will strip out
microseconds, so we use this simpler format."""
delta = dt - datetime.fromtimestamp(0)
# Since dt is a "timezoned" date, the other variable must also be
# Otherwise, we'd get:
# TypeError: can't subtract offset-naive and offset-aware datetimes
delta = dt - datetime.fromtimestamp(0, tz=utc)
return int(delta.total_seconds() * (10**6))


Expand Down
8 changes: 4 additions & 4 deletions cycledash/static/js/comments/components/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ var Comment = React.createClass({
return `/runs/${c.vcf_id}/examine?query=${c.contig}:${c.position}-${1+c.position}`;
},
render: function() {
// Add the offset to get local time
var timezoneOffset = new Date().getTimezoneOffset() * 60 * 1000;
var comment = this.props.comment,
relativeDate = moment(new Date(comment.created)).add(timezoneOffset).fromNow();
var comment = this.props.comment;
// moment uses the local timezone by default (converting the
// value, which starts in UTC, to that timezone)
var relativeDate = moment(comment.created).fromNow();
var authorName = comment.author_name ?
comment.author_name.slice(0, 15) : 'Anonymous';
return (
Expand Down
27 changes: 10 additions & 17 deletions cycledash/static/js/examine/components/CommentBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ var CommentBox = React.createClass({
getHandleSaveForCreate: function() {
var handleSetComment = this.props.handleSetComment;
var record = this.props.record;
var timezoneOffset = this.getTimezoneOffsetMillis();
return function(commentText, authorName) {
// Subtract the offset to get GMT (to match what's in the DB)
var createdDate = new Date(new Date().getTime() -
timezoneOffset);
var newComment = _.extend(
_.pick(
record,
Expand All @@ -85,9 +82,9 @@ var CommentBox = React.createClass({
'author_name': authorName,
// Note: this is a temporary date that does not get persisted
// to the DB. Instead, the DB creates its own date, but this
// date is used for distinguishing between comments in the
// meantime.
'created_date': createdDate});
// date string is used for distinguishing between comments in
// the meantime. (UTC, because DB dates are UTC.)
'created_date': moment.utc().format()});
handleSetComment(newComment, record);
};
},
Expand All @@ -100,23 +97,19 @@ var CommentBox = React.createClass({
store.set(this.LOCAL_STORAGE_AUTHOR_KEY, authorName);
}
},
getTimezoneOffsetMillis: function() {
var timezoneOffset = new Date().getTimezoneOffset();
return timezoneOffset * 60 * 1000;
},
render: function() {
var comments = this.props.record.comments;
var timezoneOffset = this.getTimezoneOffsetMillis();
var commentNodes = _.sortBy(comments, comment => {
return new Date(comment.created_date).getTime();
}).map(comment => {
// Add the offset to get local time
var createdTimestampMillis = new Date(comment.created_date).
getTime() + timezoneOffset;
var rowKey = utils.getRowKey(this.props.record);
// moment uses the local timezone by default (converting the
// value, which starts in UTC, to that timezone)
var createdString = moment(comment.created_date).fromNow();
// Prevent react key collisions
var rowKey = utils.getRowKey(this.props.record);
var reactKey = _.has(comment, 'id') ?
rowKey + comment.id : rowKey + String(createdTimestampMillis);
rowKey + comment.id :
rowKey + String(comment.created_date.getTime());
return <VCFComment record={this.props.record}
commentText={comment.comment_text}
key={reactKey}
Expand All @@ -125,7 +118,7 @@ var CommentBox = React.createClass({
cancelable={true}
saveLocalAuthorName={this.saveLocalAuthorName}
authorName={comment.author_name}
createdString={moment(createdTimestampMillis).fromNow()}
createdString={createdString}
handleDelete={this.getHandleDelete(comment)} />;
});

Expand Down
4 changes: 2 additions & 2 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ CREATE TABLE user_comments (
alternates TEXT,
comment_text TEXT NOT NULL,
author_name TEXT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_modified TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE task_states (
Expand Down
12 changes: 6 additions & 6 deletions tests/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ SELECT pg_catalog.setval('task_states_id_seq', 2, true);
--

COPY user_comments (id, vcf_id, sample_name, contig, "position", reference, alternates, comment_text, author_name, created, last_modified) FROM stdin;
1 1 NORMAL 20 61795 G T This is a comment on the second row! \N 2015-03-24 15:29:09.615967 2015-03-11 23:33:33.951753
2 1 NORMAL 20 65900 G A *This* is a comment with [Markdown](http://daringfireball.net/projects/markdown/syntax). \N 2015-03-24 15:29:09.615967 2015-03-11 23:34:18.19118
3 1 TUMOR 20 61795 G T *This* is a comment on the first variant with [Markdown](http://daringfireball.net/projects/markdown/syntax).\n \N 2015-03-24 15:29:09.615967 2015-03-12 15:30:20.570952
4 1 NORMAL 20 61795 G T This is another comment on the second row! \N 2015-03-24 17:40:44.984191 2015-03-24 17:40:44.984191
6 1 TUMOR 20 61795 G T This is a comment without Markdown by Bob! Bob 2015-03-24 17:46:40.962213 2015-03-24 17:46:40.962213
5 1 TUMOR 20 61795 G T This is a comment without Markdown! \N 2015-03-24 17:43:23.497883 2015-03-24 17:43:23.497883
1 1 NORMAL 20 61795 G T This is a comment on the second row! \N 2015-03-24 15:29:09.615967+00 2015-03-11 23:33:33.951753+00
2 1 NORMAL 20 65900 G A *This* is a comment with [Markdown](http://daringfireball.net/projects/markdown/syntax). \N 2015-03-24 15:29:09.615967+00 2015-03-11 23:34:18.19118+00
3 1 TUMOR 20 61795 G T *This* is a comment on the first variant with [Markdown](http://daringfireball.net/projects/markdown/syntax).\n \N 2015-03-24 15:29:09.615967+00 2015-03-12 15:30:20.570952+00
4 1 NORMAL 20 61795 G T This is another comment on the second row! \N 2015-03-24 17:40:44.984191+00 2015-03-24 17:40:44.984191+00
6 1 TUMOR 20 61795 G T This is a comment without Markdown by Bob! Bob 2015-03-24 17:46:40.962213+00 2015-03-24 17:46:40.962213+00
5 1 TUMOR 20 61795 G T This is a comment without Markdown! \N 2015-03-24 17:43:23.497883+00 2015-03-24 17:43:23.497883+00
\.


Expand Down
Binary file modified tests/pdifftests/images/examine_comments-edit.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/pdifftests/images/examine_comments-view.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/pdifftests/images/examine_sorted.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/pdifftests/images/examine_tooltip.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/pdifftests/images/runs_bams.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/pdifftests/images/runs_info.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/pdifftests/images/runs_page.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/pdifftests/images/website_comments.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2d2390d

Please sign in to comment.