Skip to content

Commit

Permalink
fix: default country code
Browse files Browse the repository at this point in the history
  • Loading branch information
shahharsh176 committed Sep 11, 2024
1 parent 83884bc commit 4f1d5d0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
15 changes: 11 additions & 4 deletions backend/src/zango/api/platform/tenancy/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import traceback

from django_celery_results.models import TaskResult
from phonenumbers.phonenumberutil import country_code_for_region

from django.conf import settings
from django.db.models import Q
from django.utils.decorators import method_decorator

Expand All @@ -19,7 +17,11 @@
from zango.core.api.utils import ZangoAPIPagination
from zango.core.common_utils import set_app_schema_path
from zango.core.permissions import IsPlatformUserAllowedApp
from zango.core.utils import get_search_columns, validate_phone
from zango.core.utils import (
get_country_code_for_tenant,
get_search_columns,
validate_phone,
)

from .serializers import (
AppUserModelSerializerModel,
Expand Down Expand Up @@ -342,6 +344,10 @@ class UserViewAPIV1(ZangoGenericPlatformAPIView, ZangoAPIPagination):
pagination_class = ZangoAPIPagination
permission_classes = (IsPlatformUserAllowedApp,)

def get_app_tenant(self):
tenant_obj = TenantModel.objects.get(uuid=self.kwargs["app_uuid"])
return tenant_obj

def get_dropdown_options(self):
options = {}
options["roles"] = [
Expand Down Expand Up @@ -388,11 +394,12 @@ def get(self, request, *args, **kwargs):
app_users = self.paginate_queryset(app_users, request, view=self)
serializer = AppUserModelSerializerModel(app_users, many=True)
app_users_data = self.get_paginated_response_data(serializer.data)
app_tenant = self.get_app_tenant()
success = True
response = {
"users": app_users_data,
"message": "Users fetched successfully",
"pn_country_code": f"+{country_code_for_region(settings.PHONENUMBER_DEFAULT_REGION)}",
"pn_country_code": get_country_code_for_tenant(app_tenant),
}
if include_dropdown_options:
response["dropdown_options"] = self.get_dropdown_options()
Expand Down
28 changes: 28 additions & 0 deletions backend/src/zango/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import phonenumbers
import pytz

from phonenumbers.phonenumberutil import country_code_for_region

from django.conf import settings
from django.db import connection
from django.shortcuts import render
Expand Down Expand Up @@ -154,3 +156,29 @@ def get_region_from_timezone(tzname):
for tz in timezones:
timezone_country[tz] = countrycode
return timezone_country[tzname]


def get_country_code_for_tenant(tenant, with_plus_sign=True):
"""
Returns the country code for the given tenant.
The region is first determined from the tenant's timezone. If no timezone is set,
the default region from `settings.PHONENUMBER_DEFAULT_REGION` is used.
Args:
tenant: A TenantModel instance.
with_plus_sign (bool): Whether to prepend a "+" to the country code. Default is True.
Returns:
str: The country code with or without "+" based on the region (e.g., "+1" for "US", "+91" for "IN").
"""
default_region = settings.PHONENUMBER_DEFAULT_REGION

if tenant.timezone:
try:
default_region = get_region_from_timezone(tenant.timezone)
except Exception:
pass

country_code = country_code_for_region(default_region)
return f"+{country_code}" if with_plus_sign else country_code

0 comments on commit 4f1d5d0

Please sign in to comment.