# HG changeset patch # User neko259 # Date 2014-08-17 10:38:44 # Node ID 6bdf5d6a8b0350da085d0d25e9968c8a83bc7376 # Parent 5b80316a44a4de865edd95582ce81a038b1ff1e6 Added a generator for the 'get' request for sync diff --git a/boards/tests.py b/boards/tests.py --- a/boards/tests.py +++ b/boards/tests.py @@ -14,6 +14,7 @@ from boards import urls from boards import settings from boards.views.api import api_get_threaddiff from boards.utils import datetime_to_epoch +from boards.views.sync import ModelId, generate_request_get import neboard TEST_TAG = 'test_tag' @@ -146,7 +147,7 @@ class PagesTest(TestCase): def test_404(self): """Test receiving error 404 when opening a non-existent page""" - tag_name = u'test_tag' + tag_name = 'test_tag' tag = Tag.objects.create(name=tag_name) client = Client() @@ -156,22 +157,22 @@ class PagesTest(TestCase): response_existing = client.get(THREAD_PAGE + str(existing_post_id) + '/') self.assertEqual(HTTP_CODE_OK, response_existing.status_code, - u'Cannot open existing thread') + 'Cannot open existing thread') response_not_existing = client.get(THREAD_PAGE + str( existing_post_id + 1) + '/') self.assertEqual(PAGE_404, response_not_existing.templates[0].name, - u'Not existing thread is opened') + 'Not existing thread is opened') response_existing = client.get(TAG_PAGE + tag_name + '/') self.assertEqual(HTTP_CODE_OK, response_existing.status_code, - u'Cannot open existing tag') + 'Cannot open existing tag') - response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/') + response_not_existing = client.get(TAG_PAGE + 'not_tag' + '/') self.assertEqual(PAGE_404, response_not_existing.templates[0].name, - u'Not existing tag is opened') + 'Not existing tag is opened') reply_id = Post.objects.create_post('', TEST_TEXT, thread=Post.objects.all()[0] @@ -180,15 +181,15 @@ class PagesTest(TestCase): reply_id) + '/') self.assertEqual(PAGE_404, response_not_existing.templates[0].name, - u'Reply is opened as a thread') + 'Reply is opened as a thread') class FormTest(TestCase): def test_post_validation(self): client = Client() - valid_tags = u'tag1 tag_2 тег_3' - invalid_tags = u'$%_356 ---' + valid_tags = 'tag1 tag_2 тег_3' + invalid_tags = '$%_356 ---' response = client.post(NEW_THREAD_PAGE, {'title': 'test title', 'text': TEST_TEXT, @@ -213,13 +214,13 @@ class FormTest(TestCase): response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT, 'tags': valid_tags}) self.assertEqual(HTTP_CODE_REDIRECT, response.status_code, - msg=u'Posting new message failed: got code ' + + msg='Posting new message failed: got code ' + str(response.status_code)) # Restore posting delay neboard.settings.POSTING_DELAY = old_posting_delay self.assertEqual(2, Post.objects.count(), - msg=u'No posts were created') + msg='No posts were created') class ViewTest(TestCase): @@ -258,7 +259,7 @@ class AbstractTest(TestCase): settings_manager.set_setting('test_setting', 'test_value') self.assertEqual('test_value', settings_manager.get_setting( - 'test_setting'), u'Setting update failed.') + 'test_setting'), 'Setting update failed.') class MockRequest: @@ -292,7 +293,15 @@ class KeyTest(TestCase): ' one primary key.') except Exception: pass - + + def test_request_get(self): + model_id = ModelId('111', '222', '333') + self.assertTrue('' in + generate_request_get([model_id]), + 'Wrong XML generated for the GET request.') + class ApiTest(TestCase): def test_thread_diff(self): diff --git a/boards/views/sync.py b/boards/views/sync.py new file mode 100644 --- /dev/null +++ b/boards/views/sync.py @@ -0,0 +1,56 @@ +import xml.etree.ElementTree as et + +TAG_MODEL = 'model' +TAG_REQUEST = 'request' +TAG_ID = 'id' + +TYPE_GET = 'get' + +ATTR_VERSION = 'version' +ATTR_TYPE = 'type' +ATTR_NAME = 'name' +ATTR_KEY = 'key' +ATTR_KEY_TYPE = 'type' +ATTR_LOCAL_ID = 'local-id' + + +class ModelId: + """ + Model ID describes a global ID that consists of sender's key (with the key + type) and local ID on the sender node. + """ + + def __init__(self, key, key_type, local_id): + self.key = key + self.key_type = key_type + self.local_id = local_id + + +def respond_pull(request): + pass + + +def respond_get(request): + pass + + +def generate_request_get(id_list: list): + """ + Form a get request from a list of ModelId objects. + """ + + request = et.Element(TAG_REQUEST) + request.set(ATTR_TYPE, TYPE_GET) + request.set(ATTR_VERSION, '1.0') + + model = et.SubElement(request, TAG_MODEL) + model.set(ATTR_VERSION, '1.0') + model.set(ATTR_NAME, 'post') + + for model_id in id_list: + tag_id = et.SubElement(model, TAG_ID) + tag_id.set(ATTR_KEY, model_id.key) + tag_id.set(ATTR_KEY_TYPE, model_id.key_type) + tag_id.set(ATTR_LOCAL_ID, model_id.local_id) + + return et.tostring(request, 'unicode') \ No newline at end of file diff --git a/docs/samples/request_get.xml b/docs/samples/request_get.xml --- a/docs/samples/request_get.xml +++ b/docs/samples/request_get.xml @@ -1,7 +1,7 @@ - - + + diff --git a/docs/samples/response_pull.xml b/docs/samples/response_pull.xml --- a/docs/samples/response_pull.xml +++ b/docs/samples/response_pull.xml @@ -2,9 +2,9 @@ success - - - - + + + + diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +simplejson south>=0.8.4 haystack pillow