##// END OF EJS Templates
Fixed sync without specifying tags filter
neko259 -
r1845:b8a95ef7 default
parent child Browse files
Show More
@@ -1,104 +1,110 b''
1 1 import re
2 2 import logging
3 3 import xml.etree.ElementTree as ET
4 4
5 5 import httplib2
6 6 from django.core.management import BaseCommand
7 7
8 8 from boards.models import GlobalId
9 9 from boards.models.post.sync import SyncManager, TAG_ID, TAG_VERSION
10 10
11 11 __author__ = 'neko259'
12 12
13 13
14 14 REGEX_GLOBAL_ID = re.compile(r'(\w+)::([\w\+/]+)::(\d+)')
15 15
16 16
17 17 class Command(BaseCommand):
18 18 help = 'Send a sync or get request to the server.'
19 19
20 20 def add_arguments(self, parser):
21 21 parser.add_argument('url', type=str, help='Server root url')
22 22 parser.add_argument('--global-id', type=str, default='',
23 23 help='Post global ID')
24 24 parser.add_argument('--split-query', type=int, default=1,
25 25 help='Split GET query into separate by the given'
26 26 ' number of posts in one')
27 27 parser.add_argument('--thread', type=int,
28 28 help='Get posts of one specific thread')
29 29 parser.add_argument('--tags', type=str,
30 30 help='Get posts of the tags, comma-separated')
31 31
32 32 def handle(self, *args, **options):
33 33 logger = logging.getLogger('boards.sync')
34 34
35 35 url = options.get('url')
36 36
37 37 list_url = url + 'api/sync/list/'
38 38 get_url = url + 'api/sync/get/'
39 39 file_url = url[:-1]
40 40
41 41 global_id_str = options.get('global_id')
42 42 if global_id_str:
43 43 match = REGEX_GLOBAL_ID.match(global_id_str)
44 44 if match:
45 45 key_type = match.group(1)
46 46 key = match.group(2)
47 47 local_id = match.group(3)
48 48
49 49 global_id = GlobalId(key_type=key_type, key=key,
50 50 local_id=local_id)
51 51
52 52 xml = SyncManager.generate_request_get([global_id])
53 53 h = httplib2.Http()
54 54 response, content = h.request(get_url, method="POST", body=xml)
55 55
56 56 SyncManager.parse_response_get(content, file_url)
57 57 else:
58 58 raise Exception('Invalid global ID')
59 59 else:
60 60 logger.info('Running LIST request...')
61 61 h = httplib2.Http()
62
63 tags = []
64 tags_str = options.get('tags')
65 if tags_str:
66 tags = tags_str.split(',')
67
62 68 xml = SyncManager.generate_request_list(
63 opening_post=options.get('thread'), tags=options.get('tags').split(',')).encode()
69 opening_post=options.get('thread'), tags=tags).encode()
64 70 response, content = h.request(list_url, method="POST", body=xml)
65 71 if response.status != 200:
66 72 raise Exception('Server returned error {}'.format(response.status))
67 73
68 74 logger.info('Processing response...')
69 75
70 76 root = ET.fromstring(content)
71 77 status = root.findall('status')[0].text
72 78 if status == 'success':
73 79 ids_to_sync = list()
74 80
75 81 models = root.findall('models')[0]
76 82 for model in models:
77 83 tag_id = model.find(TAG_ID)
78 84 global_id, exists = GlobalId.from_xml_element(tag_id)
79 85 tag_version = model.find(TAG_VERSION)
80 86 if tag_version is not None:
81 87 version = int(tag_version.text) or 1
82 88 else:
83 89 version = 1
84 90 if not exists or global_id.post.version < version:
85 91 logger.debug('Processed (+) post {}'.format(global_id))
86 92 ids_to_sync.append(global_id)
87 93 else:
88 94 logger.debug('* Processed (-) post {}'.format(global_id))
89 95 logger.info('Starting sync...')
90 96
91 97 if len(ids_to_sync) > 0:
92 98 limit = options.get('split_query', len(ids_to_sync))
93 99 for offset in range(0, len(ids_to_sync), limit):
94 100 xml = SyncManager.generate_request_get(ids_to_sync[offset:offset + limit])
95 101 h = httplib2.Http()
96 102 logger.info('Running GET request...')
97 103 response, content = h.request(get_url, method="POST", body=xml)
98 104 logger.info('Processing response...')
99 105
100 106 SyncManager.parse_response_get(content, file_url)
101 107 else:
102 108 logger.info('Nothing to get, everything synced')
103 109 else:
104 110 raise Exception('Invalid response status')
General Comments 0
You need to be logged in to leave comments. Login now