##// END OF EJS Templates
Sync fixes
Sync fixes

File last commit:

r1386:1a1d9a43 decentral
r1386:1a1d9a43 decentral
Show More
sync_with_server.py
79 lines | 2.7 KiB | text/x-python | PythonLexer
neko259
Added test for reflinks. Added management command to get posts from other node...
r841 import re
neko259
Some progress in the sync
r1144 import xml.etree.ElementTree as ET
neko259
Added PULL protocol method implementation without any filters
r1321 import httplib2
neko259
Added test for reflinks. Added management command to get posts from other node...
r841 from django.core.management import BaseCommand
neko259
Added PULL protocol method implementation without any filters
r1321
neko259
Added test for reflinks. Added management command to get posts from other node...
r841 from boards.models import GlobalId
neko259
Updated sync method for requesting and getting a post
r1177 from boards.models.post.sync import SyncManager
neko259
Added test for reflinks. Added management command to get posts from other node...
r841
__author__ = 'neko259'
neko259
Small progress in getting a sync data from server
r1138 REGEX_GLOBAL_ID = re.compile(r'(\w+)::([\w\+/]+)::(\d+)')
neko259
Added test for reflinks. Added management command to get posts from other node...
r841
class Command(BaseCommand):
neko259
Added PULL protocol method implementation without any filters
r1321 help = 'Send a sync or get request to the server.'
neko259
Added test for reflinks. Added management command to get posts from other node...
r841
neko259
Small progress in getting a sync data from server
r1138 def add_arguments(self, parser):
neko259
Sync fixes
r1386 parser.add_argument('url', type=str, help='Server root url')
parser.add_argument('--global-id', type=str, default='',
neko259
Added PULL protocol method implementation without any filters
r1321 help='Post global ID')
neko259
Small progress in getting a sync data from server
r1138
neko259
Added test for reflinks. Added management command to get posts from other node...
r841 def handle(self, *args, **options):
neko259
Small progress in getting a sync data from server
r1138 url = options.get('url')
neko259
Sync fixes
r1386
pull_url = url + 'api/sync/pull/'
get_url = url + 'api/sync/get/'
neko259
Small progress in getting a sync data from server
r1138 global_id_str = options.get('global_id')
if global_id_str:
match = REGEX_GLOBAL_ID.match(global_id_str)
if match:
key_type = match.group(1)
key = match.group(2)
local_id = match.group(3)
neko259
Added test for reflinks. Added management command to get posts from other node...
r841
neko259
Small progress in getting a sync data from server
r1138 global_id = GlobalId(key_type=key_type, key=key,
neko259
Added test for reflinks. Added management command to get posts from other node...
r841 local_id=local_id)
neko259
Small progress in getting a sync data from server
r1138 xml = GlobalId.objects.generate_request_get([global_id])
neko259
Updated sync method for requesting and getting a post
r1177 # body = urllib.parse.urlencode(data)
neko259
Small progress in getting a sync data from server
r1138 h = httplib2.Http()
neko259
Sync fixes
r1386 response, content = h.request(get_url, method="POST", body=xml)
neko259
Added test for reflinks. Added management command to get posts from other node...
r841
neko259
Made SyncManager's methods static
r1236 SyncManager.parse_response_get(content)
neko259
Small progress in getting a sync data from server
r1138 else:
raise Exception('Invalid global ID')
neko259
Added test for reflinks. Added management command to get posts from other node...
r841 else:
neko259
Some progress in the sync
r1144 h = httplib2.Http()
neko259
Added PULL protocol method implementation without any filters
r1321 xml = GlobalId.objects.generate_request_pull()
neko259
Sync fixes
r1386 response, content = h.request(pull_url, method="POST", body=xml)
neko259
Some progress in the sync
r1144
neko259
Sync fixes
r1386 print(content.decode() + '\n')
neko259
Some progress in the sync
r1144
root = ET.fromstring(content)
status = root.findall('status')[0].text
if status == 'success':
neko259
Get the posts that do not exist on this server yet
r1322 ids_to_sync = list()
neko259
Some progress in the sync
r1144 models = root.findall('models')[0]
for model in models:
neko259
Get the posts that do not exist on this server yet
r1322 global_id, exists = GlobalId.from_xml_element(model)
if not exists:
neko259
Sync fixes
r1386 print(global_id)
neko259
Get the posts that do not exist on this server yet
r1322 ids_to_sync.append(global_id)
neko259
Sync fixes
r1386 print()
neko259
Get the posts that do not exist on this server yet
r1322
if len(ids_to_sync) > 0:
xml = GlobalId.objects.generate_request_get(ids_to_sync)
# body = urllib.parse.urlencode(data)
h = httplib2.Http()
neko259
Sync fixes
r1386 response, content = h.request(get_url, method="POST", body=xml)
neko259
Get the posts that do not exist on this server yet
r1322
SyncManager.parse_response_get(content)
else:
print('Nothing to get, everything synced')
neko259
Some progress in the sync
r1144 else:
raise Exception('Invalid response status')