Skip to content

Commit e7100d8

Browse files
Revert "feat: refactor country disable logic into the Embargo app (#36202)" (#36393)
This reverts commit 72959ad.
1 parent f700c89 commit e7100d8

File tree

10 files changed

+62
-212
lines changed

10 files changed

+62
-212
lines changed

cms/envs/common.py

+7
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,13 @@ def _should_send_learning_badge_events(settings):
29192919
MEILISEARCH_INDEX_PREFIX = ""
29202920
MEILISEARCH_API_KEY = "devkey"
29212921

2922+
# .. setting_name: DISABLED_COUNTRIES
2923+
# .. setting_default: []
2924+
# .. setting_description: List of country codes that should be disabled
2925+
# .. for now it wil impact country listing in auth flow and user profile.
2926+
# .. eg ['US', 'CA']
2927+
DISABLED_COUNTRIES = []
2928+
29222929
# .. setting_name: LIBRARY_ENABLED_BLOCKS
29232930
# .. setting_default: ['problem', 'video', 'html', 'drag-and-drop-v2']
29242931
# .. setting_description: List of block types that are ready/enabled to be created/used

lms/envs/common.py

+9
Original file line numberDiff line numberDiff line change
@@ -5549,6 +5549,15 @@ def _should_send_learning_badge_events(settings):
55495549
# .. setting_description: Dictionary with additional information that you want to share in the report.
55505550
SURVEY_REPORT_EXTRA_DATA = {}
55515551

5552+
5553+
# .. setting_name: DISABLED_COUNTRIES
5554+
# .. setting_default: []
5555+
# .. setting_description: List of country codes that should be disabled
5556+
# .. for now it wil impact country listing in auth flow and user profile.
5557+
# .. eg ['US', 'CA']
5558+
DISABLED_COUNTRIES = []
5559+
5560+
55525561
LMS_COMM_DEFAULT_FROM_EMAIL = "[email protected]"
55535562

55545563

openedx/core/djangoapps/embargo/admin.py

+1-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.contrib import admin
99

1010
from .forms import IPFilterForm, RestrictedCourseForm
11-
from .models import CountryAccessRule, GlobalRestrictedCountry, IPFilter, RestrictedCourse
11+
from .models import CountryAccessRule, IPFilter, RestrictedCourse
1212

1313

1414
class IPFilterAdmin(ConfigurationModelAdmin):
@@ -41,20 +41,5 @@ class RestrictedCourseAdmin(admin.ModelAdmin):
4141
search_fields = ('course_key',)
4242

4343

44-
class GlobalRestrictedCountryAdmin(admin.ModelAdmin):
45-
"""
46-
Admin configuration for the Global Country Restriction model.
47-
"""
48-
list_display = ("country",)
49-
50-
def delete_queryset(self, request, queryset):
51-
"""
52-
Override the delete_queryset method to clear the cache when objects are deleted in bulk.
53-
"""
54-
super().delete_queryset(request, queryset)
55-
GlobalRestrictedCountry.update_cache()
56-
57-
5844
admin.site.register(IPFilter, IPFilterAdmin)
5945
admin.site.register(RestrictedCourse, RestrictedCourseAdmin)
60-
admin.site.register(GlobalRestrictedCountry, GlobalRestrictedCountryAdmin)

openedx/core/djangoapps/embargo/migrations/0003_add_global_restricted_country_model.py

-25
This file was deleted.

openedx/core/djangoapps/embargo/models.py

-75
Original file line numberDiff line numberDiff line change
@@ -662,81 +662,6 @@ class Meta:
662662
get_latest_by = 'timestamp'
663663

664664

665-
class GlobalRestrictedCountry(models.Model):
666-
"""
667-
Model to restrict access to specific countries globally.
668-
"""
669-
country = models.ForeignKey(
670-
"Country",
671-
help_text="The country to be restricted.",
672-
on_delete=models.CASCADE,
673-
unique=True
674-
)
675-
676-
CACHE_KEY = "embargo.global.restricted_countries"
677-
678-
@classmethod
679-
def get_countries(cls):
680-
"""
681-
Retrieve the set of restricted country codes from the cache or refresh it if not available.
682-
683-
Returns:
684-
set: A set of restricted country codes.
685-
"""
686-
return cache.get_or_set(cls.CACHE_KEY, cls._fetch_restricted_countries)
687-
688-
@classmethod
689-
def is_country_restricted(cls, country_code):
690-
"""
691-
Check if the given country code is restricted.
692-
693-
Args:
694-
country_code (str): The country code to check.
695-
696-
Returns:
697-
bool: True if the country is restricted, False otherwise.
698-
"""
699-
return country_code in cls.get_countries()
700-
701-
@classmethod
702-
def _fetch_restricted_countries(cls):
703-
"""
704-
Fetch the set of restricted country codes from the database.
705-
706-
Returns:
707-
set: A set of restricted country codes.
708-
"""
709-
return set(cls.objects.values_list("country__country", flat=True))
710-
711-
@classmethod
712-
def update_cache(cls):
713-
"""
714-
Update the cache with the latest restricted country codes.
715-
"""
716-
cache.set(cls.CACHE_KEY, cls._fetch_restricted_countries())
717-
718-
def save(self, *args, **kwargs):
719-
"""
720-
Override save method to update cache on insert/update.
721-
"""
722-
super().save(*args, **kwargs)
723-
self.update_cache()
724-
725-
def delete(self, *args, **kwargs):
726-
"""
727-
Override delete method to update cache on deletion.
728-
"""
729-
super().delete(*args, **kwargs)
730-
self.update_cache()
731-
732-
def __str__(self):
733-
return f"{self.country.country.name} ({self.country.country})"
734-
735-
class Meta:
736-
verbose_name = "Global Restricted Country"
737-
verbose_name_plural = "Global Restricted Countries"
738-
739-
740665
# Connect the signals to the receivers so we record a history
741666
# of changes to the course access rules.
742667
post_save.connect(CourseAccessRuleHistory.snapshot_post_save_receiver, sender=RestrictedCourse)

openedx/core/djangoapps/user_api/accounts/api.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
from django.conf import settings
1010
from django.core.exceptions import ObjectDoesNotExist
1111
from django.core.validators import ValidationError, validate_email
12-
from django.utils.translation import gettext as _
1312
from django.utils.translation import override as override_language
13+
from django.utils.translation import gettext as _
1414
from eventtracking import tracker
1515
from pytz import UTC
16-
1716
from common.djangoapps.student import views as student_views
1817
from common.djangoapps.student.models import (
1918
AccountRecovery,
@@ -26,7 +25,7 @@
2625
from common.djangoapps.util.password_policy_validators import validate_password
2726
from lms.djangoapps.certificates.api import get_certificates_for_user
2827
from lms.djangoapps.certificates.data import CertificateStatuses
29-
from openedx.core.djangoapps.embargo.models import GlobalRestrictedCountry
28+
3029
from openedx.core.djangoapps.enrollments.api import get_verified_enrollments
3130
from openedx.core.djangoapps.user_api import accounts, errors, helpers
3231
from openedx.core.djangoapps.user_api.errors import (
@@ -40,7 +39,6 @@
4039
from openedx.core.lib.api.view_utils import add_serializer_errors
4140
from openedx.features.enterprise_support.utils import get_enterprise_readonly_account_fields
4241
from openedx.features.name_affirmation_api.utils import is_name_affirmation_installed
43-
4442
from .serializers import AccountLegacyProfileSerializer, AccountUserSerializer, UserReadOnlySerializer, _visible_fields
4543

4644
name_affirmation_installed = is_name_affirmation_installed()
@@ -153,10 +151,7 @@ def update_account_settings(requesting_user, update, username=None):
153151

154152
_validate_email_change(user, update, field_errors)
155153
_validate_secondary_email(user, update, field_errors)
156-
if (
157-
settings.FEATURES.get('EMBARGO', False) and
158-
GlobalRestrictedCountry.is_country_restricted(update.get('country', ''))
159-
):
154+
if update.get('country', '') in settings.DISABLED_COUNTRIES:
160155
field_errors['country'] = {
161156
'developer_message': 'Country is disabled for registration',
162157
'user_message': 'This country cannot be selected for user registration'

openedx/core/djangoapps/user_api/accounts/tests/test_api.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
import itertools
88
import unicodedata
99
from unittest.mock import Mock, patch
10-
11-
import ddt
1210
import pytest
11+
import ddt
1312
from django.conf import settings
1413
from django.contrib.auth.hashers import make_password
1514
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
1615
from django.http import HttpResponse
1716
from django.test import TestCase
1817
from django.test.client import RequestFactory
18+
from django.test.utils import override_settings
1919
from django.urls import reverse
2020
from pytz import UTC
2121
from social_django.models import UserSocialAuth
22-
2322
from common.djangoapps.student.models import (
2423
AccountRecovery,
2524
PendingEmailChange,
@@ -29,14 +28,14 @@
2928
from common.djangoapps.student.tests.factories import UserFactory
3029
from common.djangoapps.student.tests.tests import UserSettingsEventTestMixin
3130
from common.djangoapps.student.views.management import activate_secondary_email
31+
3232
from lms.djangoapps.certificates.data import CertificateStatuses
3333
from openedx.core.djangoapps.ace_common.tests.mixins import EmailTemplateTagMixin
34-
from openedx.core.djangoapps.embargo.models import Country, GlobalRestrictedCountry
3534
from openedx.core.djangoapps.user_api.accounts import PRIVATE_VISIBILITY
3635
from openedx.core.djangoapps.user_api.accounts.api import (
3736
get_account_settings,
38-
get_name_validation_error,
39-
update_account_settings
37+
update_account_settings,
38+
get_name_validation_error
4039
)
4140
from openedx.core.djangoapps.user_api.accounts.tests.retirement_helpers import ( # pylint: disable=unused-import
4241
RetirementTestCase,
@@ -575,14 +574,12 @@ def test_change_country_removes_state(self):
575574
assert account_settings['country'] is None
576575
assert account_settings['state'] is None
577576

577+
@override_settings(DISABLED_COUNTRIES=['KP'])
578578
def test_change_to_disabled_country(self):
579579
"""
580580
Test that changing the country to a disabled country is not allowed
581581
"""
582582
# First set the country and state
583-
country = Country.objects.create(country="KP")
584-
GlobalRestrictedCountry.objects.create(country=country)
585-
586583
update_account_settings(self.user, {"country": UserProfile.COUNTRY_WITH_STATES, "state": "MA"})
587584
account_settings = get_account_settings(self.default_request)[0]
588585
assert account_settings['country'] == UserProfile.COUNTRY_WITH_STATES

openedx/core/djangoapps/user_authn/views/registration_form.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"""
44

55
import copy
6-
import re
76
from importlib import import_module
7+
from eventtracking import tracker
8+
import re
89

910
from django import forms
1011
from django.conf import settings
@@ -15,25 +16,26 @@
1516
from django.urls import reverse
1617
from django.utils.translation import gettext as _
1718
from django_countries import countries
18-
from eventtracking import tracker
1919

2020
from common.djangoapps import third_party_auth
2121
from common.djangoapps.edxmako.shortcuts import marketing_link
22-
from common.djangoapps.student.models import CourseEnrollmentAllowed, UserProfile, email_exists_or_retired
23-
from common.djangoapps.util.password_policy_validators import (
24-
password_validators_instruction_texts,
25-
password_validators_restrictions,
26-
validate_password
27-
)
28-
from openedx.core.djangoapps.embargo.models import GlobalRestrictedCountry
2922
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
3023
from openedx.core.djangoapps.user_api import accounts
3124
from openedx.core.djangoapps.user_api.helpers import FormDescription
32-
from openedx.core.djangoapps.user_authn.utils import check_pwned_password
33-
from openedx.core.djangoapps.user_authn.utils import is_registration_api_v1 as is_api_v1
25+
from openedx.core.djangoapps.user_authn.utils import check_pwned_password, is_registration_api_v1 as is_api_v1
3426
from openedx.core.djangoapps.user_authn.views.utils import remove_disabled_country_from_list
3527
from openedx.core.djangolib.markup import HTML, Text
3628
from openedx.features.enterprise_support.api import enterprise_customer_for_request
29+
from common.djangoapps.student.models import (
30+
CourseEnrollmentAllowed,
31+
UserProfile,
32+
email_exists_or_retired,
33+
)
34+
from common.djangoapps.util.password_policy_validators import (
35+
password_validators_instruction_texts,
36+
password_validators_restrictions,
37+
validate_password,
38+
)
3739

3840

3941
class TrueCheckbox(widgets.CheckboxInput):
@@ -304,10 +306,7 @@ def clean_country(self):
304306
Check if the user's country is in the embargoed countries list.
305307
"""
306308
country = self.cleaned_data.get("country")
307-
if (
308-
settings.FEATURES.get('EMBARGO', False) and
309-
country in GlobalRestrictedCountry.get_countries()
310-
):
309+
if country in settings.DISABLED_COUNTRIES:
311310
raise ValidationError(_("Registration from this country is not allowed due to restrictions."))
312311
return self.cleaned_data.get("country")
313312

@@ -982,6 +981,7 @@ def _add_country_field(self, form_desc, required=True):
982981
'country',
983982
default=default_country.upper()
984983
)
984+
985985
form_desc.add_field(
986986
"country",
987987
label=country_label,

0 commit comments

Comments
 (0)