Show More
@@ -1,62 +1,55 b'' | |||||
1 | import xml.etree.ElementTree as et |
|
1 | import xml.etree.ElementTree as et | |
2 | import xml.dom.minidom |
|
|||
3 |
|
2 | |||
4 | from django.http import HttpResponse, Http404 |
|
3 | from django.http import HttpResponse, Http404 | |
5 | from boards.models import GlobalId, Post |
|
4 | from boards.models import GlobalId, Post | |
6 | from boards.models.post.sync import SyncManager |
|
5 | from boards.models.post.sync import SyncManager | |
7 |
|
6 | |||
8 |
|
7 | |||
9 | def response_list(request): |
|
8 | def response_list(request): | |
10 | request_xml = request.body |
|
9 | request_xml = request.body | |
11 |
|
10 | |||
12 | if request_xml is None or len(request_xml) == 0: |
|
11 | if request_xml is None or len(request_xml) == 0: | |
13 | return HttpResponse(content='Use the API') |
|
12 | return HttpResponse(content='Use the API') | |
14 |
|
13 | |||
15 | response_xml = SyncManager.generate_response_list() |
|
14 | response_xml = SyncManager.generate_response_list() | |
16 |
|
15 | |||
17 | return HttpResponse(content=response_xml) |
|
16 | return HttpResponse(content=response_xml) | |
18 |
|
17 | |||
19 |
|
18 | |||
20 | def response_get(request): |
|
19 | def response_get(request): | |
21 | """ |
|
20 | """ | |
22 | Processes a GET request with post ID list and returns the posts XML list. |
|
21 | Processes a GET request with post ID list and returns the posts XML list. | |
23 | Request should contain an 'xml' post attribute with the actual request XML. |
|
22 | Request should contain an 'xml' post attribute with the actual request XML. | |
24 | """ |
|
23 | """ | |
25 |
|
24 | |||
26 | request_xml = request.body |
|
25 | request_xml = request.body | |
27 |
|
26 | |||
28 | if request_xml is None or len(request_xml) == 0: |
|
27 | if request_xml is None or len(request_xml) == 0: | |
29 | return HttpResponse(content='Use the API') |
|
28 | return HttpResponse(content='Use the API') | |
30 |
|
29 | |||
31 | posts = [] |
|
30 | posts = [] | |
32 |
|
31 | |||
33 | root_tag = et.fromstring(request_xml) |
|
32 | root_tag = et.fromstring(request_xml) | |
34 | model_tag = root_tag[0] |
|
33 | model_tag = root_tag[0] | |
35 | for id_tag in model_tag: |
|
34 | for id_tag in model_tag: | |
36 | global_id, exists = GlobalId.from_xml_element(id_tag) |
|
35 | global_id, exists = GlobalId.from_xml_element(id_tag) | |
37 | if exists: |
|
36 | if exists: | |
38 | posts.append(Post.objects.get(global_id=global_id)) |
|
37 | posts.append(Post.objects.get(global_id=global_id)) | |
39 |
|
38 | |||
40 | response_xml = SyncManager.generate_response_get(posts) |
|
39 | response_xml = SyncManager.generate_response_get(posts) | |
41 |
|
40 | |||
42 | return HttpResponse(content=response_xml) |
|
41 | return HttpResponse(content=response_xml) | |
43 |
|
42 | |||
44 |
|
43 | |||
45 | def get_post_sync_data(request, post_id): |
|
44 | def get_post_sync_data(request, post_id): | |
46 | try: |
|
45 | try: | |
47 | post = Post.objects.get(id=post_id) |
|
46 | post = Post.objects.get(id=post_id) | |
48 | except Post.DoesNotExist: |
|
47 | except Post.DoesNotExist: | |
49 | raise Http404() |
|
48 | raise Http404() | |
50 |
|
49 | |||
51 | xml_str = SyncManager.generate_response_get([post]) |
|
50 | xml_str = SyncManager.generate_response_get([post]) | |
52 |
|
51 | |||
53 | xml_repr = xml.dom.minidom.parseString(xml_str) |
|
|||
54 | xml_repr = xml_repr.toprettyxml() |
|
|||
55 |
|
||||
56 | content = '=Global ID=\n%s\n\n=XML=\n%s' \ |
|
|||
57 | % (post.global_id, xml_repr) |
|
|||
58 |
|
||||
59 | return HttpResponse( |
|
52 | return HttpResponse( | |
60 |
content_type='text/ |
|
53 | content_type='text/xml; charset=utf-8', | |
61 |
content= |
|
54 | content=xml_str, | |
62 | ) No newline at end of file |
|
55 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now