server/search: allow searching by post flags

This commit is contained in:
Shyam Sunder 2018-09-21 17:34:00 -04:00 committed by Marcin Kurczewski
parent 2235a72d2f
commit 3879c2ec20
6 changed files with 86 additions and 5 deletions

View File

@ -70,6 +70,10 @@
<td><code>type</code></td> <td><code>type</code></td>
<td>given type of posts. <code>&lt;value&gt;</code> can be either <code>image</code>, <code>animation</code> (or <code>animated</code> or <code>anim</code>), <code>flash</code> (or <code>swf</code>) or <code>video</code> (or <code>webm</code>).</td> <td>given type of posts. <code>&lt;value&gt;</code> can be either <code>image</code>, <code>animation</code> (or <code>animated</code> or <code>anim</code>), <code>flash</code> (or <code>swf</code>) or <code>video</code> (or <code>webm</code>).</td>
</tr> </tr>
<tr>
<td><code>flag</code></td>
<td>having given flag. <code>&lt;value&gt;</code> can be either <code>loop</code> or <code>sound</code>.</td>
</tr>
<tr> <tr>
<td><code>content-checksum</code></td> <td><code>content-checksum</code></td>
<td>having given SHA1 checksum</td> <td>having given SHA1 checksum</td>

View File

@ -222,7 +222,7 @@ class PostSerializer(serialization.BaseSerializer):
return get_post_thumbnail_url(self.post) return get_post_thumbnail_url(self.post)
def serialize_flags(self) -> Any: def serialize_flags(self) -> Any:
return self.post.flags return [x for x in self.post.flags.split(',') if x]
def serialize_tags(self) -> Any: def serialize_tags(self) -> Any:
return [ return [
@ -356,7 +356,7 @@ def create_post(
post.safety = model.Post.SAFETY_SAFE post.safety = model.Post.SAFETY_SAFE
post.user = user post.user = user
post.creation_time = datetime.utcnow() post.creation_time = datetime.utcnow()
post.flags = [] post.flags = ''
post.type = '' post.type = ''
post.checksum = '' post.checksum = ''
@ -628,7 +628,7 @@ def update_post_flags(post: model.Post, flags: List[str]) -> None:
raise InvalidPostFlagError( raise InvalidPostFlagError(
'Flag must be one of %r.' % list(FLAG_MAP.values())) 'Flag must be one of %r.' % list(FLAG_MAP.values()))
target_flags.append(flag) target_flags.append(flag)
post.flags = target_flags post.flags = ','.join(target_flags)
def feature_post(post: model.Post, user: Optional[model.User]) -> None: def feature_post(post: model.Post, user: Optional[model.User]) -> None:

View File

@ -29,7 +29,7 @@ def get_post_snapshot(post: model.Post) -> Dict[str, Any]:
'source': post.source, 'source': post.source,
'safety': post.safety, 'safety': post.safety,
'checksum': post.checksum, 'checksum': post.checksum,
'flags': post.flags, 'flags': sorted(post.flags.split(',')),
'featured': post.is_featured, 'featured': post.is_featured,
'tags': sorted([tag.first_name for tag in post.tags]), 'tags': sorted([tag.first_name for tag in post.tags]),
'relations': sorted([rel.post_id for rel in post.relations]), 'relations': sorted([rel.post_id for rel in post.relations]),

View File

@ -0,0 +1,63 @@
'''
Change flags column to string
Revision ID: 1cd4c7b22846
Created at: 2018-09-21 19:37:27.686568
'''
import sqlalchemy as sa
from alembic import op
revision = '1cd4c7b22846'
down_revision = 'a39c7f98a7fa'
branch_labels = None
depends_on = None
def upgrade():
conn = op.get_bind()
op.alter_column('post', 'flags', new_column_name='oldflags')
op.add_column('post', sa.Column(
'flags', sa.Unicode(200), default='', nullable=True))
posts = sa.Table(
'post',
sa.MetaData(),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('flags', sa.Unicode(200), default='', nullable=True),
sa.Column('oldflags', sa.PickleType(), nullable=True),
)
for row in conn.execute(posts.select()):
newflag = ','.join(row.oldflags)
conn.execute(
# pylint: disable=no-value-for-parameter
posts.update().where(
posts.c.id == row.id
).values(
flags=newflag
)
)
op.drop_column('post', 'oldflags')
def downgrade():
conn = op.get_bind()
op.alter_column('post', 'flags', new_column_name='oldflags')
op.add_column('post', sa.Column('flags', sa.PickleType(), nullable=True))
posts = sa.Table(
'post',
sa.MetaData(),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('flags', sa.PickleType(), nullable=True),
sa.Column('oldflags', sa.Unicode(200), default='', nullable=True),
)
for row in conn.execute(posts.select()):
newflag = [x for x in row.oldflags.split(',') if x]
conn.execute(
# pylint: disable=no-value-for-parameter
posts.update().where(
posts.c.id == row.id
).values(
flags=newflag
)
)
op.drop_column('post', 'oldflags')

View File

@ -169,7 +169,7 @@ class Post(Base):
last_edit_time = sa.Column('last_edit_time', sa.DateTime) last_edit_time = sa.Column('last_edit_time', sa.DateTime)
safety = sa.Column('safety', sa.Unicode(32), nullable=False) safety = sa.Column('safety', sa.Unicode(32), nullable=False)
source = sa.Column('source', sa.Unicode(200)) source = sa.Column('source', sa.Unicode(200))
flags = sa.Column('flags', sa.PickleType, default=None) flags = sa.Column('flags', sa.Unicode(200), default='')
# content description # content description
type = sa.Column('type', sa.Unicode(32), nullable=False) type = sa.Column('type', sa.Unicode(32), nullable=False)

View File

@ -35,6 +35,14 @@ def _safety_transformer(value: str) -> str:
return search_util.enum_transformer(available_values, value) return search_util.enum_transformer(available_values, value)
def _flag_transformer(value: str) -> str:
available_values = {
'loop': model.Post.FLAG_LOOP,
'sound': model.Post.FLAG_SOUND,
}
return '%' + search_util.enum_transformer(available_values, value) + '%'
def _create_score_filter(score: int) -> Filter: def _create_score_filter(score: int) -> Filter:
def wrapper( def wrapper(
query: SaQuery, query: SaQuery,
@ -326,6 +334,12 @@ class PostSearchConfig(BaseSearchConfig):
['note-text'], ['note-text'],
_note_filter _note_filter
), ),
(
['flag'],
search_util.create_str_filter(
model.Post.flags, _flag_transformer)
),
]) ])
@property @property