force backups to be treated as a replace

This commit is contained in:
Damien Elmes 2012-10-24 18:11:39 +09:00
parent c64f5130e2
commit 418acb3365
2 changed files with 17 additions and 12 deletions

View file

@ -316,28 +316,32 @@ Unable to import from a read-only file."""))
def setupApkgImport(mw, importer): def setupApkgImport(mw, importer):
meta = None meta = None
forceFull = False
try: try:
z = zipfile.ZipFile(importer.file) z = zipfile.ZipFile(importer.file)
meta = json.load(z.open("meta")) meta = json.load(z.open("meta"))
if not meta['full']: if not meta['full']:
# add # add
return True return True
elif meta['full'] == "force":
forceFull = True
except: except:
# no meta attribute on broken file # no meta attribute on broken file
pass pass
# if old file or full collection, we have to prompt user # if old file or full collection, we have to prompt user
diag = askUserDialog(_("""\ if not forceFull:
diag = askUserDialog(_("""\
Would you like to add to your collection, or replace it?"""), Would you like to add to your collection, or replace it?"""),
[_("Add"), [_("Add"),
_("Replace"), _("Replace"),
_("Cancel")]) _("Cancel")])
diag.setIcon(QMessageBox.Question) diag.setIcon(QMessageBox.Question)
diag.setDefault(0) diag.setDefault(0)
ret = diag.run() ret = diag.run()
if ret == _("Add"): if ret == _("Add"):
return True return True
elif ret == _("Cancel"): elif ret == _("Cancel"):
return False return False
if not askUser(_("""\ if not askUser(_("""\
This will delete your existing collection and replace it with the data in \ This will delete your existing collection and replace it with the data in \
the file you're importing. Are you sure?"""), msgfunc=QMessageBox.warning): the file you're importing. Are you sure?"""), msgfunc=QMessageBox.warning):

View file

@ -10,7 +10,7 @@ from aqt.qt import *
QtConfig = pyqtconfig.Configuration() QtConfig = pyqtconfig.Configuration()
from anki import Collection from anki import Collection
from anki.utils import stripHTML, checksum, isWin, isMac, intTime from anki.utils import stripHTML, checksum, isWin, isMac, intTime, json
from anki.hooks import runHook, addHook, remHook from anki.hooks import runHook, addHook, remHook
import anki.consts import anki.consts
@ -296,6 +296,7 @@ how to restore from a backup.""")
z = zipfile.ZipFile(newpath, "w", zipfile.ZIP_DEFLATED) z = zipfile.ZipFile(newpath, "w", zipfile.ZIP_DEFLATED)
z.write(path, "collection.anki2") z.write(path, "collection.anki2")
z.writestr("media", "{}") z.writestr("media", "{}")
z.writestr("meta", json.dumps(dict(full="force")))
z.close() z.close()
# remove if over # remove if over
if len(backups) + 1 > nbacks: if len(backups) + 1 > nbacks: