Show More
@@ -17,12 +17,16 b' 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) |
|
20 | parser.add_argument('url', type=str, help='Server root url') | |
21 |
parser.add_argument('--global |
|
21 | parser.add_argument('--global-id', type=str, default='', | |
22 | help='Post global ID') |
|
22 | help='Post global ID') | |
23 |
|
23 | |||
24 | def handle(self, *args, **options): |
|
24 | def handle(self, *args, **options): | |
25 | url = options.get('url') |
|
25 | url = options.get('url') | |
|
26 | ||||
|
27 | pull_url = url + 'api/sync/pull/' | |||
|
28 | get_url = url + 'api/sync/get/' | |||
|
29 | ||||
26 | global_id_str = options.get('global_id') |
|
30 | global_id_str = options.get('global_id') | |
27 | if global_id_str: |
|
31 | if global_id_str: | |
28 | match = REGEX_GLOBAL_ID.match(global_id_str) |
|
32 | match = REGEX_GLOBAL_ID.match(global_id_str) | |
@@ -37,7 +41,7 b' class Command(BaseCommand):' | |||||
37 | xml = GlobalId.objects.generate_request_get([global_id]) |
|
41 | xml = GlobalId.objects.generate_request_get([global_id]) | |
38 | # body = urllib.parse.urlencode(data) |
|
42 | # body = urllib.parse.urlencode(data) | |
39 | h = httplib2.Http() |
|
43 | h = httplib2.Http() | |
40 | response, content = h.request(url, method="POST", body=xml) |
|
44 | response, content = h.request(get_url, method="POST", body=xml) | |
41 |
|
45 | |||
42 | SyncManager.parse_response_get(content) |
|
46 | SyncManager.parse_response_get(content) | |
43 | else: |
|
47 | else: | |
@@ -45,9 +49,9 b' class Command(BaseCommand):' | |||||
45 | else: |
|
49 | else: | |
46 | h = httplib2.Http() |
|
50 | h = httplib2.Http() | |
47 | xml = GlobalId.objects.generate_request_pull() |
|
51 | xml = GlobalId.objects.generate_request_pull() | |
48 | response, content = h.request(url, method="POST", body=xml) |
|
52 | response, content = h.request(pull_url, method="POST", body=xml) | |
49 |
|
53 | |||
50 | print(content) |
|
54 | print(content.decode() + '\n') | |
51 |
|
55 | |||
52 | root = ET.fromstring(content) |
|
56 | root = ET.fromstring(content) | |
53 | status = root.findall('status')[0].text |
|
57 | status = root.findall('status')[0].text | |
@@ -57,15 +61,16 b' class Command(BaseCommand):' | |||||
57 | models = root.findall('models')[0] |
|
61 | models = root.findall('models')[0] | |
58 | for model in models: |
|
62 | for model in models: | |
59 | global_id, exists = GlobalId.from_xml_element(model) |
|
63 | global_id, exists = GlobalId.from_xml_element(model) | |
60 | print(global_id) |
|
|||
61 | if not exists: |
|
64 | if not exists: | |
|
65 | print(global_id) | |||
62 | ids_to_sync.append(global_id) |
|
66 | ids_to_sync.append(global_id) | |
|
67 | print() | |||
63 |
|
68 | |||
64 | if len(ids_to_sync) > 0: |
|
69 | if len(ids_to_sync) > 0: | |
65 | xml = GlobalId.objects.generate_request_get(ids_to_sync) |
|
70 | xml = GlobalId.objects.generate_request_get(ids_to_sync) | |
66 | # body = urllib.parse.urlencode(data) |
|
71 | # body = urllib.parse.urlencode(data) | |
67 | h = httplib2.Http() |
|
72 | h = httplib2.Http() | |
68 | response, content = h.request(url, method="POST", body=xml) |
|
73 | response, content = h.request(get_url, method="POST", body=xml) | |
69 |
|
74 | |||
70 | SyncManager.parse_response_get(content) |
|
75 | SyncManager.parse_response_get(content) | |
71 | else: |
|
76 | else: |
@@ -142,10 +142,11 b' class PostManager(models.Manager):' | |||||
142 | pub_time=pub_time, |
|
142 | pub_time=pub_time, | |
143 | poster_ip=NO_IP, |
|
143 | poster_ip=NO_IP, | |
144 | last_edit_time=pub_time, |
|
144 | last_edit_time=pub_time, | |
145 | thread_id=thread.id, |
|
|||
146 | global_id=global_id, |
|
145 | global_id=global_id, | |
147 |
opening=is_opening |
|
146 | opening=is_opening, | |
|
147 | thread=thread) | |||
148 |
|
148 | |||
|
149 | post.threads.add(thread) | |||
149 | post.build_url() |
|
150 | post.build_url() | |
150 | post.connect_replies() |
|
151 | post.connect_replies() | |
151 | post.connect_notifications() |
|
152 | post.connect_notifications() |
@@ -84,11 +84,14 b' class SyncManager:' | |||||
84 | # the post is added? Need to add some on_save signal queue |
|
84 | # the post is added? Need to add some on_save signal queue | |
85 | # and add this there. |
|
85 | # and add this there. | |
86 | key = KeyPair.objects.get(public_key=post.global_id.key) |
|
86 | key = KeyPair.objects.get(public_key=post.global_id.key) | |
87 |
signature |
|
87 | signature = Signature( | |
88 | key_type=key.key_type, |
|
88 | key_type=key.key_type, | |
89 | key=key.public_key, |
|
89 | key=key.public_key, | |
90 | signature=key.sign(et.tostring(content_tag, ENCODING_UNICODE)), |
|
90 | signature=key.sign(et.tostring(content_tag, ENCODING_UNICODE)), | |
91 | )] |
|
91 | global_id=post.global_id, | |
|
92 | ) | |||
|
93 | signature.save() | |||
|
94 | signatures = [signature] | |||
92 | for signature in signatures: |
|
95 | for signature in signatures: | |
93 | signature_tag = et.SubElement(signatures_tag, TAG_SIGNATURE) |
|
96 | signature_tag = et.SubElement(signatures_tag, TAG_SIGNATURE) | |
94 | signature_tag.set(ATTR_TYPE, signature.key_type) |
|
97 | signature_tag.set(ATTR_TYPE, signature.key_type) | |
@@ -127,13 +130,18 b' class SyncManager:' | |||||
127 | thread = tag_content.find(TAG_THREAD) |
|
130 | thread = tag_content.find(TAG_THREAD) | |
128 | tags = [] |
|
131 | tags = [] | |
129 | if thread: |
|
132 | if thread: | |
130 | opening_post = Post.objects.get( |
|
133 | thread_id = thread.find(TAG_ID) | |
131 | id=thread.find(TAG_ID).text) |
|
134 | op_global_id, exists = GlobalId.from_xml_element(thread_id) | |
|
135 | if exists: | |||
|
136 | opening_post = Post.objects.get(global_id=op_global_id) | |||
|
137 | else: | |||
|
138 | raise Exception('Load the OP first') | |||
132 | else: |
|
139 | else: | |
133 | opening_post = None |
|
140 | opening_post = None | |
134 | tag_tags = tag_content.find(TAG_TAGS) |
|
141 | tag_tags = tag_content.find(TAG_TAGS) | |
135 | for tag_tag in tag_tags: |
|
142 | for tag_tag in tag_tags: | |
136 |
tag, created = Tag.objects.get_or_create( |
|
143 | tag, created = Tag.objects.get_or_create( | |
|
144 | name=tag_tag.text) | |||
137 | tags.append(tag) |
|
145 | tags.append(tag) | |
138 |
|
146 | |||
139 | # TODO Check that the replied posts are already present |
|
147 | # TODO Check that the replied posts are already present | |
@@ -181,10 +189,13 b' class SyncManager:' | |||||
181 | signature = Signature(key_type=signature_type, |
|
189 | signature = Signature(key_type=signature_type, | |
182 | key=signature_key, |
|
190 | key=signature_key, | |
183 | signature=signature_value) |
|
191 | signature=signature_value) | |
184 | signatures.append(signature) |
|
192 | ||
|
193 | content = et.tostring(tag_content, ENCODING_UNICODE) | |||
185 |
|
194 | |||
186 | if not KeyPair.objects.verify( |
|
195 | if not KeyPair.objects.verify( | |
187 |
signature, |
|
196 | signature, content): | |
188 | raise Exception('Invalid model signature') |
|
197 | raise Exception('Invalid model signature for {}'.format(content)) | |
|
198 | ||||
|
199 | signatures.append(signature) | |||
189 |
|
200 | |||
190 | return signatures |
|
201 | return signatures |
General Comments 0
You need to be logged in to leave comments.
Login now