Skip to content

Commit

Permalink
user limitation fixes (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
marslanabdulrauf authored Dec 13, 2022
1 parent 4c0153f commit b1a5a5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
6 changes: 3 additions & 3 deletions openedx/core/djangoapps/user_authn/views/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions openedx/features/edly/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
"""
Expand Down
45 changes: 21 additions & 24 deletions openedx/features/edly/validators.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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__)

Expand Down Expand Up @@ -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

0 comments on commit b1a5a5d

Please sign in to comment.