##// END OF EJS Templates
Allow filtering sync by tags
neko259 -
r1844:031556bd default
parent child Browse files
Show More
@@ -1,8 +1,10 b''
1 import xml.etree.ElementTree as et
1 import xml.etree.ElementTree as et
2
2
3 from boards.models import Post
3 from boards.models import Post, Tag
4
4
5 TAG_THREAD = 'thread'
5 TAG_THREAD = 'thread'
6 TAG_TAGS = 'tags'
7 TAG_TAG = 'tag'
6
8
7
9
8 class PostFilter:
10 class PostFilter:
@@ -29,3 +31,25 b' class ThreadFilter(PostFilter):'
29 def add_filter(self, model_tag, value):
31 def add_filter(self, model_tag, value):
30 thread_tag = et.SubElement(model_tag, TAG_THREAD)
32 thread_tag = et.SubElement(model_tag, TAG_THREAD)
31 thread_tag.text = str(value)
33 thread_tag.text = str(value)
34
35
36 class TagsFilter(PostFilter):
37 def filter(self, posts):
38 tags = []
39 for tag_tag in self.content:
40 try:
41 tags.append(Tag.objects.get(name=tag_tag.text))
42 except Tag.DoesNotExist:
43 pass
44
45 if tags:
46 return posts.filter(thread__tags__in=tags)
47 else:
48 return posts.none()
49
50 def add_filter(self, model_tag, value):
51 tags_tag = et.SubElement(model_tag, TAG_TAGS)
52 for tag_name in value:
53 tag_tag = et.SubElement(tags_tag, TAG_TAG)
54 tag_tag.text = tag_name
55
@@ -26,6 +26,8 b' class Command(BaseCommand):'
26 ' number of posts in one')
26 ' number of posts in one')
27 parser.add_argument('--thread', type=int,
27 parser.add_argument('--thread', type=int,
28 help='Get posts of one specific thread')
28 help='Get posts of one specific thread')
29 parser.add_argument('--tags', type=str,
30 help='Get posts of the tags, comma-separated')
29
31
30 def handle(self, *args, **options):
32 def handle(self, *args, **options):
31 logger = logging.getLogger('boards.sync')
33 logger = logging.getLogger('boards.sync')
@@ -58,8 +60,11 b' class Command(BaseCommand):'
58 logger.info('Running LIST request...')
60 logger.info('Running LIST request...')
59 h = httplib2.Http()
61 h = httplib2.Http()
60 xml = SyncManager.generate_request_list(
62 xml = SyncManager.generate_request_list(
61 opening_post=options.get('thread'))
63 opening_post=options.get('thread'), tags=options.get('tags').split(',')).encode()
62 response, content = h.request(list_url, method="POST", body=xml)
64 response, content = h.request(list_url, method="POST", body=xml)
65 if response.status != 200:
66 raise Exception('Server returned error {}'.format(response.status))
67
63 logger.info('Processing response...')
68 logger.info('Processing response...')
64
69
65 root = ET.fromstring(content)
70 root = ET.fromstring(content)
@@ -3,7 +3,7 b' import logging'
3 from xml.etree import ElementTree
3 from xml.etree import ElementTree
4
4
5 from boards.abstracts.exceptions import SyncException
5 from boards.abstracts.exceptions import SyncException
6 from boards.abstracts.sync_filters import ThreadFilter
6 from boards.abstracts.sync_filters import ThreadFilter, TagsFilter
7 from boards.models import KeyPair, GlobalId, Signature, Post, Tag
7 from boards.models import KeyPair, GlobalId, Signature, Post, Tag
8 from boards.models.attachment.downloaders import download
8 from boards.models.attachment.downloaders import download
9 from boards.models.signature import TAG_REQUEST, ATTR_TYPE, TYPE_GET, \
9 from boards.models.signature import TAG_REQUEST, ATTR_TYPE, TYPE_GET, \
@@ -360,7 +360,7 b' class SyncManager:'
360 return et.tostring(request, 'unicode')
360 return et.tostring(request, 'unicode')
361
361
362 @staticmethod
362 @staticmethod
363 def generate_request_list(opening_post=None):
363 def generate_request_list(opening_post=None, tags=list()):
364 """
364 """
365 Form a pull request from a list of ModelId objects.
365 Form a pull request from a list of ModelId objects.
366 """
366 """
@@ -375,5 +375,7 b' class SyncManager:'
375
375
376 if opening_post:
376 if opening_post:
377 ThreadFilter().add_filter(model, opening_post)
377 ThreadFilter().add_filter(model, opening_post)
378 if tags:
379 TagsFilter().add_filter(model, tags)
378
380
379 return et.tostring(request, 'unicode')
381 return et.tostring(request, 'unicode')
@@ -4,7 +4,8 b' import xml.etree.ElementTree as et'
4
4
5 from django.http import HttpResponse, Http404
5 from django.http import HttpResponse, Http404
6
6
7 from boards.abstracts.sync_filters import ThreadFilter, TAG_THREAD
7 from boards.abstracts.sync_filters import ThreadFilter, TagsFilter,\
8 TAG_THREAD, TAG_TAGS
8 from boards.models import GlobalId, Post
9 from boards.models import GlobalId, Post
9 from boards.models.post.sync import SyncManager
10 from boards.models.post.sync import SyncManager
10
11
@@ -14,6 +15,7 b" logger = logging.getLogger('boards.sync'"
14
15
15 FILTERS = {
16 FILTERS = {
16 TAG_THREAD: ThreadFilter,
17 TAG_THREAD: ThreadFilter,
18 TAG_TAGS: TagsFilter,
17 }
19 }
18
20
19
21
@@ -30,10 +32,10 b' def response_list(request):'
30
32
31 for tag_filter in model_tag:
33 for tag_filter in model_tag:
32 filter_name = tag_filter.tag
34 filter_name = tag_filter.tag
33 model_filter = FILTERS.get(filter_name)(tag_filter)
35 model_filter = FILTERS.get(filter_name)
34 if not model_filter:
36 if not model_filter:
35 logger.warning('Unavailable filter: {}'.format(filter_name))
37 logger.warning('Unavailable filter: {}'.format(filter_name))
36 filters.append(model_filter)
38 filters.append(model_filter(tag_filter))
37
39
38 response_xml = SyncManager.generate_response_list(filters)
40 response_xml = SyncManager.generate_response_list(filters)
39
41
General Comments 0
You need to be logged in to leave comments. Login now