Show More
@@ -1,90 +1,88 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, TAG_ID, TAG_VERSION |
|
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 | 23 | parser.add_argument('--split-query', type=int, |
|
24 | 24 | help='Split GET query into separate by the given' |
|
25 | 25 | ' number of posts in one') |
|
26 | 26 | |
|
27 | 27 | def handle(self, *args, **options): |
|
28 | 28 | url = options.get('url') |
|
29 | 29 | |
|
30 | 30 | list_url = url + 'api/sync/list/' |
|
31 | 31 | get_url = url + 'api/sync/get/' |
|
32 | 32 | file_url = url[:-1] |
|
33 | 33 | |
|
34 | 34 | global_id_str = options.get('global_id') |
|
35 | 35 | if global_id_str: |
|
36 | 36 | match = REGEX_GLOBAL_ID.match(global_id_str) |
|
37 | 37 | if match: |
|
38 | 38 | key_type = match.group(1) |
|
39 | 39 | key = match.group(2) |
|
40 | 40 | local_id = match.group(3) |
|
41 | 41 | |
|
42 | 42 | global_id = GlobalId(key_type=key_type, key=key, |
|
43 | 43 | local_id=local_id) |
|
44 | 44 | |
|
45 | 45 | xml = GlobalId.objects.generate_request_get([global_id]) |
|
46 | # body = urllib.parse.urlencode(data) | |
|
47 | 46 | h = httplib2.Http() |
|
48 | 47 | response, content = h.request(get_url, method="POST", body=xml) |
|
49 | 48 | |
|
50 | 49 | SyncManager.parse_response_get(content, file_url) |
|
51 | 50 | else: |
|
52 | 51 | raise Exception('Invalid global ID') |
|
53 | 52 | else: |
|
53 | print('Running LIST request') | |
|
54 | 54 | h = httplib2.Http() |
|
55 | 55 | xml = GlobalId.objects.generate_request_list() |
|
56 | 56 | response, content = h.request(list_url, method="POST", body=xml) |
|
57 | 57 | |
|
58 | print(content.decode() + '\n') | |
|
59 | ||
|
60 | 58 | root = ET.fromstring(content) |
|
61 | 59 | status = root.findall('status')[0].text |
|
62 | 60 | if status == 'success': |
|
63 | 61 | ids_to_sync = list() |
|
64 | 62 | |
|
65 | 63 | models = root.findall('models')[0] |
|
66 | 64 | for model in models: |
|
67 | 65 | tag_id = model.find(TAG_ID) |
|
68 | 66 | global_id, exists = GlobalId.from_xml_element(tag_id) |
|
69 | 67 | tag_version = model.find(TAG_VERSION) |
|
70 | 68 | if tag_version is not None: |
|
71 | 69 | version = int(tag_version.text) or 1 |
|
72 | 70 | else: |
|
73 | 71 | version = 1 |
|
74 | 72 | if not exists or global_id.post.version < version: |
|
75 | 73 | ids_to_sync.append(global_id) |
|
76 | 74 | print('Starting sync...') |
|
77 | 75 | |
|
78 | 76 | if len(ids_to_sync) > 0: |
|
79 | 77 | limit = options.get('split_query', len(ids_to_sync)) |
|
80 | 78 | for offset in range(0, len(ids_to_sync), limit): |
|
81 | 79 | xml = GlobalId.objects.generate_request_get(ids_to_sync[offset:offset+limit]) |
|
82 | # body = urllib.parse.urlencode(data) | |
|
83 | 80 | h = httplib2.Http() |
|
81 | print('Running GET request') | |
|
84 | 82 | response, content = h.request(get_url, method="POST", body=xml) |
|
85 | 83 | |
|
86 | 84 | SyncManager.parse_response_get(content, file_url) |
|
87 | 85 | else: |
|
88 | 86 | print('Nothing to get, everything synced') |
|
89 | 87 | else: |
|
90 | 88 | raise Exception('Invalid response status') |
General Comments 0
You need to be logged in to leave comments.
Login now