updateElement

This commit is contained in:
j 2014-05-17 13:45:57 +02:00
commit 877e867d03
35 changed files with 516 additions and 314 deletions

View file

@ -84,7 +84,7 @@ class Changelog(db.Model):
c.data = data
c.sig = sig
action, args = json.loads(data)
print 'apply change', action
print 'apply change', action, args
if getattr(c, 'action_' + action)(user, timestamp, *args):
print 'change applied'
db.session.add(c)
@ -166,7 +166,8 @@ class Changelog(db.Model):
i = Item.get(itemid)
if not i or i.timestamp > timestamp:
return True
i.users.remove(user)
if user in i.users:
i.users.remove(user)
if i.users:
i.update()
else:

View file

@ -32,6 +32,8 @@ class UpdateStatic(Command):
oxjs = os.path.join(settings.static_path, 'oxjs')
if not os.path.exists(oxjs):
r('git', 'clone', 'https://git.0x2620.org/oxjs.git', oxjs)
elif os.path.exists(os.path.join(oxjs, '.git')):
os.system('cd "%s" && git pull' % oxjs)
r('python2', os.path.join(oxjs, 'tools', 'build', 'build.py'))
r('python2', os.path.join(settings.static_path, 'py', 'build.py'))

View file

@ -108,6 +108,15 @@ def edit(request):
return response
actions.register(edit, cache=False)
@returns_json
def remove(request):
data = json.loads(request.form['data']) if 'data' in request.form else {}
print 'remove files', data['ids']
if 'ids' in data and data['ids']:
for i in models.Item.query.filter(models.Item.id.in_(data['ids'])):
i.remove_file()
return {}
actions.register(remove, cache=False)
@returns_json
def findMetadata(request):

View file

@ -9,6 +9,7 @@ import json
import hashlib
from datetime import datetime
from StringIO import StringIO
import shutil
import Image
import ox
@ -170,9 +171,7 @@ class Item(db.Model):
def get_path(self):
f = self.files.first()
prefs = settings.preferences
prefix = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
return os.path.join(prefix, f.path) if f else None
return f.fullpath() if f else None
def update_sort(self):
for key in config['itemKeys']:
@ -366,6 +365,22 @@ class Item(db.Model):
self.update()
return False
def remove_file(self):
for f in self.files.all():
path = f.fullpath()
print path
if os.path.exists(path):
os.unlink(path)
db.session.delete(f)
user = state.user()
self.users.remove(user)
db.session.commit()
if not self.users:
db.session.delete(self)
else:
self.update()
Changelog.record(user, 'removeitem', self.id)
for key in config['itemKeys']:
if key.get('sort'):
sort_type = key.get('sortType', key['type'])
@ -445,3 +460,49 @@ class File(db.Model):
self.sha1 = sha1
self.created = datetime.now()
self.modified = datetime.now()
def fullpath(self):
prefs = settings.preferences
prefix = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
return os.path.join(prefix, self.path)
def move(self):
prefs = settings.preferences
prefix = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
j = self.item.json()
current_path = self.fullpath()
author = '; '.join([ox.canonical_name(a) for a in j.get('author', [])])
if not author:
author = 'Unknown Author'
title = j.get('title', 'Untitled')
extension = j['extension']
if len(title) > 100:
title = title[:100]
if author.endswith('.'):
author = author[:-1] + '_'
if author.startswith('.'):
author = '_' + author[1:]
filename = '%s.%s' % (title, extension)
print self.sha1, author, filename
new_path = os.path.join(author[0].upper(), author, filename)
if self.path == new_path:
return
h = ''
while os.path.exists(os.path.join(prefix, new_path)):
h = self.sha1[:len(h)+1]
filename = '%s.%s.%s' % (title, h, extension)
new_path = os.path.join(author[0].upper(), author, filename)
if current_path == os.path.join(prefix, new_path):
break
if self.path != new_path:
path = os.path.join(prefix, new_path)
ox.makedirs(os.path.dirname(path))
shutil.move(current_path, path)
self.path = new_path
self.save()
print 'move', current_path, new_path
def save(self):
db.session.add(self)
db.session.commit()

View file

@ -25,21 +25,14 @@ extensions = ['epub', 'pdf', 'txt']
def remove_missing():
dirty = False
with app.app_context():
user = User.get_or_create(settings.USER_ID)
prefs = settings.preferences
prefix = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
for f in File.query:
if not os.path.exists(f.item.get_path()):
path = f.item.get_path()
if not os.path.exists(path):
dirty = True
print 'file gone', f, f.item.get_path()
f.item.users.remove(user)
if not f.item.users:
print 'last user, remove'
db.session.delete(f.item)
else:
f.item.update_lists()
Changelog.record(user, 'removeitem', f.item.id)
db.session.delete(f)
print 'file gone', f, path
f.item.remove_file()
if dirty:
db.session.commit()
@ -171,6 +164,7 @@ def run_import(options=None):
item.meta['mainid']: item.meta[item.meta['mainid']]
})
item.scrape()
file.move()
if listname:
listitems.append(item.id)
added += 1

View file

@ -58,6 +58,7 @@ def api_requestPeering(app, user_id, username, message):
def api_acceptPeering(app, user_id, username, message):
user = User.get(user_id)
print 'incoming acceptPeering event: pending:', user.pending
if user and user.pending == 'sent':
if not user.info:
user.info = {}

View file

@ -187,7 +187,9 @@ class Node(object):
return True
def acceptPeering(self, message):
print 'run acceptPeering', message
r = self.request('acceptPeering', settings.preferences['username'], message)
print 'result', r
p = self.user
p.update_peering(True)
self.go_online()

View file

@ -192,6 +192,7 @@ def acceptPeering(request):
if len(data.get('id', '')) != 43:
print 'invalid user id'
return {}
print 'acceptPeering...', data
p = models.User.get_or_create(data['id'])
state.nodes.queue('add', p.id)
state.nodes.queue(p.id, 'acceptPeering', data.get('message', ''))

View file

@ -68,17 +68,23 @@ class User(db.Model):
was_peering = self.peered
if peered:
self.pending = ''
self.peered = True
if username:
self.info['username'] = username
self.set_nickname(self.info.get('username', 'anonymous'))
# FIXME: need to set peered to False to not trigger changelog event
# before other side receives acceptPeering request
self.peered = False
self.save()
if not was_peering:
Changelog.record(state.user(), 'addpeer', self.id, self.nickname)
self.peered = True
self.save()
else:
self.pending = ''
self.peered = False
self.nickname = None
self.save()
List.query.filter_by(user_id=self.id).delete()
for i in self.items:
i.users.remove(self)
@ -167,7 +173,8 @@ class List(db.Model):
for item_id in items:
i = Item.get(item_id)
self.items.append(i)
i.queue_download()
if self.user_id == settings.USER_ID:
i.queue_download()
i.update()
db.session.add(self)
db.session.commit()