add peering task

This commit is contained in:
j 2016-01-14 17:52:39 +05:30
parent c517922fb9
commit f6cfb255a6
2 changed files with 88 additions and 80 deletions

View file

@ -22,7 +22,7 @@ class Tasks(Thread):
def run(self):
import item.scan
from user.models import List
from user.models import export_list, update_user_peering
while self.connected:
m = self.q.get()
if m:
@ -33,9 +33,11 @@ class Tasks(Thread):
elif action == 'import':
item.scan.run_import(data)
elif action == 'export':
List.export(data)
export_list(data)
elif action == 'scan':
item.scan.run_scan()
elif action == 'peering':
update_user_peering(*data)
else:
trigger_event('error', {'error': 'unknown action'})
except:

View file

@ -331,10 +331,77 @@ class List(db.Model):
def create_symlinks(self):
pass
class Metadata(db.Model):
__tablename__ = 'user_metadata'
created = sa.Column(sa.DateTime())
modified = sa.Column(sa.DateTime())
id = sa.Column(sa.Integer(), primary_key=True)
item_id = sa.Column(sa.String(32))
user_id = sa.Column(sa.String(43), sa.ForeignKey('user.id'))
data_hash = sa.Column(sa.String(40), index=True)
data = sa.Column(MutableDict.as_mutable(sa.PickleType(pickler=json_pickler)))
def __repr__(self):
return '{item}/{user}'.format(item=self.item_id, user=self.user_id)
@property
def timestamp(self):
return utils.datetime2ts(self.modified)
@classmethod
def export(cls, data):
def get(cls, user_id, item_id):
return cls.query.filter_by(user_id=user_id,item_id=item_id).first()
@classmethod
def get_or_create(cls, user_id, item_id, data=None, commit=True):
m = cls.get(user_id=user_id, item_id=item_id)
if not m:
m = cls(user_id=user_id, item_id=item_id)
m.created = datetime.utcnow()
if data:
m.data = data
else:
m.data = {}
m.save(commit)
elif data:
m.edit(data, commit)
return m
def save(self, commit=True):
self.modified = datetime.utcnow()
self.data_hash = hashlib.sha1(json.dumps(self.data, ensure_ascii=False, sort_keys=True).encode()).hexdigest()
state.db.session.add(self)
if commit:
state.db.session.commit()
def edit(self, data, commit=True):
changes = {}
if 'isbn' in data and isinstance(data['isbn'], list):
isbns = [utils.to_isbn13(isbn) for isbn in data['isbn']]
isbns = [isbn for isbn in isbns if isbn]
if isbns:
data['isbn'] = isbns[0]
else:
del data['isbn']
for key in data:
if key == 'id':
continue
if data[key] != self.data.get(key):
self.data[key] = data[key]
changes[key] = data[key]
if changes:
self.save(commit)
return changes
def delete(self):
state.db.session.delete(self)
state.db.session.commit()
def export_list(data):
with db.session():
self = cls.get(data['list'])
self = List.get(data['list'])
if not self:
return
mode = data.get('mode')
@ -409,70 +476,9 @@ class List(db.Model):
'status': {'code': 200, 'text': ''},
})
class Metadata(db.Model):
__tablename__ = 'user_metadata'
def update_user_peering(user_id, peered, username=None):
with db.session():
u = User.get(user_id)
if u:
u.update_peering(peered, username)
created = sa.Column(sa.DateTime())
modified = sa.Column(sa.DateTime())
id = sa.Column(sa.Integer(), primary_key=True)
item_id = sa.Column(sa.String(32))
user_id = sa.Column(sa.String(43), sa.ForeignKey('user.id'))
data_hash = sa.Column(sa.String(40), index=True)
data = sa.Column(MutableDict.as_mutable(sa.PickleType(pickler=json_pickler)))
def __repr__(self):
return '{item}/{user}'.format(item=self.item_id, user=self.user_id)
@property
def timestamp(self):
return utils.datetime2ts(self.modified)
@classmethod
def get(cls, user_id, item_id):
return cls.query.filter_by(user_id=user_id,item_id=item_id).first()
@classmethod
def get_or_create(cls, user_id, item_id, data=None, commit=True):
m = cls.get(user_id=user_id, item_id=item_id)
if not m:
m = cls(user_id=user_id, item_id=item_id)
m.created = datetime.utcnow()
if data:
m.data = data
else:
m.data = {}
m.save(commit)
elif data:
m.edit(data, commit)
return m
def save(self, commit=True):
self.modified = datetime.utcnow()
self.data_hash = hashlib.sha1(json.dumps(self.data, ensure_ascii=False, sort_keys=True).encode()).hexdigest()
state.db.session.add(self)
if commit:
state.db.session.commit()
def edit(self, data, commit=True):
changes = {}
if 'isbn' in data and isinstance(data['isbn'], list):
isbns = [utils.to_isbn13(isbn) for isbn in data['isbn']]
isbns = [isbn for isbn in isbns if isbn]
if isbns:
data['isbn'] = isbns[0]
else:
del data['isbn']
for key in data:
if key == 'id':
continue
if data[key] != self.data.get(key):
self.data[key] = data[key]
changes[key] = data[key]
if changes:
self.save(commit)
return changes
def delete(self):
state.db.session.delete(self)
state.db.session.commit()