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