add per list export_json

This commit is contained in:
j 2016-01-15 13:29:35 +05:30
parent 0cd6032816
commit b961086475
4 changed files with 31 additions and 12 deletions

View File

@ -242,20 +242,11 @@ def command_dump_json(*args):
if not args:
print('usage: ./ctl json_dump dump.json')
sys.exit(1)
import json
from ox.django.shortcuts import _to_json
import item.models
import db
import state
with db.session():
items = []
for i in item.models.Item.query:
j = i.json()
for f in i.files:
j['path'] = f.fullpath()
break
items.append(j)
with open(args[0], 'w') as f:
json.dump(items, f, indent=1, default=_to_json, ensure_ascii=False, sort_keys=True)
library = state.user().library
library.export_json(args[0])
def main():
actions = globals()

View File

@ -28,6 +28,7 @@ class Downloads(Thread):
if now > settings.server.get('last_update_check', 0) + 24*60*60:
settings.server['last_update_check'] = now
update.download()
state.user().library.export_json()
def download_next(self):
import item.models

View File

@ -337,6 +337,24 @@ class List(db.Model):
def create_symlinks(self):
pass
def export_json(self, path=None):
if not path:
if self.name:
name = os.path.join('Lists', self.name)
else:
name = 'Books'
path = os.path.join(os.path.expanduser(settings.preferences['libraryPath']), name, 'library.json')
from utils import _to_json
items = []
for i in self.get_items():
j = i.json()
for f in i.files:
j['path'] = f.path
break
items.append(j)
with open(path, 'w') as f:
json.dump(items, f, indent=1, default=_to_json, ensure_ascii=False, sort_keys=True)
class Metadata(db.Model):
__tablename__ = 'user_metadata'
@ -475,6 +493,7 @@ def export_list(data):
for f in list(existing_files - new_files):
os.unlink(f)
utils.remove_empty_folders(prefix)
self.export_json(os.path.join(prefix, 'library.json'))
trigger_event('activity', {
'activity': 'export',
'progress': [count, count],

View File

@ -383,3 +383,11 @@ def can_connect_dns(host="8.8.8.8", port=53):
except:
pass
return False
def _to_json(python_object):
if isinstance(python_object, datetime):
if python_object.year < 1900:
tt = python_object.timetuple()
return '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6])
return python_object.strftime('%Y-%m-%dT%H:%M:%SZ')
raise TypeError(u'%s %s is not JSON serializable' % (repr(python_object), type(python_object)))