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