Skip to content

Commit

Permalink
Clean code in interactive app
Browse files Browse the repository at this point in the history
  • Loading branch information
istride committed Jul 15, 2024
1 parent b3e3e88 commit 988562d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
27 changes: 6 additions & 21 deletions interactive/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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<bg_color>[^"]+)"(\])?'

# 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)
Expand All @@ -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)
2 changes: 1 addition & 1 deletion interactive/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def button_callback(self, attrs, content):

def form_callback(self, attrs, content):
if attrs["type"] == "input":
return f'<input class="text-input" type="text" name="text">'
return '<input class="text-input" type="text" name="text">'

return ""

Expand Down
9 changes: 6 additions & 3 deletions interactive/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@
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):

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",
Expand Down

0 comments on commit 988562d

Please sign in to comment.