mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
remove utcOffset; make it a property of crt instead
This commit is contained in:
parent
442bb7a7db
commit
ccc325f87b
6 changed files with 38 additions and 36 deletions
29
anki/deck.py
29
anki/deck.py
|
@ -2,7 +2,7 @@
|
||||||
# Copyright: Damien Elmes <anki@ichi2.net>
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
||||||
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
|
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
|
||||||
|
|
||||||
import time, os, random, re, stat, simplejson
|
import time, os, random, re, stat, simplejson, datetime
|
||||||
|
|
||||||
from anki.lang import _, ngettext
|
from anki.lang import _, ngettext
|
||||||
from anki.utils import parseTags, tidyHTML, ids2str, hexifyID, \
|
from anki.utils import parseTags, tidyHTML, ids2str, hexifyID, \
|
||||||
|
@ -62,10 +62,12 @@ class _Deck(object):
|
||||||
self.db = db
|
self.db = db
|
||||||
self.path = db._path
|
self.path = db._path
|
||||||
self.load()
|
self.load()
|
||||||
if self.utcOffset == -2:
|
if not self.crt:
|
||||||
# shared deck; reset timezone and creation date
|
d = datetime.datetime.today()
|
||||||
self.utcOffset = time.timezone + 60*60*4
|
d -= datetime.timedelta(hours=4)
|
||||||
self.crt = intTime()
|
d = datetime.datetime(d.year, d.month, d.day)
|
||||||
|
d += datetime.timedelta(hours=4)
|
||||||
|
self.crt = int(time.mktime(d.timetuple()))
|
||||||
self.undoEnabled = False
|
self.undoEnabled = False
|
||||||
self.sessionStartReps = 0
|
self.sessionStartReps = 0
|
||||||
self.sessionStartTime = 0
|
self.sessionStartTime = 0
|
||||||
|
@ -86,15 +88,14 @@ class _Deck(object):
|
||||||
def load(self):
|
def load(self):
|
||||||
(self.crt,
|
(self.crt,
|
||||||
self.mod,
|
self.mod,
|
||||||
self.schema,
|
self.scm,
|
||||||
self.syncName,
|
self.syncName,
|
||||||
self.lastSync,
|
self.lastSync,
|
||||||
self.utcOffset,
|
|
||||||
self.qconf,
|
self.qconf,
|
||||||
self.conf,
|
self.conf,
|
||||||
self.data) = self.db.first("""
|
self.data) = self.db.first("""
|
||||||
select crt, mod, schema, syncName, lastSync,
|
select crt, mod, scm, syncName, lastSync,
|
||||||
utcOffset, qconf, conf, data from deck""")
|
qconf, conf, data from deck""")
|
||||||
self.qconf = simplejson.loads(self.qconf)
|
self.qconf = simplejson.loads(self.qconf)
|
||||||
self.conf = simplejson.loads(self.conf)
|
self.conf = simplejson.loads(self.conf)
|
||||||
self.data = simplejson.loads(self.data)
|
self.data = simplejson.loads(self.data)
|
||||||
|
@ -104,10 +105,10 @@ utcOffset, qconf, conf, data from deck""")
|
||||||
self.mod = intTime()
|
self.mod = intTime()
|
||||||
self.db.execute(
|
self.db.execute(
|
||||||
"""update deck set
|
"""update deck set
|
||||||
mod=?, schema=?, syncName=?, lastSync=?, utcOffset=?,
|
mod=?, scm=?, syncName=?, lastSync=?,
|
||||||
qconf=?, conf=?, data=?""",
|
qconf=?, conf=?, data=?""",
|
||||||
self.mod, self.schema, self.syncName, self.lastSync,
|
self.mod, self.scm, self.syncName, self.lastSync,
|
||||||
self.utcOffset, simplejson.dumps(self.qconf),
|
simplejson.dumps(self.qconf),
|
||||||
simplejson.dumps(self.conf), simplejson.dumps(self.data))
|
simplejson.dumps(self.conf), simplejson.dumps(self.data))
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
@ -144,11 +145,11 @@ qconf=?, conf=?, data=?""",
|
||||||
if not self.schemaDirty():
|
if not self.schemaDirty():
|
||||||
# next sync will be full
|
# next sync will be full
|
||||||
self.emptyTrash()
|
self.emptyTrash()
|
||||||
self.schema = intTime()
|
self.scm = intTime()
|
||||||
|
|
||||||
def schemaDirty(self):
|
def schemaDirty(self):
|
||||||
"True if schema changed since last sync, or syncing off."
|
"True if schema changed since last sync, or syncing off."
|
||||||
return self.schema > self.lastSync
|
return self.scm > self.lastSync
|
||||||
|
|
||||||
# Object creation helpers
|
# Object creation helpers
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
|
@ -94,7 +94,7 @@ class AnkiExporter(Exporter):
|
||||||
self.newDeck.rebuildCounts()
|
self.newDeck.rebuildCounts()
|
||||||
# FIXME
|
# FIXME
|
||||||
#self.exportedCards = self.newDeck.cardCount
|
#self.exportedCards = self.newDeck.cardCount
|
||||||
self.newDeck.utcOffset = -1
|
self.newDeck.crt = 0
|
||||||
self.newDeck.db.commit()
|
self.newDeck.db.commit()
|
||||||
self.newDeck.close()
|
self.newDeck.close()
|
||||||
|
|
||||||
|
|
|
@ -448,19 +448,10 @@ queue = 2 %s and due <= :lim order by %s limit %d""" % (
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
def _updateCutoff(self):
|
def _updateCutoff(self):
|
||||||
d = datetime.datetime.utcfromtimestamp(
|
# days since deck created
|
||||||
time.time() - self.deck.utcOffset) + datetime.timedelta(days=1)
|
self.today = int((time.time() - self.deck.crt) / 86400)
|
||||||
d = datetime.datetime(d.year, d.month, d.day)
|
# end of day cutoff
|
||||||
newday = self.deck.utcOffset - time.timezone
|
self.dayCutoff = self.deck.crt + (self.today+1)*86400
|
||||||
d += datetime.timedelta(seconds=newday)
|
|
||||||
cutoff = time.mktime(d.timetuple())
|
|
||||||
# cutoff must not be in the past
|
|
||||||
while cutoff < time.time():
|
|
||||||
cutoff += 86400
|
|
||||||
# cutoff must not be more than 24 hours in the future
|
|
||||||
cutoff = min(time.time() + 86400, cutoff)
|
|
||||||
self.dayCutoff = cutoff
|
|
||||||
self.today = int(cutoff/86400 - self.deck.crt/86400)
|
|
||||||
|
|
||||||
def _checkDay(self):
|
def _checkDay(self):
|
||||||
# check if the day has rolled over
|
# check if the day has rolled over
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
CURRENT_VERSION = 100
|
CURRENT_VERSION = 100
|
||||||
|
|
||||||
import os, time, simplejson, re
|
import os, time, simplejson, re, datetime
|
||||||
from anki.lang import _
|
from anki.lang import _
|
||||||
from anki.utils import intTime
|
from anki.utils import intTime
|
||||||
from anki.db import DB
|
from anki.db import DB
|
||||||
|
@ -57,11 +57,10 @@ create table if not exists deck (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
crt integer not null,
|
crt integer not null,
|
||||||
mod integer not null,
|
mod integer not null,
|
||||||
|
scm integer not null,
|
||||||
ver integer not null,
|
ver integer not null,
|
||||||
schema integer not null,
|
|
||||||
syncName text not null,
|
syncName text not null,
|
||||||
lastSync integer not null,
|
lastSync integer not null,
|
||||||
utcOffset integer not null,
|
|
||||||
qconf text not null,
|
qconf text not null,
|
||||||
conf text not null,
|
conf text not null,
|
||||||
data text not null
|
data text not null
|
||||||
|
@ -149,8 +148,8 @@ create table if not exists tags (
|
||||||
);
|
);
|
||||||
|
|
||||||
insert or ignore into deck
|
insert or ignore into deck
|
||||||
values(1,%(t)s,%(t)s,%(t)s,%(v)s,'',0,-2,'', '', '');
|
values(1,0,0,0,%(v)s,'',0,'', '', '');
|
||||||
""" % ({'t': intTime(), 'v':CURRENT_VERSION}))
|
""" % ({'v':CURRENT_VERSION}))
|
||||||
import anki.deck
|
import anki.deck
|
||||||
import anki.groups
|
import anki.groups
|
||||||
# create a default group/configuration, which should not be removed
|
# create a default group/configuration, which should not be removed
|
||||||
|
@ -355,7 +354,7 @@ def _migrateDeckTbl(db):
|
||||||
db.execute("""
|
db.execute("""
|
||||||
insert or replace into deck select id, cast(created as int), :t,
|
insert or replace into deck select id, cast(created as int), :t,
|
||||||
:t, 99, ifnull(syncName, ""), cast(lastSync as int),
|
:t, 99, ifnull(syncName, ""), cast(lastSync as int),
|
||||||
utcOffset, "", "", "" from decks""", t=intTime())
|
"", "", "" from decks""", t=intTime())
|
||||||
# update selective study
|
# update selective study
|
||||||
qconf = anki.deck.defaultQconf.copy()
|
qconf = anki.deck.defaultQconf.copy()
|
||||||
# delete old selective study settings, which we can't auto-upgrade easily
|
# delete old selective study settings, which we can't auto-upgrade easily
|
||||||
|
@ -484,7 +483,15 @@ def _fixupModels(deck):
|
||||||
def _postSchemaUpgrade(deck):
|
def _postSchemaUpgrade(deck):
|
||||||
"Handle the rest of the upgrade to 2.0."
|
"Handle the rest of the upgrade to 2.0."
|
||||||
import anki.deck
|
import anki.deck
|
||||||
|
# adjust models
|
||||||
_fixupModels(deck)
|
_fixupModels(deck)
|
||||||
|
# fix creation time
|
||||||
|
d = datetime.datetime.today()
|
||||||
|
d -= datetime.timedelta(hours=4)
|
||||||
|
d = datetime.datetime(d.year, d.month, d.day)
|
||||||
|
d += datetime.timedelta(hours=4)
|
||||||
|
d -= datetime.timedelta(days=1+int((time.time()-deck.crt)/86400))
|
||||||
|
deck.crt = int(time.mktime(d.timetuple()))
|
||||||
# update uniq cache
|
# update uniq cache
|
||||||
deck.updateFieldCache(deck.db.list("select id from facts"))
|
deck.updateFieldCache(deck.db.list("select id from facts"))
|
||||||
# remove old views
|
# remove old views
|
||||||
|
|
|
@ -68,7 +68,7 @@ def test_delete():
|
||||||
deck.addFact(f)
|
deck.addFact(f)
|
||||||
assert deck.cardCount() == 1
|
assert deck.cardCount() == 1
|
||||||
# mark the schema as clean
|
# mark the schema as clean
|
||||||
deck.lastSync = deck.schema + 1
|
deck.lastSync = deck.scm + 1
|
||||||
# cards/facts should go in the deletion log instead
|
# cards/facts should go in the deletion log instead
|
||||||
cid = f.cards()[0].id
|
cid = f.cards()[0].id
|
||||||
deck.delCard(cid)
|
deck.delCard(cid)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
import os, re
|
import os, re, datetime
|
||||||
from tests.shared import assertException, getEmptyDeck, testDir
|
from tests.shared import assertException, getEmptyDeck, testDir
|
||||||
|
|
||||||
from anki import Deck
|
from anki import Deck
|
||||||
|
@ -121,6 +121,9 @@ def test_upgrade():
|
||||||
print "upgrade to", dst
|
print "upgrade to", dst
|
||||||
shutil.copy(src, dst)
|
shutil.copy(src, dst)
|
||||||
deck = Deck(dst)
|
deck = Deck(dst)
|
||||||
|
# creation time should have been adjusted
|
||||||
|
d = datetime.datetime.fromtimestamp(deck.crt)
|
||||||
|
assert d.hour == 4 and d.minute == 0
|
||||||
# 3 new, 2 failed, 1 due
|
# 3 new, 2 failed, 1 due
|
||||||
assert deck.sched.counts() == (3,2,1)
|
assert deck.sched.counts() == (3,2,1)
|
||||||
# now's a good time to test the integrity check too
|
# now's a good time to test the integrity check too
|
||||||
|
|
Loading…
Reference in a new issue