Show More
@@ -1,9 +1,9 b'' | |||||
1 | import re |
|
1 | import re | |
2 | import urllib.parse |
|
|||
3 | import httplib2 |
|
|||
4 | import xml.etree.ElementTree as ET |
|
2 | import xml.etree.ElementTree as ET | |
5 |
|
3 | |||
|
4 | import httplib2 | |||
6 | from django.core.management import BaseCommand |
|
5 | from django.core.management import BaseCommand | |
|
6 | ||||
7 | from boards.models import GlobalId |
|
7 | from boards.models import GlobalId | |
8 | from boards.models.post.sync import SyncManager |
|
8 | from boards.models.post.sync import SyncManager | |
9 |
|
9 | |||
@@ -14,12 +14,12 b" REGEX_GLOBAL_ID = re.compile(r'(\\w+)::([" | |||||
14 |
|
14 | |||
15 |
|
15 | |||
16 | class Command(BaseCommand): |
|
16 | 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 | 'sync_with_server <server_url> [post_global_id]' |
|
|||
19 |
|
18 | |||
20 | def add_arguments(self, parser): |
|
19 | def add_arguments(self, parser): | |
21 | parser.add_argument('url', type=str) |
|
20 | parser.add_argument('url', type=str) | |
22 |
parser.add_argument('global_id', type=str |
|
21 | parser.add_argument('--global_id', type=str, default='', | |
|
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') | |
@@ -44,7 +44,8 b' class Command(BaseCommand):' | |||||
44 | raise Exception('Invalid global ID') |
|
44 | raise Exception('Invalid global ID') | |
45 | else: |
|
45 | else: | |
46 | h = httplib2.Http() |
|
46 | h = httplib2.Http() | |
47 | response, content = h.request(url, method="POST") |
|
47 | xml = GlobalId.objects.generate_request_pull() | |
|
48 | response, content = h.request(url, method="POST", body=xml) | |||
48 |
|
49 | |||
49 | print(content) |
|
50 | print(content) | |
50 |
|
51 | |||
@@ -53,7 +54,6 b' class Command(BaseCommand):' | |||||
53 | if status == 'success': |
|
54 | if status == 'success': | |
54 | models = root.findall('models')[0] |
|
55 | models = root.findall('models')[0] | |
55 | for model in models: |
|
56 | for model in models: | |
56 | model_content = model[0] |
|
57 | print(ET.tostring(model)) | |
57 | print(model_content.findall('text')[0].text) |
|
|||
58 | else: |
|
58 | else: | |
59 | raise Exception('Invalid response status') |
|
59 | raise Exception('Invalid response status') |
@@ -150,6 +150,21 b' class SyncManager:' | |||||
150 | pass |
|
150 | pass | |
151 |
|
151 | |||
152 | @staticmethod |
|
152 | @staticmethod | |
|
153 | def generate_response_pull(): | |||
|
154 | response = et.Element(TAG_RESPONSE) | |||
|
155 | ||||
|
156 | status = et.SubElement(response, TAG_STATUS) | |||
|
157 | status.text = STATUS_SUCCESS | |||
|
158 | ||||
|
159 | models = et.SubElement(response, TAG_MODELS) | |||
|
160 | ||||
|
161 | for post in Post.objects.all(): | |||
|
162 | tag_id = et.SubElement(models, TAG_ID) | |||
|
163 | post.global_id.to_xml_element(tag_id) | |||
|
164 | ||||
|
165 | return et.tostring(response, ENCODING_UNICODE) | |||
|
166 | ||||
|
167 | @staticmethod | |||
153 | def _verify_model(tag_content, tag_model): |
|
168 | def _verify_model(tag_content, tag_model): | |
154 | """ |
|
169 | """ | |
155 | Verifies all signatures for a single model. |
|
170 | Verifies all signatures for a single model. |
@@ -7,6 +7,7 b" TAG_REQUEST = 'request'" | |||||
7 | TAG_ID = 'id' |
|
7 | TAG_ID = 'id' | |
8 |
|
8 | |||
9 | TYPE_GET = 'get' |
|
9 | TYPE_GET = 'get' | |
|
10 | TYPE_PULL = 'pull' | |||
10 |
|
11 | |||
11 | ATTR_VERSION = 'version' |
|
12 | ATTR_VERSION = 'version' | |
12 | ATTR_TYPE = 'type' |
|
13 | ATTR_TYPE = 'type' | |
@@ -37,6 +38,21 b' class GlobalIdManager(models.Manager):' | |||||
37 |
|
38 | |||
38 | return et.tostring(request, 'unicode') |
|
39 | return et.tostring(request, 'unicode') | |
39 |
|
40 | |||
|
41 | def generate_request_pull(self): | |||
|
42 | """ | |||
|
43 | Form a pull request from a list of ModelId objects. | |||
|
44 | """ | |||
|
45 | ||||
|
46 | request = et.Element(TAG_REQUEST) | |||
|
47 | request.set(ATTR_TYPE, TYPE_PULL) | |||
|
48 | request.set(ATTR_VERSION, '1.0') | |||
|
49 | ||||
|
50 | model = et.SubElement(request, TAG_MODEL) | |||
|
51 | model.set(ATTR_VERSION, '1.0') | |||
|
52 | model.set(ATTR_NAME, 'post') | |||
|
53 | ||||
|
54 | return et.tostring(request, 'unicode') | |||
|
55 | ||||
40 | def global_id_exists(self, global_id): |
|
56 | def global_id_exists(self, global_id): | |
41 | """ |
|
57 | """ | |
42 | Checks if the same global id already exists in the system. |
|
58 | Checks if the same global id already exists in the system. |
@@ -10,7 +10,7 b' from boards.views.notifications import N' | |||||
10 | from boards.views.search import BoardSearchView |
|
10 | from boards.views.search import BoardSearchView | |
11 | from boards.views.static import StaticPageView |
|
11 | from boards.views.static import StaticPageView | |
12 | from boards.views.preview import PostPreviewView |
|
12 | from boards.views.preview import PostPreviewView | |
13 | from boards.views.sync import get_post_sync_data, response_get |
|
13 | from boards.views.sync import get_post_sync_data, response_get, response_pull | |
14 | from boards.views.random import RandomImageView |
|
14 | from boards.views.random import RandomImageView | |
15 |
|
15 | |||
16 |
|
16 | |||
@@ -72,7 +72,7 b" urlpatterns = patterns(''," | |||||
72 | url(r'^api/preview/$', api.api_get_preview, name='preview'), |
|
72 | url(r'^api/preview/$', api.api_get_preview, name='preview'), | |
73 |
|
73 | |||
74 | # Sync protocol API |
|
74 | # Sync protocol API | |
75 |
url(r'^api/sync/pull/$', |
|
75 | url(r'^api/sync/pull/$', response_pull, name='api_sync_pull'), | |
76 | url(r'^api/sync/get/$', response_get, name='api_sync_pull'), |
|
76 | url(r'^api/sync/get/$', response_get, name='api_sync_pull'), | |
77 | # TODO 'get' request |
|
77 | # TODO 'get' request | |
78 |
|
78 |
@@ -241,18 +241,3 b' def api_get_preview(request):' | |||||
241 |
|
241 | |||
242 | parser = Parser() |
|
242 | parser = Parser() | |
243 | return HttpResponse(content=parser.parse(parser.preparse(raw_text))) |
|
243 | return HttpResponse(content=parser.parse(parser.preparse(raw_text))) | |
244 |
|
||||
245 |
|
||||
246 | # TODO Make a separate module for sync API methods |
|
|||
247 | def sync_pull(request): |
|
|||
248 | """ |
|
|||
249 | Return 'pull' request response for all posts. |
|
|||
250 | """ |
|
|||
251 | request_xml = request.get('xml') |
|
|||
252 | if request_xml is None: |
|
|||
253 | posts = Post.objects.all() |
|
|||
254 | else: |
|
|||
255 | pass # TODO Parse the XML and get filters from it |
|
|||
256 |
|
||||
257 | xml = SyncManager.generate_response_get(posts) |
|
|||
258 | return HttpResponse(content=xml) |
|
@@ -5,7 +5,14 b' from boards.models.post.sync import Sync' | |||||
5 |
|
5 | |||
6 |
|
6 | |||
7 | def response_pull(request): |
|
7 | def response_pull(request): | |
8 | pass |
|
8 | request_xml = request.body | |
|
9 | ||||
|
10 | if request_xml is None: | |||
|
11 | return HttpResponse(content='Use the API') | |||
|
12 | ||||
|
13 | response_xml = SyncManager.generate_response_pull() | |||
|
14 | ||||
|
15 | return HttpResponse(content=response_xml) | |||
9 |
|
16 | |||
10 |
|
17 | |||
11 | def response_get(request): |
|
18 | def response_get(request): |
General Comments 0
You need to be logged in to leave comments.
Login now