# HG changeset patch # User neko259 # Date 2015-05-03 16:20:34 # Node ID 7246d9ec3073195cb9ef791fbebf36f06d112f65 # Parent 5c9f15aaa77c41ff08b47778272cc59e0ad033ba Small progress in getting a sync data from server diff --git a/boards/management/commands/sync_with_server.py b/boards/management/commands/sync_with_server.py --- a/boards/management/commands/sync_with_server.py +++ b/boards/management/commands/sync_with_server.py @@ -1,5 +1,5 @@ import re -import urllib +import urllib.parse import httplib2 from django.core.management import BaseCommand from boards.models import GlobalId @@ -7,31 +7,40 @@ from boards.models import GlobalId __author__ = 'neko259' -REGEX_GLOBAL_ID = r'\[(\w+)\]\[(\w+)\]\[(\d+)\]' +REGEX_GLOBAL_ID = re.compile(r'(\w+)::([\w\+/]+)::(\d+)') class Command(BaseCommand): help = 'Send a sync or get request to the server.' + \ 'sync_with_server [post_global_id]' + def add_arguments(self, parser): + parser.add_argument('url', type=str) + parser.add_argument('global_id', type=str) + def handle(self, *args, **options): - url = args[0] - if len(args) > 1: - global_id_str = args[1] - match = re.match(REGEX_GLOBAL_ID, global_id_str) - key_type = match.group(1) - key = match.group(2) - local_id = match.group(3) + 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, + global_id = GlobalId(key_type=key_type, key=key, local_id=local_id) - xml = GlobalId.objects.generate_request_get([global_id]) - data = {'xml': xml} - body = urllib.urlencode(data) - h = httplib2.Http() - response, content = h.request(url, method="POST", body=body) + xml = GlobalId.objects.generate_request_get([global_id]) + data = {'xml': xml} + body = urllib.parse.urlencode(data) + h = httplib2.Http() + response, content = h.request(url, method="POST", body=body) - # TODO Parse content and get the model list + # TODO Parse content and get the model list + + print(content) + else: + raise Exception('Invalid global ID') else: raise Exception('Full sync is not supported yet.')