import logging import xml.etree.ElementTree as et from django.http import HttpResponse, Http404 from boards.abstracts.sync_filters import ThreadFilter, TagsFilter,\ TimestampFromFilter,\ TAG_THREAD, TAG_TAGS, TAG_TIME_FROM from boards.models import GlobalId, Post from boards.models.post.sync import SyncManager logger = logging.getLogger('boards.sync') FILTERS = { TAG_THREAD: ThreadFilter, TAG_TAGS: TagsFilter, TAG_TIME_FROM: TimestampFromFilter, } def response_list(request): request_xml = request.body filters = [] if request_xml is None or len(request_xml) == 0: return HttpResponse(content='Use the API') else: root_tag = et.fromstring(request_xml) model_tag = root_tag[0] for tag_filter in model_tag: filter_name = tag_filter.tag model_filter = FILTERS.get(filter_name) if not model_filter: logger.warning('Unavailable filter: {}'.format(filter_name)) filters.append(model_filter(tag_filter)) response_xml = SyncManager.generate_response_list(filters) return HttpResponse(content=response_xml) def response_get(request): """ 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. """ request_xml = request.body if request_xml is None or len(request_xml) == 0: return HttpResponse(content='Use the API') posts = [] root_tag = et.fromstring(request_xml) model_tag = root_tag[0] for id_tag in model_tag: global_id, exists = GlobalId.from_xml_element(id_tag) if exists: posts.append(Post.objects.get(global_id=global_id)) response_xml = SyncManager.generate_response_get(posts) 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() xml_str = SyncManager.generate_response_get([post]) return HttpResponse( content_type='text/xml; charset=utf-8', content=xml_str, )