##// END OF EJS Templates
Added limit option to sync command
neko259 -
r1551:a596995d default
parent child Browse files
Show More
@@ -1,80 +1,85 b''
1 import re
1 import re
2 import xml.etree.ElementTree as ET
2 import xml.etree.ElementTree as ET
3
3
4 import httplib2
4 import httplib2
5 from django.core.management import BaseCommand
5 from django.core.management import BaseCommand
6
6
7 from boards.models import GlobalId
7 from boards.models import GlobalId
8 from boards.models.post.sync import SyncManager
8 from boards.models.post.sync import SyncManager
9
9
10 __author__ = 'neko259'
10 __author__ = 'neko259'
11
11
12
12
13 REGEX_GLOBAL_ID = re.compile(r'(\w+)::([\w\+/]+)::(\d+)')
13 REGEX_GLOBAL_ID = re.compile(r'(\w+)::([\w\+/]+)::(\d+)')
14
14
15
15
16 class Command(BaseCommand):
16 class Command(BaseCommand):
17 help = 'Send a sync or get request to the server.'
17 help = 'Send a sync or get request to the server.'
18
18
19 def add_arguments(self, parser):
19 def add_arguments(self, parser):
20 parser.add_argument('url', type=str, help='Server root url')
20 parser.add_argument('url', type=str, help='Server root url')
21 parser.add_argument('--global-id', type=str, default='',
21 parser.add_argument('--global-id', type=str, default='',
22 help='Post global ID')
22 help='Post global ID')
23 parser.add_argument('--split-query', type=int,
24 help='Split GET query into separate by the given'
25 ' number of posts in one')
23
26
24 def handle(self, *args, **options):
27 def handle(self, *args, **options):
25 url = options.get('url')
28 url = options.get('url')
26
29
27 pull_url = url + 'api/sync/pull/'
30 pull_url = url + 'api/sync/pull/'
28 get_url = url + 'api/sync/get/'
31 get_url = url + 'api/sync/get/'
29 file_url = url[:-1]
32 file_url = url[:-1]
30
33
31 global_id_str = options.get('global_id')
34 global_id_str = options.get('global_id')
32 if global_id_str:
35 if global_id_str:
33 match = REGEX_GLOBAL_ID.match(global_id_str)
36 match = REGEX_GLOBAL_ID.match(global_id_str)
34 if match:
37 if match:
35 key_type = match.group(1)
38 key_type = match.group(1)
36 key = match.group(2)
39 key = match.group(2)
37 local_id = match.group(3)
40 local_id = match.group(3)
38
41
39 global_id = GlobalId(key_type=key_type, key=key,
42 global_id = GlobalId(key_type=key_type, key=key,
40 local_id=local_id)
43 local_id=local_id)
41
44
42 xml = GlobalId.objects.generate_request_get([global_id])
45 xml = GlobalId.objects.generate_request_get([global_id])
43 # body = urllib.parse.urlencode(data)
46 # body = urllib.parse.urlencode(data)
44 h = httplib2.Http()
47 h = httplib2.Http()
45 response, content = h.request(get_url, method="POST", body=xml)
48 response, content = h.request(get_url, method="POST", body=xml)
46
49
47 SyncManager.parse_response_get(content, file_url)
50 SyncManager.parse_response_get(content, file_url)
48 else:
51 else:
49 raise Exception('Invalid global ID')
52 raise Exception('Invalid global ID')
50 else:
53 else:
51 h = httplib2.Http()
54 h = httplib2.Http()
52 xml = GlobalId.objects.generate_request_pull()
55 xml = GlobalId.objects.generate_request_pull()
53 response, content = h.request(pull_url, method="POST", body=xml)
56 response, content = h.request(pull_url, method="POST", body=xml)
54
57
55 print(content.decode() + '\n')
58 print(content.decode() + '\n')
56
59
57 root = ET.fromstring(content)
60 root = ET.fromstring(content)
58 status = root.findall('status')[0].text
61 status = root.findall('status')[0].text
59 if status == 'success':
62 if status == 'success':
60 ids_to_sync = list()
63 ids_to_sync = list()
61
64
62 models = root.findall('models')[0]
65 models = root.findall('models')[0]
63 for model in models:
66 for model in models:
64 global_id, exists = GlobalId.from_xml_element(model)
67 global_id, exists = GlobalId.from_xml_element(model)
65 if not exists:
68 if not exists:
66 print(global_id)
69 print(global_id)
67 ids_to_sync.append(global_id)
70 ids_to_sync.append(global_id)
68 print()
71 print()
69
72
70 if len(ids_to_sync) > 0:
73 if len(ids_to_sync) > 0:
71 xml = GlobalId.objects.generate_request_get(ids_to_sync)
74 limit = options.get('split_query', len(ids_to_sync))
72 # body = urllib.parse.urlencode(data)
75 for offset in range(0, len(ids_to_sync), limit):
73 h = httplib2.Http()
76 xml = GlobalId.objects.generate_request_get(ids_to_sync[offset:offset+limit0])
74 response, content = h.request(get_url, method="POST", body=xml)
77 # body = urllib.parse.urlencode(data)
78 h = httplib2.Http()
79 response, content = h.request(get_url, method="POST", body=xml)
75
80
76 SyncManager.parse_response_get(content, file_url)
81 SyncManager.parse_response_get(content, file_url)
77 else:
82 else:
78 print('Nothing to get, everything synced')
83 print('Nothing to get, everything synced')
79 else:
84 else:
80 raise Exception('Invalid response status')
85 raise Exception('Invalid response status')
General Comments 0
You need to be logged in to leave comments. Login now