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: if not args:
print('usage: ./ctl json_dump dump.json') print('usage: ./ctl json_dump dump.json')
sys.exit(1) sys.exit(1)
import json
from ox.django.shortcuts import _to_json
import item.models
import db import db
import state
with db.session(): with db.session():
items = [] library = state.user().library
for i in item.models.Item.query: library.export_json(args[0])
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)
def main(): def main():
actions = globals() actions = globals()

View file

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

View file

@ -337,6 +337,24 @@ class List(db.Model):
def create_symlinks(self): def create_symlinks(self):
pass 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): class Metadata(db.Model):
__tablename__ = 'user_metadata' __tablename__ = 'user_metadata'
@ -475,6 +493,7 @@ def export_list(data):
for f in list(existing_files - new_files): for f in list(existing_files - new_files):
os.unlink(f) os.unlink(f)
utils.remove_empty_folders(prefix) utils.remove_empty_folders(prefix)
self.export_json(os.path.join(prefix, 'library.json'))
trigger_event('activity', { trigger_event('activity', {
'activity': 'export', 'activity': 'export',
'progress': [count, count], 'progress': [count, count],

View file

@ -383,3 +383,11 @@ def can_connect_dns(host="8.8.8.8", port=53):
except: except:
pass pass
return False 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)))