Add function for finding user mentions
This commit is contained in:
parent
0be8d3cc0f
commit
be993d8a07
|
@ -1,9 +1,8 @@
|
|||
import pytest
|
||||
from srht.database import db
|
||||
from todosrht.tickets import add_comment
|
||||
from todosrht.tickets import add_comment, find_mentioned_users
|
||||
from todosrht.types import TicketResolution, TicketStatus
|
||||
from todosrht.types import TicketSubscription, EventType
|
||||
from unittest.mock import patch
|
||||
|
||||
from .factories import UserFactory, TrackerFactory, TicketFactory
|
||||
|
||||
|
@ -170,3 +169,21 @@ def test_failed_comments():
|
|||
|
||||
with pytest.raises(AssertionError):
|
||||
add_comment(user, ticket)
|
||||
|
||||
|
||||
def test_find_mentioned_users():
|
||||
comment = "mentioning users ~mention1, ~mention2, and ~mention3 in a comment"
|
||||
|
||||
assert find_mentioned_users(comment) == set()
|
||||
|
||||
u1 = UserFactory(username="mention1")
|
||||
db.session.commit()
|
||||
assert find_mentioned_users(comment) == {u1}
|
||||
|
||||
u2 = UserFactory(username="mention2")
|
||||
db.session.commit()
|
||||
assert find_mentioned_users(comment) == {u1, u2}
|
||||
|
||||
u3 = UserFactory(username="mention3")
|
||||
db.session.commit()
|
||||
assert find_mentioned_users(comment) == {u1, u2, u3}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import re
|
||||
from collections import namedtuple
|
||||
from datetime import datetime
|
||||
from srht.config import cfg
|
||||
|
@ -5,7 +6,7 @@ from srht.database import db
|
|||
from todosrht.email import notify
|
||||
from todosrht.types import Event, EventType, EventNotification
|
||||
from todosrht.types import TicketComment, TicketStatus, TicketSubscription
|
||||
from todosrht.types import TicketSeen, TicketAssignee
|
||||
from todosrht.types import TicketSeen, TicketAssignee, User
|
||||
from todosrht.urls import ticket_url
|
||||
from sqlalchemy import func
|
||||
|
||||
|
@ -20,6 +21,14 @@ StatusChange = namedtuple("StatusChange", [
|
|||
"new_resolution",
|
||||
])
|
||||
|
||||
# Matches user mentions, e.g. ~username
|
||||
USER_MENTION_PATTERN = re.compile(r"~(\w+)\b")
|
||||
|
||||
def find_mentioned_users(text):
|
||||
usernames = re.findall(USER_MENTION_PATTERN, text)
|
||||
users = User.query.filter(User.username.in_(usernames)).all()
|
||||
return set(users)
|
||||
|
||||
def _create_comment(ticket, user, text):
|
||||
comment = TicketComment()
|
||||
comment.text = text
|
||||
|
|
Loading…
Reference in New Issue