# HG changeset patch # User neko259 # Date 2017-01-26 13:58:10 # Node ID 3bc312723e6b72827df8b3e95d1ed7c4c2090217 # Parent b8a95ef763d1a54761fa4a840d61746b3203d61f Allow syncing posts from timestamp diff --git a/boards/abstracts/sync_filters.py b/boards/abstracts/sync_filters.py --- a/boards/abstracts/sync_filters.py +++ b/boards/abstracts/sync_filters.py @@ -5,6 +5,7 @@ from boards.models import Post, Tag TAG_THREAD = 'thread' TAG_TAGS = 'tags' TAG_TAG = 'tag' +TAG_TIME_FROM = 'timestamp_from' class PostFilter: @@ -53,3 +54,13 @@ class TagsFilter(PostFilter): tag_tag = et.SubElement(tags_tag, TAG_TAG) tag_tag.text = tag_name + +class TimestampFromFilter(PostFilter): + def filter(self, posts): + from_time = self.content.text + return posts.filter(pub_time__gt=from_time) + + def add_filter(self, model_tag, value): + tags_from_time = et.SubElement(model_tag, TAG_TIME_FROM) + tags_from_time.text = value + diff --git a/boards/management/commands/sync_with_server.py b/boards/management/commands/sync_with_server.py --- a/boards/management/commands/sync_with_server.py +++ b/boards/management/commands/sync_with_server.py @@ -28,6 +28,8 @@ class Command(BaseCommand): help='Get posts of one specific thread') parser.add_argument('--tags', type=str, help='Get posts of the tags, comma-separated') + parser.add_argument('--time-from', type=str, + help='Get posts from the given timestamp') def handle(self, *args, **options): logger = logging.getLogger('boards.sync') @@ -66,7 +68,8 @@ class Command(BaseCommand): tags = tags_str.split(',') xml = SyncManager.generate_request_list( - opening_post=options.get('thread'), tags=tags).encode() + opening_post=options.get('thread'), tags=tags, + timestamp_from=options.get('time_from')).encode() response, content = h.request(list_url, method="POST", body=xml) if response.status != 200: raise Exception('Server returned error {}'.format(response.status)) diff --git a/boards/models/post/sync.py b/boards/models/post/sync.py --- a/boards/models/post/sync.py +++ b/boards/models/post/sync.py @@ -3,7 +3,8 @@ import logging from xml.etree import ElementTree from boards.abstracts.exceptions import SyncException -from boards.abstracts.sync_filters import ThreadFilter, TagsFilter +from boards.abstracts.sync_filters import ThreadFilter, TagsFilter,\ + TimestampFromFilter from boards.models import KeyPair, GlobalId, Signature, Post, Tag from boards.models.attachment.downloaders import download from boards.models.signature import TAG_REQUEST, ATTR_TYPE, TYPE_GET, \ @@ -360,7 +361,8 @@ class SyncManager: return et.tostring(request, 'unicode') @staticmethod - def generate_request_list(opening_post=None, tags=list()): + def generate_request_list(opening_post=None, tags=list(), + timestamp_from=None): """ Form a pull request from a list of ModelId objects. """ @@ -377,5 +379,7 @@ class SyncManager: ThreadFilter().add_filter(model, opening_post) if tags: TagsFilter().add_filter(model, tags) + if timestamp_from: + TimestampFromFilter().add_filter(model, timestamp_from) return et.tostring(request, 'unicode') diff --git a/boards/tests/test_sync.py b/boards/tests/test_sync.py --- a/boards/tests/test_sync.py +++ b/boards/tests/test_sync.py @@ -212,3 +212,42 @@ class SyncTest(TestCase): in response_thread, 'Wrong response generated for the LIST request for posts of ' 'non-existing thread.') + + def test_list_pub_time(self): + key = KeyPair.objects.generate_key(primary=True) + tag = Tag.objects.create(name='tag1') + post = Post.objects.create_post(title='test_title', + text='test_text\rline two', + tags=[tag]) + post2 = Post.objects.create_post(title='test title 2', + text='test text 2', + tags=[tag]) + + request_thread = MockRequest() + request_thread.body = ( + '' + '' + '{}' + '' + ''.format( + post.pub_time, + ) + ) + + response_thread = response_list(request_thread).content.decode() + self.assertTrue( + 'success' + '' + '' + '' + '{}' + '' + ''.format( + post2.global_id.key, + post2.global_id.local_id, + post2.global_id.key_type, + post2.version, + ) in response_thread, + 'Wrong response generated for the LIST request for posts of ' + 'existing thread.') + diff --git a/boards/views/sync.py b/boards/views/sync.py --- a/boards/views/sync.py +++ b/boards/views/sync.py @@ -5,7 +5,8 @@ import xml.etree.ElementTree as et from django.http import HttpResponse, Http404 from boards.abstracts.sync_filters import ThreadFilter, TagsFilter,\ - TAG_THREAD, TAG_TAGS + TimestampFromFilter,\ + TAG_THREAD, TAG_TAGS, TAG_TIME_FROM from boards.models import GlobalId, Post from boards.models.post.sync import SyncManager @@ -16,6 +17,7 @@ logger = logging.getLogger('boards.sync' FILTERS = { TAG_THREAD: ThreadFilter, TAG_TAGS: TagsFilter, + TAG_TIME_FROM: TimestampFromFilter, }