Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions python/modules/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def apply_karma(self) -> NoReturn:
operator = self.matched.group("operator")[0]
amount = self.matched.group("amount")
amount = int(amount) if amount else 0
reason = self.matched.group("reason")

utcnow = datetime.utcnow()

Expand Down Expand Up @@ -211,7 +212,7 @@ def apply_karma(self) -> NoReturn:
return

user_karma_change, selected_user_karma_change, collective_vote_applied, voters = self.apply_karma_change(
operator, amount)
operator, amount, reason)

if collective_vote_applied:
self.current_user.last_collective_vote = int(utcnow.timestamp())
Expand All @@ -223,14 +224,15 @@ def apply_karma(self) -> NoReturn:
self.data_service.save_user(self.user)
self.vk_instance.send_msg(
CommandsBuilder.build_karma_change(
user_karma_change, selected_user_karma_change, voters),
user_karma_change, selected_user_karma_change, voters, reason),
self.peer_id)
self.vk_instance.delete_message(self.peer_id, self.msg_id)

def apply_karma_change(
self,
operator: str,
amount: int
amount: int,
reason: Optional[str] = None
) -> Tuple[
Optional[Tuple[int, str, int, int]], # current user karma changed
Optional[Tuple[int, str, int, int]], # selected user karma changed
Expand Down
12 changes: 7 additions & 5 deletions python/modules/commands_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,16 @@ def build_top_users(
def build_karma_change(
user_karma_change: Optional[Tuple[int, str, int, int]],
selected_user_karma_change: Optional[Tuple[int, str, int, int]],
voters: List[int]
voters: List[int],
reason: Optional[str] = None
) -> Optional[str]:
"""Builds karma changing
"""
if selected_user_karma_change:
reason_text = f" Причина: {reason}" if reason else ""
if user_karma_change:
return ("Карма изменена: [id%s|%s] [%s]->[%s], [id%s|%s] [%s]->[%s]." %
(user_karma_change + selected_user_karma_change))
return ("Карма изменена: [id%s|%s] [%s]->[%s]. Голосовали: (%s)" %
(selected_user_karma_change + (", ".join([f"@id{voter}" for voter in voters]),)))
return ("Карма изменена: [id%s|%s] [%s]->[%s], [id%s|%s] [%s]->[%s].%s" %
(user_karma_change + selected_user_karma_change + (reason_text,)))
return ("Карма изменена: [id%s|%s] [%s]->[%s]. Голосовали: (%s)%s" %
(selected_user_karma_change + (", ".join([f"@id{voter}" for voter in voters]), reason_text)))
return None
2 changes: 1 addition & 1 deletion python/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
r'\A\s*(карма|karma)\s*\Z', IGNORECASE)

APPLY_KARMA = recompile(
r'\A(\[id(?<selectedUserId>\d+)\|@\w+\])?\s*(?P<operator>\+|\-)(?P<amount>[0-9]*)\s*\Z')
r'\A(\[id(?P<selectedUserId>\d+)\|@\w+\])?\s*(?P<operator>\+|\-)(?P<amount>[0-9]*)(?:\s+(?P<reason>.+?))?\s*\Z')

ADD_PROGRAMMING_LANGUAGE = recompile(
r'\A\s*\+=\s*(?P<language>' + DEFAULT_LANGUAGES + r')\s*\Z', IGNORECASE)
Expand Down
24 changes: 23 additions & 1 deletion python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,31 @@ def test_apply_collective_vote(
def test_apply_karma_change(
self
) -> NoReturn:
self.commands.apply_karma_change('-', 6)
self.commands.apply_karma_change('-', 6, "тестовая причина")
self.commands.karma_message()

@ordered
def test_karma_with_reasons(
self
) -> NoReturn:
# Test karma with reasons
test_messages = [
"+ решение сработало",
"- вопрос недостаточно конкретный",
"+5 отличная помощь",
"-2 неточный ответ"
]

for msg in test_messages:
self.commands.msg = msg
self.commands.match_command(patterns.APPLY_KARMA)
if self.commands.matched:
operator = self.commands.matched.group("operator")
amount = self.commands.matched.group("amount")
amount = int(amount) if amount else 0
reason = self.commands.matched.group("reason")
print(f"Message: '{msg}' -> Operator: '{operator}', Amount: {amount}, Reason: '{reason}'")


if __name__ == '__main__':
db = BetterBotBaseDataService("test_db")
Expand Down
Loading