changelog
This commit is contained in:
parent
cfa8b2686b
commit
0815ccd930
6 changed files with 32 additions and 33 deletions
|
@ -35,6 +35,7 @@ class Changelog(db.Model):
|
||||||
id = db.Column(db.Integer(), primary_key=True)
|
id = db.Column(db.Integer(), primary_key=True)
|
||||||
|
|
||||||
created = db.Column(db.DateTime())
|
created = db.Column(db.DateTime())
|
||||||
|
timestamp = db.Column(db.BigInteger())
|
||||||
|
|
||||||
user_id = db.Column(db.String(43))
|
user_id = db.Column(db.String(43))
|
||||||
revision = db.Column(db.BigInteger())
|
revision = db.Column(db.BigInteger())
|
||||||
|
@ -44,22 +45,18 @@ class Changelog(db.Model):
|
||||||
@classmethod
|
@classmethod
|
||||||
def record(cls, user, action, *args):
|
def record(cls, user, action, *args):
|
||||||
c = cls()
|
c = cls()
|
||||||
c.created = datetime.now()
|
c.created = datetime.utcnow()
|
||||||
|
c.timestamp = int(c.created.strftime('%s'))
|
||||||
c.user_id = user.id
|
c.user_id = user.id
|
||||||
c.revision = cls.query.filter_by(user_id=user.id).count()
|
c.revision = cls.query.filter_by(user_id=user.id).count()
|
||||||
c.data = json.dumps([action, args])
|
c.data = json.dumps([action] + list(args))
|
||||||
timestamp = c.timestamp
|
_data = str(c.revision) + str(c.timestamp) + c.data
|
||||||
_data = str(c.revision) + str(timestamp) + c.data
|
|
||||||
c.sig = settings.sk.sign(_data, encoding='base64')
|
c.sig = settings.sk.sign(_data, encoding='base64')
|
||||||
db.session.add(c)
|
db.session.add(c)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
if state.online:
|
if state.online:
|
||||||
state.nodes.queue('peered', 'pushChanges', [c.json()])
|
state.nodes.queue('peered', 'pushChanges', [c.json()])
|
||||||
|
|
||||||
@property
|
|
||||||
def timestamp(self):
|
|
||||||
return self.created.strftime('%s')
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def apply_changes(cls, user, changes):
|
def apply_changes(cls, user, changes):
|
||||||
for change in changes:
|
for change in changes:
|
||||||
|
@ -82,14 +79,15 @@ class Changelog(db.Model):
|
||||||
sig = settings.sk.sign(_data, encoding='base64')
|
sig = settings.sk.sign(_data, encoding='base64')
|
||||||
if valid(user.id, _data, sig):
|
if valid(user.id, _data, sig):
|
||||||
c = cls()
|
c = cls()
|
||||||
c.created = datetime.fromtimestamp(float(timestamp))
|
c.created = datetime.utcnow()
|
||||||
|
c.timestamp = timestamp
|
||||||
c.user_id = user.id
|
c.user_id = user.id
|
||||||
c.revision = revision
|
c.revision = revision
|
||||||
c.data = data
|
c.data = data
|
||||||
c.sig = sig
|
c.sig = sig
|
||||||
action, args = json.loads(data)
|
args = json.loads(data)
|
||||||
logger.debug('apply change %s %s', action, args)
|
logger.debug('apply change %s', args)
|
||||||
if getattr(c, 'action_' + action)(user, timestamp, *args):
|
if getattr(c, 'action_' + args[0])(user, timestamp, *args[1:]):
|
||||||
logger.debug('change applied')
|
logger.debug('change applied')
|
||||||
db.session.add(c)
|
db.session.add(c)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -119,7 +117,8 @@ class Changelog(db.Model):
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
def json(self):
|
def json(self):
|
||||||
return [self.revision, self.timestamp, self.sig, self.data]
|
timestamp = self.timestamp or self.created.strftime('%s')
|
||||||
|
return [self.revision, timestamp, self.sig, self.data]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def restore(cls, user_id, path=None):
|
def restore(cls, user_id, path=None):
|
||||||
|
@ -150,7 +149,7 @@ class Changelog(db.Model):
|
||||||
return True
|
return True
|
||||||
if not i:
|
if not i:
|
||||||
i = Item.get_or_create(itemid, info)
|
i = Item.get_or_create(itemid, info)
|
||||||
i.modified = datetime.fromtimestamp(float(timestamp))
|
i.modified = datetime.utcfromtimestamp(float(timestamp))
|
||||||
if user not in i.users:
|
if user not in i.users:
|
||||||
i.users.append(user)
|
i.users.append(user)
|
||||||
i.update()
|
i.update()
|
||||||
|
@ -172,7 +171,7 @@ class Changelog(db.Model):
|
||||||
i.update_mainid(key, meta[key])
|
i.update_mainid(key, meta[key])
|
||||||
else:
|
else:
|
||||||
i.update_meta(meta)
|
i.update_meta(meta)
|
||||||
i.modified = datetime.fromtimestamp(float(timestamp))
|
i.modified = datetime.utffromtimestamp(float(timestamp))
|
||||||
i.save()
|
i.save()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ class ReaderHandler(OMLHandler):
|
||||||
self.set_status(404)
|
self.set_status(404)
|
||||||
self.finish()
|
self.finish()
|
||||||
return
|
return
|
||||||
item.sort_accessed = item.accessed = datetime.now()
|
item.sort_accessed = item.accessed = datetime.utcnow()
|
||||||
item.sort_timesaccessed = item.timesaccessed = (item.timesaccessed or 0) + 1
|
item.sort_timesaccessed = item.timesaccessed = (item.timesaccessed or 0) + 1
|
||||||
item.save()
|
item.save()
|
||||||
return serve_static(self, os.path.join(settings.static_path, html), 'text/html')
|
return serve_static(self, os.path.join(settings.static_path, html), 'text/html')
|
||||||
|
|
|
@ -49,8 +49,8 @@ class Work(db.Model):
|
||||||
|
|
||||||
def __init__(self, id):
|
def __init__(self, id):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.created = datetime.now()
|
self.created = datetime.utcnow()
|
||||||
self.modified = datetime.now()
|
self.modified = datetime.utcnow()
|
||||||
|
|
||||||
class Edition(db.Model):
|
class Edition(db.Model):
|
||||||
|
|
||||||
|
@ -69,8 +69,8 @@ class Edition(db.Model):
|
||||||
|
|
||||||
def __init__(self, id):
|
def __init__(self, id):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.created = datetime.now()
|
self.created = datetime.utcnow()
|
||||||
self.modified = datetime.now()
|
self.modified = datetime.utcnow()
|
||||||
|
|
||||||
user_items = db.Table('useritem',
|
user_items = db.Table('useritem',
|
||||||
db.Column('user_id', db.String(43), db.ForeignKey('user.id')),
|
db.Column('user_id', db.String(43), db.ForeignKey('user.id')),
|
||||||
|
@ -114,8 +114,8 @@ class Item(db.Model):
|
||||||
if isinstance(id, list):
|
if isinstance(id, list):
|
||||||
id = base64.b32encode(hashlib.sha1(''.join(id)).digest())
|
id = base64.b32encode(hashlib.sha1(''.join(id)).digest())
|
||||||
self.id = id
|
self.id = id
|
||||||
self.created = datetime.now()
|
self.created = datetime.utcnow()
|
||||||
self.modified = datetime.now()
|
self.modified = datetime.utcnow()
|
||||||
self.info = {}
|
self.info = {}
|
||||||
self.meta = {}
|
self.meta = {}
|
||||||
|
|
||||||
|
@ -262,7 +262,7 @@ class Item(db.Model):
|
||||||
self.update_sort()
|
self.update_sort()
|
||||||
self.update_find()
|
self.update_find()
|
||||||
self.update_lists()
|
self.update_lists()
|
||||||
#self.modified = datetime.now()
|
#self.modified = datetime.utcnow()
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
@ -273,7 +273,7 @@ class Item(db.Model):
|
||||||
if data != self.meta:
|
if data != self.meta:
|
||||||
self.meta = data
|
self.meta = data
|
||||||
self.update()
|
self.update()
|
||||||
self.modified = datetime.now()
|
self.modified = datetime.utcnow()
|
||||||
self.save()
|
self.save()
|
||||||
user = state.user()
|
user = state.user()
|
||||||
if user in self.users:
|
if user in self.users:
|
||||||
|
@ -302,7 +302,7 @@ class Item(db.Model):
|
||||||
self.scrape()
|
self.scrape()
|
||||||
self.update()
|
self.update()
|
||||||
self.update_cover()
|
self.update_cover()
|
||||||
self.modified = datetime.now()
|
self.modified = datetime.utcnow()
|
||||||
self.save()
|
self.save()
|
||||||
user = state.user()
|
user = state.user()
|
||||||
if user in self.users:
|
if user in self.users:
|
||||||
|
@ -347,7 +347,7 @@ class Item(db.Model):
|
||||||
u = state.user()
|
u = state.user()
|
||||||
if not u in self.users:
|
if not u in self.users:
|
||||||
self.transferprogress = 0
|
self.transferprogress = 0
|
||||||
self.transferadded = datetime.now()
|
self.transferadded = datetime.utcnow()
|
||||||
self.users.append(u)
|
self.users.append(u)
|
||||||
|
|
||||||
def save_file(self, content):
|
def save_file(self, content):
|
||||||
|
@ -368,7 +368,7 @@ class Item(db.Model):
|
||||||
if u not in self.users:
|
if u not in self.users:
|
||||||
self.users.append(u)
|
self.users.append(u)
|
||||||
self.transferprogress = 1
|
self.transferprogress = 1
|
||||||
self.added = datetime.now()
|
self.added = datetime.utcnow()
|
||||||
Changelog.record(u, 'additem', self.id, self.info)
|
Changelog.record(u, 'additem', self.id, self.info)
|
||||||
self.update()
|
self.update()
|
||||||
f.move()
|
f.move()
|
||||||
|
@ -480,8 +480,8 @@ class File(db.Model):
|
||||||
|
|
||||||
def __init__(self, sha1):
|
def __init__(self, sha1):
|
||||||
self.sha1 = sha1
|
self.sha1 = sha1
|
||||||
self.created = datetime.now()
|
self.created = datetime.utcnow()
|
||||||
self.modified = datetime.now()
|
self.modified = datetime.utcnow()
|
||||||
|
|
||||||
def fullpath(self):
|
def fullpath(self):
|
||||||
prefs = settings.preferences
|
prefs = settings.preferences
|
||||||
|
|
|
@ -58,7 +58,7 @@ def add_file(id, f, prefix):
|
||||||
Changelog.record(user, 'edititem', item.id, {
|
Changelog.record(user, 'edititem', item.id, {
|
||||||
item.meta['mainid']: item.meta[item.meta['mainid']]
|
item.meta['mainid']: item.meta[item.meta['mainid']]
|
||||||
})
|
})
|
||||||
item.added = datetime.now()
|
item.added = datetime.utcnow()
|
||||||
item.scrape()
|
item.scrape()
|
||||||
item.update_cover()
|
item.update_cover()
|
||||||
item.save()
|
item.save()
|
||||||
|
|
|
@ -39,7 +39,7 @@ def reader(id, filename=''):
|
||||||
html = 'html/txt.html'
|
html = 'html/txt.html'
|
||||||
else:
|
else:
|
||||||
abort(404)
|
abort(404)
|
||||||
item.sort_accessed = item.accessed = datetime.now()
|
item.sort_accessed = item.accessed = datetime.utcnow()
|
||||||
item.sort_timesaccessed = item.timesaccessed = (item.timesaccessed or 0) + 1
|
item.sort_timesaccessed = item.timesaccessed = (item.timesaccessed or 0) + 1
|
||||||
item.save()
|
item.save()
|
||||||
return app.send_static_file(html)
|
return app.send_static_file(html)
|
||||||
|
|
|
@ -265,7 +265,7 @@ class Node(Thread):
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent': settings.USER_AGENT,
|
'User-Agent': settings.USER_AGENT,
|
||||||
}
|
}
|
||||||
t1 = datetime.now()
|
t1 = datetime.utcnow()
|
||||||
logger.debug('download %s', url)
|
logger.debug('download %s', url)
|
||||||
'''
|
'''
|
||||||
r = requests.get(url, headers=headers)
|
r = requests.get(url, headers=headers)
|
||||||
|
@ -290,7 +290,7 @@ class Node(Thread):
|
||||||
'''
|
'''
|
||||||
content = r.read()
|
content = r.read()
|
||||||
|
|
||||||
t2 = datetime.now()
|
t2 = datetime.utcnow()
|
||||||
duration = (t2-t1).total_seconds()
|
duration = (t2-t1).total_seconds()
|
||||||
if duration:
|
if duration:
|
||||||
self.download_speed = len(content) / duration
|
self.download_speed = len(content) / duration
|
||||||
|
|
Loading…
Reference in a new issue