openedx.core.djangoapps.embargo package

Contents

openedx.core.djangoapps.embargo package#

Subpackages#

Submodules#

openedx.core.djangoapps.embargo.api module#

The Python API layer of the country access settings. Essentially the middle tier of the project, responsible for all business logic that is not directly tied to the data itself.

This API is exposed via the middleware(emabargo/middileware.py) layer but may be used directly in-process.

openedx.core.djangoapps.embargo.api.check_course_access(course_key: CourseKey, user: User | None = None, ip_addresses: List[str] | None = None, url: str | None = None) bool#

Check is the user with this ip_addresses chain has access to the given course

Parameters:
  • course_key – Location of the course the user is trying to access.

  • user – The user making the request. Can be None, in which case the user’s profile country will not be checked.

  • ip_addresses – The full external chain of IP addresses of the request.

  • url – The URL the user is trying to access. Used in log messages.

Returns:

True if the user has access to the course; False otherwise

openedx.core.djangoapps.embargo.api.get_embargo_response(request: Request, course_key: CourseKey, user: User) Response | None#

Check whether any country access rules block the user from enrollment.

Parameters:
  • request – The request object

  • course_key – The requested course ID

  • user – The current user object

Returns:

Response of the embargo page if embargoed, None if not

openedx.core.djangoapps.embargo.api.message_url_path(course_key: CourseKey, access_point: str) str#

Determine the URL path for the message explaining why the user was blocked.

This is configured per-course. See RestrictedCourse in the embargo.models module for more details.

Parameters:
  • course_key – The location of the course.

  • access_point – How the user was trying to access the course. Can be either “enrollment” or “courseware”.

Returns:

The URL path to a page explaining why the user was blocked.

Raises:

InvalidAccessPoint – Raised if access_point is not a supported value.

openedx.core.djangoapps.embargo.api.redirect_if_blocked(request: Request, course_key: CourseKey, access_point: str = 'enrollment', user: User | None = None) str | None#

Redirect if the user does not have access to the course.

Even if the user would normally be blocked, if the given access_point is ‘courseware’ and the course has enabled the is_disabled_access_check flag, then the user can still view that course.

Parameters:
  • request – The current request to be checked.

  • course_key – Location of the course the user is trying to access.

  • access_point – Type of page being accessed (e.g. ‘courseware’, ‘enrollment’, etc)

  • user – User to check for (uses request.user if None)

Returns:

If blocked, a URL path to a page explaining why the user was blocked. Else None.

openedx.core.djangoapps.embargo.exceptions module#

Exceptions for the embargo app.

exception openedx.core.djangoapps.embargo.exceptions.InvalidAccessPoint(access_point, *args, **kwargs)#

Bases: Exception

The requested access point is not supported.

openedx.core.djangoapps.embargo.forms module#

Defines forms for providing validation of embargo admin details.

class openedx.core.djangoapps.embargo.forms.IPFilterForm(data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=<class 'django.forms.utils.ErrorList'>, label_suffix=None, empty_permitted=False, instance=None, use_required_attribute=None, renderer=None)#

Bases: ModelForm

Form validating entry of IP addresses

class Meta#

Bases: object

fields = '__all__'#
model#

alias of IPFilter

base_fields = {'blacklist': <django.forms.fields.CharField object>, 'enabled': <django.forms.fields.BooleanField object>, 'whitelist': <django.forms.fields.CharField object>}#
clean_blacklist()#

Validates the blacklist

clean_whitelist()#

Validates the whitelist

declared_fields = {}#
property media#

Return all media required to render the widgets on this form.

class openedx.core.djangoapps.embargo.forms.RestrictedCourseForm(data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=<class 'django.forms.utils.ErrorList'>, label_suffix=None, empty_permitted=False, instance=None, use_required_attribute=None, renderer=None)#

Bases: ModelForm

Validate course keys for the RestrictedCourse model.

The default behavior in Django admin is to: * Save course keys for courses that do not exist. * Return a 500 response if the course key format is invalid.

Using this form ensures that we display a user-friendly error message instead.

class Meta#

Bases: object

fields = '__all__'#
model#

alias of RestrictedCourse

base_fields = {'access_msg_key': <django.forms.fields.TypedChoiceField object>, 'course_key': <django.forms.fields.CharField object>, 'disable_access_check': <django.forms.fields.BooleanField object>, 'enroll_msg_key': <django.forms.fields.TypedChoiceField object>}#
clean_course_key()#

Validate the course key.

Checks that the key format is valid and that the course exists. If not, displays an error message.

Parameters:

field_name (str) – The name of the field to validate.

Returns:

CourseKey

declared_fields = {}#
property media#

Return all media required to render the widgets on this form.

openedx.core.djangoapps.embargo.messages module#

Define messages for restricted courses.

These messages are displayed to users when they are blocked from either enrolling in or accessing a course.

class openedx.core.djangoapps.embargo.messages.BlockedMessage(description, template)#

Bases: tuple

description#

Alias for field number 0

template#

Alias for field number 1

openedx.core.djangoapps.embargo.middleware module#

Middleware for embargoing site and courses.

IMPORTANT NOTE: This code WILL NOT WORK if you have a misconfigured proxy server. If you are configuring embargo functionality, or if you are experiencing mysterious problems with embargoing, please check that your reverse proxy is setting any of the well known client IP address headers (ex., HTTP_X_FORWARDED_FOR).

This middleware allows you to:

  • Embargoing courses (access restriction by courses)

  • Embargoing site (access restriction of the main site)

Embargo can restrict by states and whitelist/blacklist (IP Addresses (ie. 10.0.0.0), Networks (ie. 10.0.0.0/24)), or the user profile country.

Usage:

  1. Enable embargo by setting settings.FEATURES[‘EMBARGO’] to True.

  2. In Django admin, create a new IPFilter model to block or whitelist

    an IP address from accessing the site.

  3. In Django admin, create a new RestrictedCourse model and

    configure a whitelist or blacklist of countries for that course.

class openedx.core.djangoapps.embargo.middleware.EmbargoMiddleware(*args, **kwargs)#

Bases: MiddlewareMixin

Middleware for embargoing site and courses.

ALLOW_URL_PATTERNS = [re.compile('^/embargo/blocked-message/'), re.compile('^/admin/')]#
country_access_rules(request: Request) Response | None#

Check the country access rules for a given course. Applies only to courseware URLs.

Parameters:

request – The request to validate against the embargo rules

Returns:

HttpResponse or None

process_request(request: Request) Response | None#

Block requests based on embargo rules.

This will perform the following checks:

  1. If the user’s IP address is blacklisted, block.

  2. If the user’s IP address is whitelisted, allow.

  3. If the user’s country (inferred from their IP address) is blocked for

    a courseware page, block.

  4. If the user’s country (retrieved from the user’s profile) is blocked

    for a courseware page, block.

  5. Allow access.

openedx.core.djangoapps.embargo.models module#

Models for embargoing visits to certain courses by IP address.

WE’RE USING MIGRATIONS!

If you make changes to this model, be sure to create an appropriate migration file and check it in at the same time as your model changes. To do that,

  1. Go to the edx-platform dir

  2. ./manage.py lms schemamigration embargo –auto description_of_your_change

  3. Add the migration file created in edx-platform/openedx/core/djangoapps/embargo/migrations/

class openedx.core.djangoapps.embargo.models.Country(*args, **kwargs)#

Bases: Model

Representation of a country.

This is used to define country-based access rules. There is a data migration that creates entries for each country code.

exception DoesNotExist#

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

country#

A descriptor for country fields on a model instance. Returns a Country when accessed so you can do things like:

>>> from people import Person
>>> person = Person.object.get(name='Chris')

>>> person.country.name
'New Zealand'

>>> person.country.flag
'/static/flags/nz.gif'
countryaccessrule_set#

Accessor to the related objects manager on the reverse side of a many-to-one relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Parent.children is a ReverseManyToOneDescriptor instance.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

get_country_display(*, field=<django_countries.fields.CountryField: country>)#
globalrestrictedcountry_set#

Accessor to the related objects manager on the reverse side of a many-to-one relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Parent.children is a ReverseManyToOneDescriptor instance.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

id#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>#
class openedx.core.djangoapps.embargo.models.CountryAccessRule(*args, **kwargs)#

Bases: Model

Course access rule based on the user’s country.

The rule applies to a particular course-country pair. Countries can either be whitelisted or blacklisted, but not both.

To determine whether a user has access to a course based on the user’s country:

1) Retrieve the list of whitelisted countries for the course. (If there aren’t any, then include every possible country.)

2) From the initial list, remove all blacklisted countries for the course.

ALL_COUNTRIES = {'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'XK', 'YE', 'YT', 'ZA', 'ZM', 'ZW'}#
BLACKLIST_RULE = 'blacklist'#
CACHE_KEY = 'embargo.allowed_countries.{course_key}'#
exception DoesNotExist#

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

RULE_TYPE_CHOICES = (('whitelist', 'Whitelist (allow only these countries)'), ('blacklist', 'Blacklist (block these countries)'))#
WHITELIST_RULE = 'whitelist'#
classmethod check_country_access(course_key: CourseKey, country: str) bool#

Check if the country is either in whitelist or blacklist of countries for the course_id

Parameters:
  • course_key – course to look for

  • country – A 2 characters code of country

Returns:

True if country found in allowed country, otherwise check given country exists in list

country#

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

country_id#
get_rule_type_display(*, field=<django.db.models.fields.CharField: rule_type>)#
id#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod invalidate_cache_for_course(course_key: CourseKey) None#

Invalidate the cache.

objects = <django.db.models.manager.Manager object>#
restricted_course#

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

restricted_course_id#
rule_type#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

class openedx.core.djangoapps.embargo.models.CourseAccessRuleHistory(*args, **kwargs)#

Bases: Model

History of course access rule changes.

DELETED_PLACEHOLDER = 'DELETED'#
exception DoesNotExist#

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

course_key#

DO NOT REUSE THIS CLASS. Provided for backwards compatibility only!

A placeholder class that provides a way to set the attribute on the model.

get_next_by_timestamp(*, field=<django.db.models.fields.DateTimeField: timestamp>, is_next=True, **kwargs)#
get_previous_by_timestamp(*, field=<django.db.models.fields.DateTimeField: timestamp>, is_next=False, **kwargs)#
id#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>#
classmethod save_snapshot(restricted_course, deleted=False)#

Save a snapshot of access rules for a course.

Parameters:

restricted_course (RestrictedCourse)

Keyword Arguments:

deleted (boolean) – If True, the restricted course is about to be deleted. Create a placeholder snapshot recording that the course and all its rules was deleted.

Returns:

None

snapshot#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

static snapshot_post_delete_receiver(sender, instance, **kwargs)#

Create a snapshot of course access rules when rules are deleted.

static snapshot_post_save_receiver(sender, instance, **kwargs)#

Create a snapshot of course access rules when the rules are updated.

timestamp#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

class openedx.core.djangoapps.embargo.models.EmbargoedCourse(*args, **kwargs)#

Bases: Model

Enable course embargo on a course-by-course basis.

Deprecated by RestrictedCourse

exception DoesNotExist#

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

course_id#

DO NOT REUSE THIS CLASS. Provided for backwards compatibility only!

A placeholder class that provides a way to set the attribute on the model.

embargoed#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

id#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod is_embargoed(course_id)#

Returns whether or not the given course id is embargoed.

If course has not been explicitly embargoed, returns False.

objects = <openedx.core.djangoapps.xmodule_django.models.NoneToEmptyManager object>#
class openedx.core.djangoapps.embargo.models.EmbargoedState(*args, **kwargs)#

Bases: ConfigurationModel

Register countries to be embargoed.

Deprecated by Country.

exception DoesNotExist#

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

change_date#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

changed_by#

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

changed_by_id#
embargoed_countries#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

property embargoed_countries_list#

Return a list of upper case country codes

enabled#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

get_next_by_change_date(*, field=<django.db.models.fields.DateTimeField: change_date>, is_next=True, **kwargs)#
get_previous_by_change_date(*, field=<django.db.models.fields.DateTimeField: change_date>, is_next=False, **kwargs)#
id#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

class openedx.core.djangoapps.embargo.models.GlobalRestrictedCountry(*args, **kwargs)#

Bases: Model

Model to restrict access to specific countries globally.

CACHE_KEY = 'embargo.global.restricted_countries'#
exception DoesNotExist#

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

country#

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

country_id#
delete(*args, **kwargs)#

Override delete method to update cache on deletion.

classmethod get_countries()#

Retrieve the set of restricted country codes from the cache or refresh it if not available.

Returns:

A set of restricted country codes.

Return type:

set

id#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod is_country_restricted(country_code)#

Check if the given country code is restricted.

Parameters:

country_code (str) – The country code to check.

Returns:

True if the country is restricted, False otherwise.

Return type:

bool

objects = <django.db.models.manager.Manager object>#
save(*args, **kwargs)#

Override save method to update cache on insert/update.

classmethod update_cache()#

Update the cache with the latest restricted country codes.

class openedx.core.djangoapps.embargo.models.IPFilter(*args, **kwargs)#

Bases: ConfigurationModel

Register specific IP addresses to explicitly block or unblock.

exception DoesNotExist#

Bases: ObjectDoesNotExist

class IPFilterList(ips)#

Bases: object

Represent a list of IP addresses with support of networks.

exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

blacklist#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

property blacklist_ips#

Return a list of valid IP addresses to blacklist

change_date#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

changed_by#

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

changed_by_id#
enabled#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

get_next_by_change_date(*, field=<django.db.models.fields.DateTimeField: change_date>, is_next=True, **kwargs)#
get_previous_by_change_date(*, field=<django.db.models.fields.DateTimeField: change_date>, is_next=False, **kwargs)#
id#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

whitelist#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

property whitelist_ips#

Return a list of valid IP addresses to whitelist

class openedx.core.djangoapps.embargo.models.RestrictedCourse(*args, **kwargs)#

Bases: Model

Course with access restrictions.

Restricted courses can block users at two points:

  1. When enrolling in a course.

  2. When attempting to access a course the user is already enrolled in.

The second case can occur when new restrictions are put into place; for example, when new countries are embargoed.

Restricted courses can be configured to display messages to users when they are blocked. These displayed on pages served by the embargo app.

COURSEWARE_MSG_KEY_CHOICES = (('default', 'Default'), ('embargo', 'Embargo'))#
COURSE_LIST_CACHE_KEY = 'embargo.restricted_courses'#
exception DoesNotExist#

Bases: ObjectDoesNotExist

ENROLL_MSG_KEY_CHOICES = (('default', 'Default'), ('embargo', 'Embargo'))#
MESSAGE_URL_CACHE_KEY = 'embargo.message_url_path.{access_point}.{course_key}'#
exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

access_msg_key#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

countryaccessrule_set#

Accessor to the related objects manager on the reverse side of a many-to-one relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Parent.children is a ReverseManyToOneDescriptor instance.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

course_key#

DO NOT REUSE THIS CLASS. Provided for backwards compatibility only!

A placeholder class that provides a way to set the attribute on the model.

disable_access_check#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

enroll_msg_key#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

get_access_msg_key_display(*, field=<django.db.models.fields.CharField: access_msg_key>)#
get_enroll_msg_key_display(*, field=<django.db.models.fields.CharField: enroll_msg_key>)#
id#

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod invalidate_cache_for_course(course_key: CourseKey) None#

Invalidate the caches for the restricted course.

classmethod is_disabled_access_check(course_key: CourseKey) bool#

Check if the course is in restricted list has disabled_access_check

Parameters:

course_key – course to look for

Returns:

disabled_access_check attribute of restricted course

classmethod is_restricted_course(course_key: CourseKey) bool#

Check if the course is in restricted list

Parameters:

course_key – course to look for

Returns:

True if the course is in the restricted course list.

message_key_for_access_point(access_point: str) str | None#

Determine which message to show the user.

The message can be configured per-course and depends on how the user is trying to access the course (trying to enroll or accessing courseware).

Parameters:

access_point – Either “courseware” or “enrollment”

Returns:

The message key. If the access point is not valid, returns None instead.

classmethod message_url_path(course_key: CourseKey, access_point: str) str#

Determine the URL path for the message explaining why the user was blocked.

This is configured per-course. See RestrictedCourse in the embargo.models module for more details.

Parameters:
  • course_key – The location of the course.

  • access_point – How the user was trying to access the course. Can be either “enrollment” or “courseware”.

Returns:

The URL path to a page explaining why the user was blocked.

Raises:

InvalidAccessPoint – Raised if access_point is not a supported value.

objects = <django.db.models.manager.Manager object>#
snapshot()#

Return a snapshot of all access rules for this course.

This is useful for recording an audit trail of rule changes. The returned dictionary is JSON-serializable.

Returns:

dict

Example Usage: >>> restricted_course.snapshot() {

‘enroll_msg’: ‘default’, ‘access_msg’: ‘default’, ‘country_rules’: [

{‘country’: ‘IR’, ‘rule_type’: ‘blacklist’}, {‘country’: ‘CU’, ‘rule_type’: ‘blacklist’}

]

}

openedx.core.djangoapps.embargo.models.invalidate_country_rule_cache(sender, instance, **kwargs)#

Invalidate cached rule information on changes to the rule models.

We need to handle this in a Django receiver, because Django admin doesn’t always call the model’s delete() method directly during a bulk delete operation.

Parameters:

openedx.core.djangoapps.embargo.test_utils module#

Utilities for writing unit tests that involve course embargos.

openedx.core.djangoapps.embargo.test_utils.restrict_course(course_key, access_point='enrollment', disable_access_check=False)#

Simulate that a course is restricted.

This does two things: 1) Configures country access rules so that the course is restricted. 2) Mocks the GeoIP call so the user appears to be coming

from a country that’s blocked from the course.

This is useful for tests that need to verify that restricted users won’t be able to access particular views.

Parameters:

course_key (CourseKey) – The location of the course to block.

Keyword Arguments:

access_point (str) – Either “courseware” or “enrollment”

Yields:

str

A URL to the page in the embargo app that explains

why the user was blocked.

Example Usage: >>> with restrict_course(course_key) as redirect_url: >>> # The client will appear to be coming from >>> # an IP address that is blocked. >>> resp = self.client.get(url) >>> self.assertRedirects(resp, redirect_url)

openedx.core.djangoapps.embargo.urls module#

URLs served by the embargo app.

openedx.core.djangoapps.embargo.views module#

Views served by the embargo app.

class openedx.core.djangoapps.embargo.views.CheckCourseAccessView(**kwargs)#

Bases: APIView

get(request)#

GET /api/embargo/v1/course_access/

Parameters:

request (HttpRequest)

Returns:

True or False depending on the check.

Return type:

Response

permission_classes = (<class 'rest_framework.permissions.IsAuthenticated'>, <class 'rest_framework.permissions.IsAdminUser'>)#
class openedx.core.djangoapps.embargo.views.CourseAccessMessageView(**kwargs)#

Bases: View

Show a message explaining that the user was blocked from a course.

COURSEWARE_ACCESS_POINT = 'courseware'#
ENROLLMENT_ACCESS_POINT = 'enrollment'#
get(request, access_point=None, message_key=None)#

Show a message explaining that the user was blocked.

Parameters:

request (HttpRequest)

Keyword Arguments:
  • access_point (str) – Either ‘enrollment’ or ‘courseware’, indicating how the user is trying to access the restricted content.

  • message_key (str) – An identifier for which message to show. See embargo.messages for more information.

Returns:

HttpResponse

Raises:

Http404 – If no message is configured for the specified message key.

Module contents#