Skip to content
Merged
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
19 changes: 14 additions & 5 deletions django_project/minisass_authentication/admin/minisass.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
PasswordHistory,
CountryMapping
)
from monitor.models import Sites


@admin.register(Lookup)
Expand Down Expand Up @@ -44,13 +45,21 @@ def correct_country(modeladmin, request, queryset):
}

for user in queryset:
if user.userprofile:
site = Sites.objects.filter(user=user).first()
try:
userprofile = UserProfile.objects.get(user=user)
except UserProfile.DoesNotExist:
userprofile = UserProfile.objects.create(
user=user,
country=site.country if site else None,
)
if userprofile:
country = country_mappings.get(
user.userprofile.country,
user.userprofile.country
userprofile.country,
userprofile.country
)
user.userprofile.country = country
user.userprofile.save()
userprofile.country = country
userprofile.save()
correct_country.short_description = "Correct Country"


Expand Down
13 changes: 13 additions & 0 deletions django_project/minisass_authentication/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.test import TestCase
from django.contrib.gis.geos import Point
from django.contrib.auth.models import User
from django.contrib.admin.sites import AdminSite
from minisass_authentication.admin import UserAdmin, correct_country
from minisass_authentication.models import UserProfile, CountryMapping
from monitor.models import Sites


class CorrectCountryAdminActionTest(TestCase):
Expand All @@ -25,6 +27,16 @@ def setUp(self):
user.userprofile.country = '999999999'
user.userprofile.save()
self.users.append((user, '999999999'))
user = User.objects.create(username=f"user_without_profile")
user.userprofile.delete()
site = Sites.objects.create(
site_name='Test Site',
river_name='Test River',
the_geom=Point(-7.713518, 110.009160),
user=user,
country='ID'
)
self.users.append((user, site.country))

def test_correct_country_bulk(self):
queryset = User.objects.filter(pk__in=[u.pk for u, _ in self.users])
Expand All @@ -34,5 +46,6 @@ def test_correct_country_bulk(self):

for user, expected_country in self.users:
with self.subTest(user=user.username):
user.refresh_from_db()
user.userprofile.refresh_from_db()
self.assertEqual(user.userprofile.country, expected_country)
14 changes: 11 additions & 3 deletions django_project/monitor/management/commands/fix_countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings
from django.contrib.auth.models import User
from monitor.models import Sites
from minisass_authentication.models import UserProfile
from minisass_authentication.admin.minisass import correct_country

logger = logging.getLogger(__name__)
Expand All @@ -27,9 +28,16 @@ def handle(self, *args, **kwargs):
except Exception as e:
logger.error('Error fixing Site %s: %s', site.gid, e)
# set user country if empty
if not site.user.userprofile.country:
site.user.userprofile.country = site.country
site.user.userprofile.save()
try:
user_profile = UserProfile.objects.get(user=site.user)
except UserProfile.DoesNotExist:
user_profile = UserProfile.objects.create(user=site.user)
try:
if not user_profile.country:
user_profile.country = site.country
user_profile.save()
except Exception as e:
logger.error('Error fixing User country %s: %s', site.user, e)
logger.info('Site %s fixed successfully', site.gid)

correct_country(None, None, User.objects.all().filter(email='[email protected]'))
Expand Down
53 changes: 53 additions & 0 deletions django_project/monitor/tests/test_commands/test_fix_countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,59 @@ def test_fix_countries_command_updates_user_profile_without_country(self, mock_s
# Assert user profile country is updated
self.assertEquals(non_expert_profile.country, 'ZA')

@patch('monitor.utils.requests.get')
@patch('time.sleep')
def test_fix_countries_command_updates_user_without_profile(self, mock_sleep, mock_get):
# Create a user
non_expert_user = User.objects.create_user(
username='nonexpert',
password='testpassword',
email='[email protected]'
)
non_expert_user.userprofile.delete()

# Create site for non-expert user
site_non_expert = Sites.objects.create(
site_name='Non Expert Site',
river_name='Non Expert River',
the_geom=Point(30.0, -25.0),
user=non_expert_user,
country=None
)

# Mock the API response
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"ISO_A2": "ZA"},
}
],
"totalFeatures": 1,
"numberMatched": 1,
"numberReturned": 1,
"timeStamp": "2025-07-08T03:17:38.103Z",
"crs": None
}
mock_get.return_value = mock_response

with override_settings(ENABLE_GEOCODING=True):
# Run the command
call_command('fix_countries')

# Refresh objects
site_non_expert.refresh_from_db()
non_expert_user.refresh_from_db()

# Assert site country is set
self.assertEqual(site_non_expert.country, 'ZA')

# Assert user profile country is updated
self.assertEquals(non_expert_user.userprofile.country, 'ZA')

@patch('monitor.utils.requests.get')
@patch('time.sleep')
def test_fix_countries_command_not_update_user_profile_with_country(self, mock_sleep, mock_get):
Expand Down