Extract topic_match_sa() helper.

We'll also use this in zerver/views/messages.py, but
that's a bigger change.
This commit is contained in:
Steve Howell
2018-11-01 20:48:49 +00:00
committed by Tim Abbott
parent df743e8948
commit 79d5e36ca3
3 changed files with 18 additions and 3 deletions

View File

@@ -276,7 +276,9 @@ def build_custom_checkers(by_lang):
'description': 'avoid subject as a var', 'description': 'avoid subject as a var',
'good_lines': ['topic_name'], 'good_lines': ['topic_name'],
'bad_lines': ['subject="foo"', ' MAX_SUBJECT_LEN'], 'bad_lines': ['subject="foo"', ' MAX_SUBJECT_LEN'],
'include_only': set(['zerver/lib/actions.py'])}, 'include_only': set([
'zerver/lib/actions.py',
'zerver/lib/topic_mutes.py'])},
{'pattern': '^(?!#)@login_required', {'pattern': '^(?!#)@login_required',
'description': '@login_required is unsupported; use @zulip_login_required', 'description': '@login_required is unsupported; use @zulip_login_required',
'good_lines': ['@zulip_login_required', '# foo @login_required'], 'good_lines': ['@zulip_login_required', '# foo @login_required'],

View File

@@ -4,6 +4,11 @@ from django.db import connection
from django.db.models.query import QuerySet, Q from django.db.models.query import QuerySet, Q
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from sqlalchemy.sql import (
column,
func,
)
from zerver.models import ( from zerver.models import (
Message, Message,
Recipient, Recipient,
@@ -18,6 +23,12 @@ TOPIC_NAME = "subject"
TOPIC_LINKS = "subject_links" TOPIC_LINKS = "subject_links"
PREV_TOPIC = "prev_subject" PREV_TOPIC = "prev_subject"
def topic_match_sa(topic_name: str) -> Any:
# _sa is short for Sql Alchemy, which we use mostly for
# queries that search messages
topic_cond = func.upper(column("subject")) == func.upper(topic_name)
return topic_cond
def filter_by_exact_message_topic(query: QuerySet, message: Message) -> QuerySet: def filter_by_exact_message_topic(query: QuerySet, message: Message) -> QuerySet:
topic_name = message.topic_name() topic_name = message.topic_name()
return query.filter(subject=topic_name) return query.filter(subject=topic_name)

View File

@@ -1,5 +1,8 @@
from typing import Any, Callable, Dict, List, Optional from typing import Any, Callable, Dict, List, Optional
from zerver.lib.topic import (
topic_match_sa,
)
from zerver.models import ( from zerver.models import (
get_stream_recipient, get_stream_recipient,
get_stream, get_stream,
@@ -9,7 +12,6 @@ from zerver.models import (
from sqlalchemy.sql import ( from sqlalchemy.sql import (
and_, and_,
column, column,
func,
not_, not_,
or_, or_,
Selectable Selectable
@@ -97,7 +99,7 @@ def exclude_topic_mutes(conditions: List[Selectable],
recipient_id = row['recipient_id'] recipient_id = row['recipient_id']
topic_name = row['topic_name'] topic_name = row['topic_name']
stream_cond = column("recipient_id") == recipient_id stream_cond = column("recipient_id") == recipient_id
topic_cond = func.upper(column("subject")) == func.upper(topic_name) topic_cond = topic_match_sa(topic_name)
return and_(stream_cond, topic_cond) return and_(stream_cond, topic_cond)
condition = not_(or_(*list(map(mute_cond, rows)))) condition = not_(or_(*list(map(mute_cond, rows))))