This commit is contained in:
j 2016-06-24 12:27:05 +02:00
parent 3e5c3c9a47
commit abc0b82961
4 changed files with 48 additions and 47 deletions

View file

@ -157,6 +157,7 @@ class UploadHandler(tornado.web.RequestHandler):
self.set_status(403) self.set_status(403)
self.write('') self.write('')
return return
def save_files(context, request, callback): def save_files(context, request, callback):
listname = request.arguments.get('list', None) listname = request.arguments.get('list', None)
if listname: if listname:
@ -198,7 +199,7 @@ class UploadHandler(tornado.web.RequestHandler):
callback(response) callback(response)
response = yield tornado.gen.Task(save_files, self._context, self.request) response = yield tornado.gen.Task(save_files, self._context, self.request)
if not 'status' in response: if 'status' not in response:
response = json_response(response) response = json_response(response)
response = json_dumps(response) response = json_dumps(response)
self.set_header('Content-Type', 'application/json') self.set_header('Content-Type', 'application/json')

View file

@ -36,8 +36,7 @@ logger = logging.getLogger(__name__)
user_items = sa.Table('useritem', db.metadata, user_items = sa.Table('useritem', db.metadata,
sa.Column('user_id', sa.String(43), sa.ForeignKey('user.id')), sa.Column('user_id', sa.String(43), sa.ForeignKey('user.id')),
sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id')) sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id')))
)
class Item(db.Model): class Item(db.Model):
__tablename__ = 'item' __tablename__ = 'item'
@ -118,10 +117,10 @@ class Item(db.Model):
def add_user(self, user): def add_user(self, user):
from user.models import list_items from user.models import list_items
if not user in self.users: if user not in self.users:
self.users.append(user) self.users.append(user)
l = user.library l = user.library
if not self in l.items: if self not in l.items:
q = list_items.insert({'item_id': self.id, 'list_id': l.id}) q = list_items.insert({'item_id': self.id, 'list_id': l.id})
state.db.session.execute(q) state.db.session.execute(q)
@ -140,7 +139,7 @@ class Item(db.Model):
j['transferadded'] = t['added'] j['transferadded'] = t['added']
j['transferprogress'] = t['progress'] j['transferprogress'] = t['progress']
# unused and slow # unused and slow
#j['users'] = list(map(str, list(self.users))) # j['users'] = list(map(str, list(self.users)))
if self.info: if self.info:
meta_keys = [k for k in self.meta_keys if k != 'pages'] meta_keys = [k for k in self.meta_keys if k != 'pages']
@ -206,7 +205,7 @@ class Item(db.Model):
if value: if value:
value = str(value) value = str(value)
value = ox.sort_string(value).lower() value = ox.sort_string(value).lower()
elif isinstance(value, list): #empty list elif isinstance(value, list): # empty list
value = None value = None
if not value and sort_type != 'boolean': if not value and sort_type != 'boolean':
value = None value = None
@ -251,7 +250,8 @@ class Item(db.Model):
value = self.json().get(key['id'], None) value = self.json().get(key['id'], None)
if key.get('filterMap') and value: if key.get('filterMap') and value:
value = re.compile(key.get('filterMap')).findall(value) value = re.compile(key.get('filterMap')).findall(value)
if value: value = value[0] if value:
value = value[0]
if key.get('type') == 'boolean': if key.get('type') == 'boolean':
value = True if value else False value = True if value else False
value = str(value).lower() value = str(value).lower()
@ -397,7 +397,7 @@ class Item(db.Model):
# of fall back to first peer that has this item # of fall back to first peer that has this item
# in case its not available locally # in case its not available locally
if not sync_from and self.info.get('mediastate') != 'available' and first_peer: if not sync_from and self.info.get('mediastate') != 'available' and first_peer:
#logger.debug('syncing from first peer that has item %s', first_peer) # logger.debug('syncing from first peer that has item %s', first_peer)
sync_from = first_peer sync_from = first_peer
if sync_from: if sync_from:
peer = utils.get_peer(sync_from) peer = utils.get_peer(sync_from)
@ -436,7 +436,7 @@ class Item(db.Model):
def update_cover(self): def update_cover(self):
logger.debug('%s update cover', self.id) logger.debug('%s update cover', self.id)
key = 'cover:%s'%self.id key = 'cover:%s' % self.id
cover = None cover = None
if 'cover' in self.meta and self.meta['cover']: if 'cover' in self.meta and self.meta['cover']:
logger.debug('download cover %s %s', self.id, self.meta['cover']) logger.debug('download cover %s %s', self.id, self.meta['cover'])
@ -459,7 +459,7 @@ class Item(db.Model):
logger.debug('%s update_cover done', self.id) logger.debug('%s update_cover done', self.id)
def get_preview(self): def get_preview(self):
key = 'preview:%s'%self.id key = 'preview:%s' % self.id
data = icons[key] data = icons[key]
if not data: if not data:
preview = self.extract_preview() preview = self.extract_preview()
@ -468,12 +468,12 @@ class Item(db.Model):
def update_preview(self): def update_preview(self):
logger.debug('%s update_preview', self.id) logger.debug('%s update_preview', self.id)
key = 'preview:%s'%self.id key = 'preview:%s' % self.id
preview = self.extract_preview() preview = self.extract_preview()
if preview: if preview:
icons[key] = preview icons[key] = preview
self.info['previewRatio'] = get_ratio(preview) self.info['previewRatio'] = get_ratio(preview)
if not 'coverRatio' in self.info: if 'coverRatio' not in self.info:
self.info['coverRatio'] = self.info['previewRatio'] self.info['coverRatio'] = self.info['previewRatio']
else: else:
del icons[key] del icons[key]
@ -505,7 +505,7 @@ class Item(db.Model):
if key != 'pages': if key != 'pages':
del self.info[key] del self.info[key]
#FIXME get from user_meta # FIXME get from user_meta
if state.online: if state.online:
if 'isbn' in self.meta: if 'isbn' in self.meta:
data = meta.lookup('isbn', self.meta['isbn']) data = meta.lookup('isbn', self.meta['isbn'])
@ -515,13 +515,13 @@ class Item(db.Model):
def queue_download(self): def queue_download(self):
u = state.user() u = state.user()
if not self.id in state.downloads.transfers: if self.id not in state.downloads.transfers:
state.downloads.transfers[self.id] = { state.downloads.transfers[self.id] = {
'added': datetime.utcnow(), 'added': datetime.utcnow(),
'progress': 0 'progress': 0
} }
logger.debug('queue %s for download', self.id) logger.debug('queue %s for download', self.id)
if not u in self.users: if u not in self.users:
self.add_user(u) self.add_user(u)
def save_file(self, content): def save_file(self, content):
@ -730,7 +730,7 @@ class File(db.Model):
def move(self): def move(self):
def format_underscores(string): def format_underscores(string):
return re.sub('^\.|\.$|:|/|\?|<|>', '_', string) return re.sub(r'^\.|\.$|:|/|\?|<|>', '_', string)
prefs = settings.preferences prefs = settings.preferences
prefix = os.sep.join(os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/').split('/')) prefix = os.sep.join(os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/').split('/'))
if not self.item: if not self.item:
@ -833,7 +833,7 @@ def update_sort_table():
sort_indexes = [i[len('ix_sort_'):] for i in layout['indexes'] if i.startswith('ix_sort_')] sort_indexes = [i[len('ix_sort_'):] for i in layout['indexes'] if i.startswith('ix_sort_')]
for col in set(Item.sort_keys)-set(sort_indexes): for col in set(Item.sort_keys)-set(sort_indexes):
sql.append('CREATE INDEX ix_sort_{col} ON sort ({col})'.format(col=col)) sql.append('CREATE INDEX ix_sort_{col} ON sort ({col})'.format(col=col))
if not 'sortvalue' in db.get_table_columns('find'): if 'sortvalue' not in db.get_table_columns('find'):
create_table = str(CreateTable(Find.__table__).compile(db.engine)).split('\n') create_table = str(CreateTable(Find.__table__).compile(db.engine)).split('\n')
col = 'sortvalue' col = 'sortvalue'
add = [r for r in create_table if '\t%s ' % col in r][0].strip()[:-1] add = [r for r in create_table if '\t%s ' % col in r][0].strip()[:-1]
@ -848,7 +848,7 @@ def update_sort_table():
def get_cover(id): def get_cover(id):
delay = 60 delay = 60
if state.online: if state.online:
#logger.debug('get_cover(%s)', id) # logger.debug('get_cover(%s)', id)
with db.session(): with db.session():
i = Item.get(id) i = Item.get(id)
if i: if i:
@ -859,7 +859,7 @@ def get_cover(id):
def get_preview(id): def get_preview(id):
if state.online: if state.online:
#logger.debug('get_preview(%s)', id) # logger.debug('get_preview(%s)', id)
with db.session(): with db.session():
i = Item.get(id) i = Item.get(id)
if i: if i:
@ -869,7 +869,7 @@ def get_preview(id):
time.sleep(0.5) time.sleep(0.5)
def sync_metadata(ids=None): def sync_metadata(ids=None):
#logger.debug('sync_metadata(%s)', len(ids) if len(ids) > 10 else ids) # logger.debug('sync_metadata(%s)', len(ids) if len(ids) > 10 else ids)
step = 1000 step = 1000
delay = 10 delay = 10
with db.session(): with db.session():
@ -893,5 +893,5 @@ def sync_metadata(ids=None):
if later: if later:
if state.main and state.tasks: if state.main and state.tasks:
state.main.call_later(delay, lambda: state.tasks.queue('syncmetadata', later)) state.main.call_later(delay, lambda: state.tasks.queue('syncmetadata', later))
#else: # else:
# logger.debug('sync_metadata done') # logger.debug('sync_metadata done')

View file

@ -1,6 +1,6 @@
# encoding: utf-8 # encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
DEBUG=False DEBUG = False
try: try:
from gi.repository import Gtk, GObject from gi.repository import Gtk, GObject
GObject.threads_init() GObject.threads_init()
@ -62,7 +62,8 @@ class GtkUI:
class TkUI: class TkUI:
def __init__(self): def __init__(self):
self.root = Tk() self.root = Tk()
self.root.withdraw() #hiding tkinter window self.root.withdraw() # hiding tkinter window
def selectFolder(self, data): def selectFolder(self, data):
return tkinter.filedialog.askdirectory(title=data.get("title", "Select Folder")) return tkinter.filedialog.askdirectory(title=data.get("title", "Select Folder"))

View file

@ -160,7 +160,7 @@ class User(db.Model):
self.save() self.save()
if not was_peering: if not was_peering:
Changelog.record(state.user(), 'addpeer', self.id, self.nickname) Changelog.record(state.user(), 'addpeer', self.id, self.nickname)
if not 'index' in self.info: if 'index' not in self.info:
self.info['index'] = max([ self.info['index'] = max([
u.info.get('index', -1) for u in User.query.filter_by(peered=True) u.info.get('index', -1) for u in User.query.filter_by(peered=True)
] + [0]) + 1 ] + [0]) + 1
@ -204,7 +204,7 @@ class User(db.Model):
name = self.info.get('nickname') or self.info.get('username') or 'anonymous' name = self.info.get('nickname') or self.info.get('username') or 'anonymous'
nickname = name nickname = name
n = 2 n = 2
while self.query.filter_by(nickname=nickname).filter(User.id!=self.id).first(): while self.query.filter_by(nickname=nickname).filter(User.id != self.id).first():
nickname = '%s [%d]' % (name, n) nickname = '%s [%d]' % (name, n)
n += 1 n += 1
self.nickname = nickname self.nickname = nickname
@ -238,8 +238,7 @@ class User(db.Model):
list_items = sa.Table('listitem', db.metadata, list_items = sa.Table('listitem', db.metadata,
sa.Column('list_id', sa.Integer(), sa.ForeignKey('list.id')), sa.Column('list_id', sa.Integer(), sa.ForeignKey('list.id')),
sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id')) sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id')))
)
class List(db.Model): class List(db.Model):
__tablename__ = 'list' __tablename__ = 'list'
@ -305,13 +304,13 @@ class List(db.Model):
@classmethod @classmethod
def rename_user(cls, old, new): def rename_user(cls, old, new):
for l in cls.query.filter(cls._query!=None): for l in cls.query.filter(cls._query is not None):
def update_conditions(conditions): def update_conditions(conditions):
changed = False changed = False
for c in conditions: for c in conditions:
if 'conditions' in c: if 'conditions' in c:
changed = update_conditions(c['conditions'] ) changed = update_conditions(c['conditions'])
else: else:
if c.get('key') == 'list' and c.get('value', '').startswith('%s:' % old): if c.get('key') == 'list' and c.get('value', '').startswith('%s:' % old):
c['value'] = '%s:%s' % new, c['value'].split(':', 1)[1] c['value'] = '%s:%s' % new, c['value'].split(':', 1)[1]