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.write('')
return
def save_files(context, request, callback):
listname = request.arguments.get('list', None)
if listname:
@ -198,7 +199,7 @@ class UploadHandler(tornado.web.RequestHandler):
callback(response)
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_dumps(response)
self.set_header('Content-Type', 'application/json')

View file

@ -35,9 +35,8 @@ import logging
logger = logging.getLogger(__name__)
user_items = sa.Table('useritem', db.metadata,
sa.Column('user_id', sa.String(43), sa.ForeignKey('user.id')),
sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id'))
)
sa.Column('user_id', sa.String(43), sa.ForeignKey('user.id')),
sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id')))
class Item(db.Model):
__tablename__ = 'item'
@ -51,12 +50,12 @@ class Item(db.Model):
meta = sa.Column(MutableDict.as_mutable(sa.PickleType(pickler=json_pickler)))
# why is this in db and not in i.e. info?
added = sa.Column(sa.DateTime()) # added to local library
added = sa.Column(sa.DateTime()) # added to local library
accessed = sa.Column(sa.DateTime())
timesaccessed = sa.Column(sa.Integer())
users = sa.orm.relationship('User', secondary=user_items,
backref=sa.orm.backref('items', lazy='dynamic'))
backref=sa.orm.backref('items', lazy='dynamic'))
@property
def timestamp(self):
@ -118,10 +117,10 @@ class Item(db.Model):
def add_user(self, user):
from user.models import list_items
if not user in self.users:
if user not in self.users:
self.users.append(user)
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})
state.db.session.execute(q)
@ -140,7 +139,7 @@ class Item(db.Model):
j['transferadded'] = t['added']
j['transferprogress'] = t['progress']
# unused and slow
#j['users'] = list(map(str, list(self.users)))
# j['users'] = list(map(str, list(self.users)))
if self.info:
meta_keys = [k for k in self.meta_keys if k != 'pages']
@ -206,7 +205,7 @@ class Item(db.Model):
if value:
value = str(value)
value = ox.sort_string(value).lower()
elif isinstance(value, list): #empty list
elif isinstance(value, list): # empty list
value = None
if not value and sort_type != 'boolean':
value = None
@ -247,11 +246,12 @@ class Item(db.Model):
for key in config['itemKeys']:
if key.get('find') or \
key.get('filter') or key.get('type') in [['string'], 'string'] or \
(key.get('type') == 'boolean' and key.get('sort')):
(key.get('type') == 'boolean' and key.get('sort')):
value = self.json().get(key['id'], None)
if key.get('filterMap') and value:
value = re.compile(key.get('filterMap')).findall(value)
if value: value = value[0]
if value:
value = value[0]
if key.get('type') == 'boolean':
value = True if value else False
value = str(value).lower()
@ -271,7 +271,7 @@ class Item(db.Model):
removed_values = current_values[key['id']] - set(value)
if removed_values:
for f in Find.query.filter_by(item_id=self.id,
key=key['id']).filter(Find.value.in_(removed_values)):
key=key['id']).filter(Find.value.in_(removed_values)):
state.db.session.delete(f)
removed_keys = set(current_values) - set(keys)
if removed_keys:
@ -397,7 +397,7 @@ class Item(db.Model):
# of fall back to first peer that has this item
# in case its not available locally
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
if sync_from:
peer = utils.get_peer(sync_from)
@ -436,7 +436,7 @@ class Item(db.Model):
def update_cover(self):
logger.debug('%s update cover', self.id)
key = 'cover:%s'%self.id
key = 'cover:%s' % self.id
cover = None
if 'cover' in self.meta and 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)
def get_preview(self):
key = 'preview:%s'%self.id
key = 'preview:%s' % self.id
data = icons[key]
if not data:
preview = self.extract_preview()
@ -468,12 +468,12 @@ class Item(db.Model):
def update_preview(self):
logger.debug('%s update_preview', self.id)
key = 'preview:%s'%self.id
key = 'preview:%s' % self.id
preview = self.extract_preview()
if preview:
icons[key] = 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']
else:
del icons[key]
@ -505,7 +505,7 @@ class Item(db.Model):
if key != 'pages':
del self.info[key]
#FIXME get from user_meta
# FIXME get from user_meta
if state.online:
if 'isbn' in self.meta:
data = meta.lookup('isbn', self.meta['isbn'])
@ -515,13 +515,13 @@ class Item(db.Model):
def queue_download(self):
u = state.user()
if not self.id in state.downloads.transfers:
if self.id not in state.downloads.transfers:
state.downloads.transfers[self.id] = {
'added': datetime.utcnow(),
'progress': 0
}
logger.debug('queue %s for download', self.id)
if not u in self.users:
if u not in self.users:
self.add_user(u)
def save_file(self, content):
@ -643,7 +643,7 @@ for key in config['itemKeys']:
Item.sort_keys.append(key['id'])
Item.id_keys = ['isbn', 'lccn', 'olid', 'oclc', 'asin']
Item.item_keys = config['itemKeys']
Item.item_keys = config['itemKeys']
Item.filter_keys = [k['id'] for k in config['itemKeys'] if k.get('filter')]
Item.array_keys = [k['id'] for k in config['itemKeys'] if isinstance(k['type'], list)]
@ -730,7 +730,7 @@ class File(db.Model):
def move(self):
def format_underscores(string):
return re.sub('^\.|\.$|:|/|\?|<|>', '_', string)
return re.sub(r'^\.|\.$|:|/|\?|<|>', '_', string)
prefs = settings.preferences
prefix = os.sep.join(os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/').split('/'))
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_')]
for col in set(Item.sort_keys)-set(sort_indexes):
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')
col = 'sortvalue'
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):
delay = 60
if state.online:
#logger.debug('get_cover(%s)', id)
# logger.debug('get_cover(%s)', id)
with db.session():
i = Item.get(id)
if i:
@ -859,7 +859,7 @@ def get_cover(id):
def get_preview(id):
if state.online:
#logger.debug('get_preview(%s)', id)
# logger.debug('get_preview(%s)', id)
with db.session():
i = Item.get(id)
if i:
@ -869,7 +869,7 @@ def get_preview(id):
time.sleep(0.5)
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
delay = 10
with db.session():
@ -893,5 +893,5 @@ def sync_metadata(ids=None):
if later:
if state.main and state.tasks:
state.main.call_later(delay, lambda: state.tasks.queue('syncmetadata', later))
#else:
# else:
# logger.debug('sync_metadata done')

View file

@ -1,6 +1,6 @@
# encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4
DEBUG=False
DEBUG = False
try:
from gi.repository import Gtk, GObject
GObject.threads_init()
@ -13,10 +13,10 @@ except:
class GtkUI:
def selectFolder(self, data):
dialog = Gtk.FileChooserDialog(data.get("title", "Select Folder"),
None,
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
None,
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
dialog.set_default_response(Gtk.ResponseType.OK)
response = dialog.run()
@ -37,10 +37,10 @@ class GtkUI:
def selectFile(self, data):
dialog = Gtk.FileChooserDialog(data.get("title", "Select File"),
None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
dialog.set_default_response(Gtk.ResponseType.OK)
response = dialog.run()
@ -62,7 +62,8 @@ class GtkUI:
class TkUI:
def __init__(self):
self.root = Tk()
self.root.withdraw() #hiding tkinter window
self.root.withdraw() # hiding tkinter window
def selectFolder(self, data):
return tkinter.filedialog.askdirectory(title=data.get("title", "Select Folder"))

View file

@ -33,7 +33,7 @@ class User(db.Model):
nickname = sa.Column(sa.String(256), unique=True)
pending = sa.Column(sa.String(64)) # sent|received
pending = sa.Column(sa.String(64)) # sent|received
queued = sa.Column(sa.Boolean())
peered = sa.Column(sa.Boolean())
online = sa.Column(sa.Boolean())
@ -160,7 +160,7 @@ class User(db.Model):
self.save()
if not was_peering:
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([
u.info.get('index', -1) for u in User.query.filter_by(peered=True)
] + [0]) + 1
@ -204,7 +204,7 @@ class User(db.Model):
name = self.info.get('nickname') or self.info.get('username') or 'anonymous'
nickname = name
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)
n += 1
self.nickname = nickname
@ -237,9 +237,8 @@ class User(db.Model):
state.db.session.commit()
list_items = sa.Table('listitem', db.metadata,
sa.Column('list_id', sa.Integer(), sa.ForeignKey('list.id')),
sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id'))
)
sa.Column('list_id', sa.Integer(), sa.ForeignKey('list.id')),
sa.Column('item_id', sa.String(32), sa.ForeignKey('item.id')))
class List(db.Model):
__tablename__ = 'list'
@ -255,7 +254,7 @@ class List(db.Model):
user = sa.orm.relationship('User', backref=sa.orm.backref('lists', lazy='dynamic'))
items = sa.orm.relationship('Item', secondary=list_items,
backref=sa.orm.backref('lists', lazy='dynamic'))
backref=sa.orm.backref('lists', lazy='dynamic'))
@classmethod
def get(cls, user_id, name=None):
@ -305,13 +304,13 @@ class List(db.Model):
@classmethod
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):
changed = False
for c in conditions:
if 'conditions' in c:
changed = update_conditions(c['conditions'] )
changed = update_conditions(c['conditions'])
else:
if c.get('key') == 'list' and c.get('value', '').startswith('%s:' % old):
c['value'] = '%s:%s' % new, c['value'].split(':', 1)[1]