diff --git a/python/modules/commands.py b/python/modules/commands.py index 93d99817..1a0bd6ac 100644 --- a/python/modules/commands.py +++ b/python/modules/commands.py @@ -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() @@ -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()) @@ -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 diff --git a/python/modules/commands_builder.py b/python/modules/commands_builder.py index 29dc739f..c3cce2d9 100644 --- a/python/modules/commands_builder.py +++ b/python/modules/commands_builder.py @@ -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 diff --git a/python/patterns.py b/python/patterns.py index 1834c72c..515ecc58 100644 --- a/python/patterns.py +++ b/python/patterns.py @@ -19,7 +19,7 @@ r'\A\s*(карма|karma)\s*\Z', IGNORECASE) APPLY_KARMA = recompile( - r'\A(\[id(?\d+)\|@\w+\])?\s*(?P\+|\-)(?P[0-9]*)\s*\Z') + r'\A(\[id(?P\d+)\|@\w+\])?\s*(?P\+|\-)(?P[0-9]*)(?:\s+(?P.+?))?\s*\Z') ADD_PROGRAMMING_LANGUAGE = recompile( r'\A\s*\+=\s*(?P' + DEFAULT_LANGUAGES + r')\s*\Z', IGNORECASE) diff --git a/python/tests.py b/python/tests.py index 4be4241e..f539c9ca 100644 --- a/python/tests.py +++ b/python/tests.py @@ -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")