sync.py
61 lines
| 1.6 KiB
| text/x-python
|
PythonLexer
neko259
|
r836 | import xml.etree.ElementTree as et | |
neko259
|
r1511 | import xml.dom.minidom | |
neko259
|
r837 | from django.http import HttpResponse, Http404 | |
neko259
|
r836 | from boards.models import GlobalId, Post | |
neko259
|
r1177 | from boards.models.post.sync import SyncManager | |
neko259
|
r836 | ||
neko259
|
r1177 | def response_pull(request): | |
neko259
|
r1321 | request_xml = request.body | |
if request_xml is None: | |||
return HttpResponse(content='Use the API') | |||
response_xml = SyncManager.generate_response_pull() | |||
return HttpResponse(content=response_xml) | |||
neko259
|
r810 | ||
neko259
|
r1177 | def response_get(request): | |
neko259
|
r836 | """ | |
Processes a GET request with post ID list and returns the posts XML list. | |||
Request should contain an 'xml' post attribute with the actual request XML. | |||
""" | |||
neko259
|
r1177 | request_xml = request.body | |
if request_xml is None: | |||
return HttpResponse(content='Use the API') | |||
neko259
|
r836 | ||
posts = [] | |||
root_tag = et.fromstring(request_xml) | |||
model_tag = root_tag[0] | |||
for id_tag in model_tag: | |||
neko259
|
r1233 | global_id, exists = GlobalId.from_xml_element(id_tag) | |
if exists: | |||
neko259
|
r1177 | posts.append(Post.objects.get(global_id=global_id)) | |
neko259
|
r836 | ||
neko259
|
r1236 | response_xml = SyncManager.generate_response_get(posts) | |
neko259
|
r836 | ||
neko259
|
r837 | return HttpResponse(content=response_xml) | |
def get_post_sync_data(request, post_id): | |||
try: | |||
post = Post.objects.get(id=post_id) | |||
except Post.DoesNotExist: | |||
raise Http404() | |||
neko259
|
r1511 | xml_str = SyncManager.generate_response_get([post]) | |
neko259
|
r837 | ||
neko259
|
r1511 | xml_repr = xml.dom.minidom.parseString(xml_str) | |
xml_repr = xml_repr.toprettyxml() | |||
content = '=Global ID=\n%s\n\n=XML=\n%s' \ | |||
% (post.global_id, xml_repr) | |||
neko259
|
r837 | ||
return HttpResponse( | |||
content_type='text/plain', | |||
content=content, | |||
) |