From b1a5a5df075cd209d4e36809d23d77d3c519b573 Mon Sep 17 00:00:00 2001 From: Muhammad Arslan Date: Tue, 13 Dec 2022 18:01:07 +0500 Subject: [PATCH] user limitation fixes (#343) --- .../djangoapps/user_authn/views/register.py | 6 +-- openedx/features/edly/tests/test_views.py | 7 ++- openedx/features/edly/validators.py | 45 +++++++++---------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/openedx/core/djangoapps/user_authn/views/register.py b/openedx/core/djangoapps/user_authn/views/register.py index 9e0b1414dc23..511597ab970b 100644 --- a/openedx/core/djangoapps/user_authn/views/register.py +++ b/openedx/core/djangoapps/user_authn/views/register.py @@ -57,7 +57,7 @@ RegistrationFormFactory ) from openedx.core.djangoapps.waffle_utils import WaffleFlag, WaffleFlagNamespace -from openedx.features.edly.validators import is_registered_user_limit_reached_for_plan +from openedx.features.edly.validators import get_subscription_limit, handle_subscription_limit from openedx.features.edly.utils import ( create_learner_link_with_permission_groups, create_user_link_with_edly_sub_organization, @@ -489,8 +489,8 @@ def post(self, request): """ data = request.POST.copy() self._handle_terms_of_service(data) - - errors = is_registered_user_limit_reached_for_plan(request) + remaining_limit = get_subscription_limit(request.site.edly_sub_org_for_lms) + errors = handle_subscription_limit(remaining_limit) if errors: response = self._create_response(request, errors, status_code=403) return response diff --git a/openedx/features/edly/tests/test_views.py b/openedx/features/edly/tests/test_views.py index 41b350ac19e4..a7d436583ea4 100644 --- a/openedx/features/edly/tests/test_views.py +++ b/openedx/features/edly/tests/test_views.py @@ -5,7 +5,9 @@ from django.test import TestCase from django.urls import reverse -from openedx.features.edly.tests.factories import EdlySubOrganizationFactory, SiteFactory +from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory +from openedx.features.edly.tests.factories import EdlySubOrganizationFactory + class EdlyUserRegistrationTests(TestCase): @@ -19,7 +21,8 @@ def setUp(self): """ super(EdlyUserRegistrationTests, self).setUp() self.url = reverse('user_api_registration') - self.site = SiteFactory() + self.site_config = SiteConfigurationFactory() + self.site = self.site_config.site def test_edly_profile_creation_with_user_registration(self): """ diff --git a/openedx/features/edly/validators.py b/openedx/features/edly/validators.py index 78454ec80ada..bda8dea31940 100644 --- a/openedx/features/edly/validators.py +++ b/openedx/features/edly/validators.py @@ -1,7 +1,6 @@ from logging import getLogger from django.conf import settings -from django.db.models import Q from django.utils.translation import ugettext as _ from openedx.core.djangoapps.content.course_overviews.models import CourseOverview @@ -19,7 +18,6 @@ create_user_link_with_edly_sub_organization, user_can_login_on_requested_edly_organization ) -from student.models import User logger = getLogger(__name__) @@ -98,30 +96,29 @@ def is_courses_limit_reached_for_plan(): return False -def is_registered_user_limit_reached_for_plan(request): +def get_subscription_limit(edly_sub_org): """ Checks if the limit for the current site for number of registered users is reached. """ - errors = {} - try: - site_config = configuration_helpers.get_current_site_configuration() - current_plan = site_config.get_value('DJANGO_SETTINGS_OVERRIDE').get('CURRENT_PLAN', ESSENTIALS) - plan_features = settings.PLAN_FEATURES.get(current_plan) - - user_records_count = User.objects.select_related('profile').select_related('edly_profile').exclude( - groups__name=settings.ADMIN_CONFIGURATION_USERS_GROUP - ).filter( - Q(edly_profile__edly_sub_organizations=request.site.edly_sub_org_for_lms) - ).count() - - if user_records_count >= plan_features.get(NUMBER_OF_REGISTERED_USERS): - errors['email'] = [{"user_message": _( - u"The maximum courses limit for your plan has reached. " - u"Please upgrade your plan." - )}] - return errors + site_config = configuration_helpers.get_current_site_configuration() + current_plan = site_config.get_value('DJANGO_SETTINGS_OVERRIDE', {}).get('CURRENT_PLAN', ESSENTIALS) + plan_features = settings.PLAN_FEATURES.get(current_plan) + registration_limit = plan_features.get(NUMBER_OF_REGISTERED_USERS) - return False + user_records_count = EdlyUserProfile.objects.filter(edly_sub_organizations=edly_sub_org).count() - except AttributeError: - return False + return registration_limit - user_records_count + + +def handle_subscription_limit(remaining_limit): + """ + Returns appropriate errors if the limit has reached. + """ + errors = {} + if remaining_limit <= 0: + errors['email'] = [{"user_message": _( + u"The maximum users limit for your plan has reached. " + u"Please upgrade your plan." + )}] + + return errors