simple changelog
This commit is contained in:
parent
04f994d4b7
commit
e966256fa2
15 changed files with 267 additions and 103 deletions
|
|
@ -1,7 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
|
||||
import os
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
|
|
@ -17,6 +17,49 @@ import state
|
|||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def changelog_path():
|
||||
return os.path.join(settings.data_path, 'peers', '%s.log' % settings.USER_ID)
|
||||
|
||||
def next_revision():
|
||||
settings.server['revision'] = settings.server.get('revision', -1) + 1
|
||||
return settings.server['revision']
|
||||
|
||||
def add_record(action, *args, **kwargs):
|
||||
if '_ts' in kwargs:
|
||||
timestamp = kwargs['_ts']
|
||||
del kwargs['_ts']
|
||||
else:
|
||||
timestamp = None
|
||||
if not timestamp:
|
||||
timestamp = datetime.utcnow()
|
||||
timestamp = datetime2ts(timestamp)
|
||||
revision = next_revision()
|
||||
|
||||
data = [revision, timestamp, [action] + list(args)]
|
||||
data = json.dumps(data, ensure_ascii=False)
|
||||
|
||||
path = changelog_path()
|
||||
if os.path.exists(path):
|
||||
mode = 'a'
|
||||
state.changelog_size = os.path.getsize(path)
|
||||
else:
|
||||
mode = 'w'
|
||||
state.changelog_size = 0
|
||||
with open(path, mode) as fd:
|
||||
fd.write(data + '\n')
|
||||
state.changelog_size = os.path.getsize(path)
|
||||
#logger.debug('record change: %s', data)
|
||||
|
||||
def changelog_size():
|
||||
if state.changelog_size is None:
|
||||
path = changelog_path()
|
||||
if not os.path.exists(path):
|
||||
return 0
|
||||
return os.path.getsize(path)
|
||||
else:
|
||||
return state.changelog_size
|
||||
|
||||
class Changelog(db.Model):
|
||||
'''
|
||||
additem itemid metadata from file (info) + OLID
|
||||
|
|
@ -47,29 +90,7 @@ class Changelog(db.Model):
|
|||
|
||||
@classmethod
|
||||
def record(cls, user, action, *args, **kwargs):
|
||||
commit = True
|
||||
if '_commit' in kwargs:
|
||||
commit = kwargs['_commit']
|
||||
del kwargs['_commit']
|
||||
if '_ts' in kwargs:
|
||||
timestamp = kwargs['_ts']
|
||||
del kwargs['_ts']
|
||||
else:
|
||||
timestamp = None
|
||||
c = cls()
|
||||
c.created = datetime.utcnow()
|
||||
if not timestamp:
|
||||
timestamp = c.created
|
||||
c.timestamp = datetime2ts(timestamp)
|
||||
c.user_id = user.id
|
||||
c.revision = cls.query.filter_by(user_id=user.id).count()
|
||||
c.data = json.dumps([action] + list(args), ensure_ascii=False)
|
||||
_data = str(c.revision) + str(c.timestamp) + c.data
|
||||
_data = _data.encode()
|
||||
state.db.session.add(c)
|
||||
if commit:
|
||||
state.db.session.commit()
|
||||
logger.debug('record change: %s', c.json())
|
||||
return add_record(action, *args, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def apply_changes(cls, user_, changes, first=False):
|
||||
|
|
@ -162,7 +183,6 @@ class Changelog(db.Model):
|
|||
timestamp = self.timestamp or datetime2ts(self.created)
|
||||
return [self.revision, timestamp, json.loads(self.data)]
|
||||
|
||||
|
||||
def action_additem(self, user, timestamp, itemid, info):
|
||||
from item.models import Item
|
||||
i = Item.get(itemid)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue