##// END OF EJS Templates
Fixed import of truncatewords
Fixed import of truncatewords

File last commit:

r1322:71a3c79d decentral
r1385:a552aeec decentral
Show More
sync_with_server.py
74 lines | 2.5 KiB | text/x-python | PythonLexer
import re
import xml.etree.ElementTree as ET
import httplib2
from django.core.management import BaseCommand
from boards.models import GlobalId
from boards.models.post.sync import SyncManager
__author__ = 'neko259'
REGEX_GLOBAL_ID = re.compile(r'(\w+)::([\w\+/]+)::(\d+)')
class Command(BaseCommand):
help = 'Send a sync or get request to the server.'
def add_arguments(self, parser):
parser.add_argument('url', type=str)
parser.add_argument('--global_id', type=str, default='',
help='Post global ID')
def handle(self, *args, **options):
url = options.get('url')
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)
global_id = GlobalId(key_type=key_type, key=key,
local_id=local_id)
xml = GlobalId.objects.generate_request_get([global_id])
# body = urllib.parse.urlencode(data)
h = httplib2.Http()
response, content = h.request(url, method="POST", body=xml)
SyncManager.parse_response_get(content)
else:
raise Exception('Invalid global ID')
else:
h = httplib2.Http()
xml = GlobalId.objects.generate_request_pull()
response, content = h.request(url, method="POST", body=xml)
print(content)
root = ET.fromstring(content)
status = root.findall('status')[0].text
if status == 'success':
ids_to_sync = list()
models = root.findall('models')[0]
for model in models:
global_id, exists = GlobalId.from_xml_element(model)
print(global_id)
if not exists:
ids_to_sync.append(global_id)
if len(ids_to_sync) > 0:
xml = GlobalId.objects.generate_request_get(ids_to_sync)
# body = urllib.parse.urlencode(data)
h = httplib2.Http()
response, content = h.request(url, method="POST", body=xml)
SyncManager.parse_response_get(content)
else:
print('Nothing to get, everything synced')
else:
raise Exception('Invalid response status')