##// END OF EJS Templates
Version bump
Version bump

File last commit:

r1846:3bc31272 default
r2085:7c9be4c6 4.9.2 default
Show More
sync_filters.py
66 lines | 1.7 KiB | text/x-python | PythonLexer
neko259
Added ability to filter posts in the LIST request
r1834 import xml.etree.ElementTree as et
neko259
Allow filtering sync by tags
r1844 from boards.models import Post, Tag
neko259
Added ability to filter posts in the LIST request
r1834
TAG_THREAD = 'thread'
neko259
Allow filtering sync by tags
r1844 TAG_TAGS = 'tags'
TAG_TAG = 'tag'
neko259
Allow syncing posts from timestamp
r1846 TAG_TIME_FROM = 'timestamp_from'
neko259
Added ability to filter posts in the LIST request
r1834
class PostFilter:
def __init__(self, content=None):
self.content = content
def filter(self, posts):
return posts
def add_filter(self, model_tag, value):
return model_tag
class ThreadFilter(PostFilter):
def filter(self, posts):
op_id = self.content.text
op = Post.objects.filter(opening=True, id=op_id).first()
if op:
return posts.filter(thread=op.get_thread())
else:
return posts.none()
def add_filter(self, model_tag, value):
thread_tag = et.SubElement(model_tag, TAG_THREAD)
thread_tag.text = str(value)
neko259
Allow filtering sync by tags
r1844
class TagsFilter(PostFilter):
def filter(self, posts):
tags = []
for tag_tag in self.content:
try:
tags.append(Tag.objects.get(name=tag_tag.text))
except Tag.DoesNotExist:
pass
if tags:
return posts.filter(thread__tags__in=tags)
else:
return posts.none()
def add_filter(self, model_tag, value):
tags_tag = et.SubElement(model_tag, TAG_TAGS)
for tag_name in value:
tag_tag = et.SubElement(tags_tag, TAG_TAG)
tag_tag.text = tag_name
neko259
Allow syncing posts from timestamp
r1846
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