diff --git a/interactive/models.py b/interactive/models.py index ad6cca69f..003c2bd4e 100644 --- a/interactive/models.py +++ b/interactive/models.py @@ -83,7 +83,7 @@ def serve(self, request, *args, **kwargs): try: response = requests.post(url=channel_url, data=data) response.raise_for_status() - except requests.exceptions.RequestException as e: + except requests.exceptions.RequestException: return redirect("/") return redirect(self.get_url(request)) @@ -105,11 +105,8 @@ def get_user_identifier(self, request): request.session.save() user_uuid = request.session.setdefault("interactive_uuid", str(uuid.uuid4())) - - # Get the authenticated user user = request.user - # If the user is authenticated and has no 'interactive_uuid' set, update it if user.is_authenticated: if user.interactive_uuid: user_uuid = user.interactive_uuid @@ -121,16 +118,15 @@ def get_user_identifier(self, request): return user_uuid def get_message_from_db(self, user): - # wait a second to receive new message from rapidpro + # Wait to receive new message from rapidpro time.sleep(1) start_time = time.time() while True: - # Calculate the elapsed time + # Elapsed time in seconds elapsed_time = time.time() - start_time - # Break the loop if 5 seconds have elapsed if elapsed_time >= 5: break @@ -140,26 +136,16 @@ def get_message_from_db(self, user): text = chat.text.strip() - # Check if the message has a next message indicator if text.endswith("[CONTINUE]"): time.sleep(1) else: - break # Exit the loop if the message does not end with '[CONTINUE]' + break shortcode_service = ShortCodeService() text = shortcode_service.apply_shortcode(text) - - # Define the regular expression pattern pattern = r'\[color_scheme\s+bg-color="(?P[^"]+)"(\])?' - - # Search for matches in the input string match = re.search(pattern, text) - - bg_color = "" - # Check if a match is found - if match: - # Extract the bg_color attributes - bg_color = match.group("bg_color") + bg_color = match.group("bg_color") if match else "" # Remove the [bg_color] tag from the input string text = re.sub(pattern, "", text) @@ -180,5 +166,4 @@ def send_message_on_language_switch(self, request, user): if referer_lang != current_lang: data = {"from": user, "text": self.trigger_string} - - response = requests.post(url=self.channel.request_url, data=data) + requests.post(url=self.channel.request_url, data=data) diff --git a/interactive/services.py b/interactive/services.py index 9de34e84e..4047b81db 100644 --- a/interactive/services.py +++ b/interactive/services.py @@ -32,7 +32,7 @@ def button_callback(self, attrs, content): def form_callback(self, attrs, content): if attrs["type"] == "input": - return f'' + return '' return "" diff --git a/interactive/tests/tests.py b/interactive/tests/tests.py index 006adc699..6c3f03b37 100644 --- a/interactive/tests/tests.py +++ b/interactive/tests/tests.py @@ -9,15 +9,15 @@ from interactive.models import Message -# Create your tests here. class RapidProWebhookTest(APITestCase): def setUp(self) -> None: username = uuid4() management.call_command("sync_rapidpro_bot_user", username=username) self.bot_user = User.objects.get(username=username) self.client = APIClient() + token = RefreshToken.for_user(self.bot_user).access_token self.client.credentials( - HTTP_AUTHORIZATION=f"Bearer {RefreshToken.for_user(self.bot_user).access_token}" + HTTP_AUTHORIZATION=f"Bearer {token}" ) def test_webhook_stitches_messages(self): @@ -25,7 +25,10 @@ def test_webhook_stitches_messages(self): rapidpro_data_list = [ { "id": "1", - "text": "Some message with the first part of text. First part ends at the exclamation mark!", + "text": ( + "Some message with the first part of text. First part ends at the" + " exclamation mark!" + ), "to": "bd3577c6-65b1-4bb7-9611-306c11b1dcc5", "from": "abcd", "channel": "bd3577c6-65b1-4bb7-9611-306c11b1dcc5",