openmedialibrary/oml/changelog.py

291 lines
9.6 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-02 22:32:44 +00:00
2014-05-04 17:26:43 +00:00
from datetime import datetime
2014-08-12 08:16:57 +00:00
import json
2014-05-04 17:26:43 +00:00
2014-08-12 08:16:57 +00:00
import sqlalchemy as sa
2014-05-04 17:26:43 +00:00
from utils import datetime2ts, ts2datetime
2014-08-12 08:16:57 +00:00
from websocket import trigger_event
2014-08-09 15:03:16 +00:00
import db
2014-08-12 08:16:57 +00:00
import settings
2014-05-04 17:26:43 +00:00
import state
2014-08-12 08:16:57 +00:00
import logging
2015-11-29 14:56:25 +00:00
logger = logging.getLogger(__name__)
2014-05-17 14:26:59 +00:00
2014-05-04 17:26:43 +00:00
class Changelog(db.Model):
'''
additem itemid metadata from file (info) + OLID
edititem itemid name->id (i.e. olid-> OL...M)
removeitem itemid
addlist name
editlist name {name: newname}
orderlists [name, name, name]
removelist name
2014-05-12 12:57:47 +00:00
addlistitems listname [ids]
removelistitems listname [ids]
2014-05-04 17:26:43 +00:00
editusername username
editcontact string
addpeer peerid peername
removepeer peerid peername
2014-05-21 00:02:21 +00:00
editmeta key, value data (i.e. 'isbn', '0000000000', {title: 'Example'})
resetmeta key, value
2014-05-04 17:26:43 +00:00
'''
2014-08-09 15:03:16 +00:00
__tablename__ = 'changelog'
id = sa.Column(sa.Integer(), primary_key=True)
2014-05-04 17:26:43 +00:00
2014-08-09 15:03:16 +00:00
created = sa.Column(sa.DateTime())
timestamp = sa.Column(sa.BigInteger())
2014-05-04 17:26:43 +00:00
2014-08-09 15:03:16 +00:00
user_id = sa.Column(sa.String(43))
revision = sa.Column(sa.BigInteger())
data = sa.Column(sa.Text())
sig = sa.Column(sa.String(96))
2014-05-04 17:26:43 +00:00
@classmethod
def record(cls, user, action, *args):
c = cls()
2014-05-20 00:43:54 +00:00
c.created = datetime.utcnow()
2014-05-20 17:03:31 +00:00
c.timestamp = datetime2ts(c.created)
2014-05-04 17:26:43 +00:00
c.user_id = user.id
c.revision = cls.query.filter_by(user_id=user.id).count()
2014-10-31 11:45:54 +00:00
c.data = json.dumps([action] + list(args), ensure_ascii=False)
2014-05-20 00:43:54 +00:00
_data = str(c.revision) + str(c.timestamp) + c.data
2014-09-09 10:08:04 +00:00
_data = _data.encode()
2014-08-09 16:14:14 +00:00
state.db.session.add(c)
state.db.session.commit()
2015-11-30 23:26:35 +00:00
#if state.nodes:
# state.nodes.queue('peered', 'pushChanges', [c.json()])
2014-05-04 17:26:43 +00:00
2014-05-16 14:30:16 +00:00
@classmethod
def apply_changes(cls, user, changes):
trigger = changes
2014-05-16 14:30:16 +00:00
for change in changes:
if not cls.apply_change(user, change, trigger=False):
logger.debug('FAIL %s', change)
trigger = False
break
return False
if trigger:
2014-05-17 00:14:15 +00:00
trigger_event('change', {});
2014-05-16 14:30:16 +00:00
return True
2014-05-04 17:26:43 +00:00
@classmethod
def apply_change(cls, user, change, trigger=True):
revision, timestamp, data = change
last = cls.query.filter_by(user_id=user.id).order_by('-revision').first()
2014-05-04 17:26:43 +00:00
next_revision = last.revision + 1 if last else 0
2015-12-01 14:15:18 +00:00
if revision >= next_revision:
c = cls()
c.created = datetime.utcnow()
c.timestamp = timestamp
c.user_id = user.id
c.revision = revision
c.data = data
args = json.loads(data)
logger.debug('apply change from %s: %s', user.name, args)
if getattr(c, 'action_' + args[0])(user, timestamp, *args[1:]):
logger.debug('change applied')
state.db.session.add(c)
state.db.session.commit()
if trigger:
trigger_event('change', {});
return True
2014-05-04 17:26:43 +00:00
else:
2014-05-17 14:26:59 +00:00
logger.debug('revsion does not match! got %s expecting %s', revision, next_revision)
2014-05-04 17:26:43 +00:00
return False
def __repr__(self):
return self.data
def json(self):
2014-05-20 17:03:31 +00:00
timestamp = self.timestamp or datetime2ts(self.created)
return [self.revision, timestamp, self.data]
2014-05-04 17:26:43 +00:00
@classmethod
def restore(cls, user_id, path=None):
from user.models import User
user = User.get_or_create(user_id)
if not path:
path = '/tmp/oml_changelog_%s.json' % user_id
with open(path, 'r') as fd:
for change in fd:
change = json.loads(change)
cls.apply_change(user, change, user_id == settings.USER_ID)
@classmethod
def export(cls, user_id, path=None):
if not path:
path = '/tmp/oml_changelog_%s.json' % user_id
with open(path, 'w') as fd:
for c in cls.query.filter_by(user_id=user_id).order_by('revision'):
2014-10-31 11:45:54 +00:00
fd.write(json.dumps(c.json(), ensure_ascii=False) + '\n')
2014-05-04 17:26:43 +00:00
def action_additem(self, user, timestamp, itemid, info):
from item.models import Item
i = Item.get(itemid)
if i:
if user not in i.users:
i.add_user(user)
i.update()
else:
2014-05-04 17:26:43 +00:00
i = Item.get_or_create(itemid, info)
2014-05-20 17:03:31 +00:00
i.modified = ts2datetime(timestamp)
if user not in i.users:
i.add_user(user)
i.update()
user.clear_smart_list_cache()
2014-05-04 17:26:43 +00:00
return True
def action_edititem(self, user, timestamp, itemid, meta):
from item.models import Item
i = Item.get(itemid)
2014-05-26 08:23:10 +00:00
if not i:
logger.debug('ignore edititem for unknown item %s %s', timestamp, itemid)
return True
2014-05-04 17:26:43 +00:00
if i.timestamp > timestamp:
2014-05-20 17:03:31 +00:00
logger.debug('ignore edititem change %s %s %s', timestamp, itemid, meta)
2014-05-04 17:26:43 +00:00
return True
primary = None
2016-01-05 16:16:50 +00:00
if 'primaryid' in meta:
primary = meta['primaryid']
key = primary[0]
2016-01-05 16:16:50 +00:00
else:
keys = [k for k in meta if k in Item.id_keys]
if keys:
key = keys[0]
primary = [key, meta[key]]
if primary:
2014-05-21 00:02:21 +00:00
if not meta[key] and i.meta.get('primaryid', [''])[0] == key:
logger.debug('remove id mapping %s %s', i.id, primary)
2015-12-02 15:30:37 +00:00
i.update_primaryid(*primary, scrape=False)
2014-05-25 20:08:44 +00:00
i.modified = ts2datetime(timestamp)
2014-05-21 00:02:21 +00:00
elif meta[key] and i.meta.get('primaryid') != primary:
logger.debug('edit mapping %s %s', i.id, primary)
2015-12-02 15:30:37 +00:00
i.update_primaryid(*primary, scrape=False)
2014-05-25 20:08:44 +00:00
i.modified = ts2datetime(timestamp)
2014-05-19 20:58:00 +00:00
else:
i.update_meta(meta)
2014-05-25 20:08:44 +00:00
i.modified = ts2datetime(timestamp)
i.save()
user.clear_smart_list_cache()
2014-05-04 17:26:43 +00:00
return True
def action_removeitem(self, user, timestamp, itemid):
from item.models import Item
i = Item.get(itemid)
if i:
if user in i.users:
i.users.remove(user)
if i.users:
i.update()
else:
i.delete()
user.clear_list_cache()
user.clear_smart_list_cache()
2014-05-04 17:26:43 +00:00
return True
def action_addlist(self, user, timestamp, name, query=None):
from user.models import List
l = List.create(user.id, name)
return True
def action_editlist(self, user, timestamp, name, new):
from user.models import List
l = List.get_or_create(user.id, name)
if 'name' in new:
l.name = new['name']
l.save()
user.clear_list_cache()
2014-05-04 17:26:43 +00:00
return True
def action_orderlists(self, user, timestamp, lists):
from user.models import List
2014-05-25 18:06:12 +00:00
idx = 0
2014-05-04 17:26:43 +00:00
for name in lists:
l = List.get_or_create(user.id, name)
2014-05-25 18:06:12 +00:00
l.index_ = idx
2014-05-04 17:26:43 +00:00
l.save()
2014-05-25 18:06:12 +00:00
idx += 1
2014-05-04 17:26:43 +00:00
return True
def action_removelist(self, user, timestamp, name):
from user.models import List
l = List.get(user.id, name)
if l:
l.remove()
user.clear_list_cache()
2014-05-04 17:26:43 +00:00
return True
2014-05-12 12:57:47 +00:00
def action_addlistitems(self, user, timestamp, name, ids):
2014-05-04 17:26:43 +00:00
from user.models import List
2014-05-12 12:57:47 +00:00
l = List.get_or_create(user.id, name)
l.add_items(ids)
2014-05-04 17:26:43 +00:00
return True
2014-05-12 23:43:27 +00:00
def action_removelistitems(self, user, timestamp, name, ids):
2014-05-04 17:26:43 +00:00
from user.models import List
l = List.get(user.id, name)
2014-05-12 12:57:47 +00:00
if l:
l.remove_items(ids)
2014-05-04 17:26:43 +00:00
return True
def action_editusername(self, user, timestamp, username):
2014-05-25 18:06:12 +00:00
from user.models import List
old = user.nickname
2014-05-04 17:26:43 +00:00
user.info['username'] = username
2014-05-25 12:16:04 +00:00
user.update_name()
2014-05-25 18:06:12 +00:00
if old != user.nickname:
List.rename_user(old, user.nickname)
2014-05-04 17:26:43 +00:00
user.save()
return True
def action_editcontact(self, user, timestamp, contact):
user.info['contact'] = contact
user.save()
return True
2014-05-12 23:43:27 +00:00
def action_addpeer(self, user, timestamp, peerid, username):
2015-11-29 14:56:25 +00:00
if len(peerid) == 16:
from user.models import User
if not 'users' in user.info:
user.info['users'] = {}
user.info['users'][peerid] = username
user.save()
peer = User.get_or_create(peerid)
if not 'username' in peer.info:
peer.info['username'] = username
peer.update_name()
peer.save()
2014-05-04 17:26:43 +00:00
return True
2014-05-12 23:43:27 +00:00
def action_removepeer(self, user, timestamp, peerid):
2014-05-04 17:26:43 +00:00
if 'users' in user.info and peerid in user.info['users']:
del user.info['users'][peerid]
user.save()
#fixme, remove from User table if no other connection exists
return True
2014-05-21 00:02:21 +00:00
def action_editmeta(self, user, timestamp, key, value, data):
from item.models import Metadata
m = Metadata.get(key, value)
if not m or m.timestamp < timestamp:
if not m:
m = Metadata.get_or_create(key, value)
if m.edit(data):
m.update_items()
user.clear_smart_list_cache()
2014-05-21 00:02:21 +00:00
return True
def action_resetmeta(self, user, timestamp, key, value):
from item.models import Metadata
m = Metadata.get(key, value)
if m and m.timestamp < timestamp:
m.reset()
user.clear_smart_list_cache()
2014-05-21 00:02:21 +00:00
return True