openmedialibrary/oml/node/nodeapi.py

113 lines
3.2 KiB
Python
Raw Normal View History

2014-05-17 00:14:15 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-02 22:32:44 +00:00
2014-05-17 00:14:15 +00:00
2014-05-04 17:26:43 +00:00
from changelog import Changelog
from user.models import User
from websocket import trigger_event
2014-08-12 08:16:57 +00:00
import settings
import state
2014-05-04 17:26:43 +00:00
2014-05-17 14:26:59 +00:00
import logging
2015-11-29 14:56:38 +00:00
logger = logging.getLogger(__name__)
2014-05-17 14:26:59 +00:00
2014-08-09 16:33:59 +00:00
def api_pullChanges(remote_id, user_id=None, from_=None, to=None):
2014-05-04 17:26:43 +00:00
if user_id and not from_ and not to:
from_ = user_id
user_id = None
if user_id and from_ and not to:
if isinstance(user_id, int):
to = from_
from_ = user_id
user_id = None
from_ = from_ or 0
if user_id:
return []
2016-01-13 09:58:06 +00:00
return Changelog.aggregated_changes(from_)
'''
2014-05-04 17:26:43 +00:00
if not user_id:
user_id = settings.USER_ID
qs = Changelog.query.filter_by(user_id=user_id)
if from_:
qs = qs.filter(Changelog.revision>=from_)
if to:
qs = qs.filter(Changelog.revision<to)
state.nodes.queue('add', remote_id)
return [c.json() for c in qs]
2016-01-13 09:58:06 +00:00
'''
2014-05-04 17:26:43 +00:00
2014-08-09 16:33:59 +00:00
def api_pushChanges(user_id, changes):
2015-11-30 23:26:35 +00:00
logger.debug('pushChanges no longer used, ignored')
return True
2014-05-04 17:26:43 +00:00
user = User.get(user_id)
2014-05-16 14:30:16 +00:00
if not Changelog.apply_changes(user, changes):
2014-05-17 14:26:59 +00:00
logger.debug('FAILED TO APPLY CHANGE')
2014-05-16 14:30:16 +00:00
state.nodes.queue(user_id, 'pullChanges')
return False
2014-05-04 17:26:43 +00:00
return True
2014-08-09 16:33:59 +00:00
def api_requestPeering(user_id, username, message):
2014-05-04 17:26:43 +00:00
user = User.get_or_create(user_id)
if not user.info:
user.info = {}
if not user.peered:
if user.pending == 'sent':
user.info['message'] = message
user.update_peering(True, username)
2014-05-25 21:19:56 +00:00
user.update_name()
2014-05-04 17:26:43 +00:00
else:
user.pending = 'received'
user.info['username'] = username
user.info['message'] = message
2014-05-25 21:19:56 +00:00
user.update_name()
2014-05-04 17:26:43 +00:00
user.save()
trigger_event('peering.request', user.json())
2014-05-04 17:26:43 +00:00
return True
return False
2014-08-09 16:33:59 +00:00
def api_acceptPeering(user_id, username, message):
2014-05-04 17:26:43 +00:00
user = User.get(user_id)
2014-05-17 14:26:59 +00:00
logger.debug('incoming acceptPeering event: pending: %s', user.pending)
2014-05-04 17:26:43 +00:00
if user and user.pending == 'sent':
if not user.info:
user.info = {}
user.info['username'] = username
user.info['message'] = message
2014-05-25 21:19:56 +00:00
user.update_name()
2014-05-04 17:26:43 +00:00
user.update_peering(True, username)
2014-05-18 03:01:24 +00:00
state.nodes.queue('add', user.id)
2015-12-01 08:59:52 +00:00
trigger_event('peering.accept', user.json())
return True
elif user and user.peered:
2014-05-04 17:26:43 +00:00
return True
return False
2014-08-09 16:33:59 +00:00
def api_rejectPeering(user_id, message):
2014-05-04 17:26:43 +00:00
user = User.get(user_id)
if user:
if not user.info:
user.info = {}
user.info['message'] = message
user.update_peering(False)
trigger_event('peering.reject', user.json())
2014-05-04 17:26:43 +00:00
return True
return False
2014-08-09 16:33:59 +00:00
def api_removePeering(user_id, message):
2014-05-04 17:26:43 +00:00
user = User.get(user_id)
if user:
user.info['message'] = message
2014-05-12 23:43:27 +00:00
user.update_peering(False)
trigger_event('peering.remove', user.json())
2014-05-12 23:43:27 +00:00
return True
return False
2014-08-09 16:33:59 +00:00
def api_cancelPeering(user_id, message):
2014-05-12 23:43:27 +00:00
user = User.get(user_id)
if user:
user.info['message'] = message
user.update_peering(False)
trigger_event('peering.cancel', user.json())
2014-05-04 17:26:43 +00:00
return True
return False