openedx.core.djangolib package#

Subpackages#

Submodules#

openedx.core.djangolib.default_auth_classes module#

Default Authentication classes that are ONLY meant to be used by DEFAULT_AUTHENTICATION_CLASSES for observability purposes.

class openedx.core.djangolib.default_auth_classes.DefaultJwtAuthentication#

Bases: JwtAuthentication

Default JwtAuthentication with observability

Note that the plan is to add JwtAuthentication as a default, but it is not yet used. This class will be used during the transition.

authenticate(request)#

Returns a two-tuple of User and token if a valid signature has been supplied using JWT-based authentication. Otherwise returns None.

class openedx.core.djangolib.default_auth_classes.DefaultSessionAuthentication#

Bases: SessionAuthentication

Default SessionAuthentication with observability

authenticate(request)#

Returns a User if the request session currently has a logged in user. Otherwise returns None.

openedx.core.djangolib.fields module#

Custom Django fields.

class openedx.core.djangolib.fields.BigAutoField(*args, **kwargs)#

Bases: AutoField

AutoField that uses BigIntegers.

This exists in Django as of version 1.10.

db_type(connection)#

The type of the field to insert into the database.

rel_db_type(connection)#

The type to be used by relations pointing to this field.

Not used until Django 1.10.

class openedx.core.djangolib.fields.CharNullField(*args, db_collation=None, **kwargs)#

Bases: CharField

CharField that stores NULL but returns ‘’

description = "CharField that stores NULL but returns ''"#
get_db_prep_value(value, connection, prepared=False)#

Converts value to a backend-specific value.

to_python(value)#

Converts the value into the correct Python object.

openedx.core.djangolib.js_utils module#

Utilities for dealing with Javascript and JSON.

openedx.core.djangolib.js_utils.dump_js_escaped_json(obj, cls=<class 'openedx.core.lib.json_utils.EdxJSONEncoder'>)#

JSON dumps and escapes objects that are safe to be embedded in JavaScript.

Use this for anything but strings (e.g. dicts, tuples, lists, bools, and numbers). For strings, use js_escaped_string.

The output of this method is also usable as plain-old JSON.

Usage:

Used as follows in a Mako template inside a <SCRIPT> tag:

var json_obj = ${obj | n, dump_js_escaped_json}

If you must use the cls argument, then use as follows:

var json_obj = ${dump_js_escaped_json(obj, cls) | n}

Use the “n” Mako filter above. It is possible that the default filter may include html escaping in the future, and this ensures proper escaping.

Ensure ascii in json.dumps (ensure_ascii=True) allows safe skipping of Mako’s default filter decode.utf8.

Parameters:
  • obj – The object soon to become a JavaScript escaped JSON string. The object can be anything but strings (e.g. dicts, tuples, lists, bools, and numbers).

  • cls (class) – The JSON encoder class (defaults to EdxJSONEncoder).

Returns:

(string) Escaped encoded JSON.

openedx.core.djangolib.js_utils.js_escaped_string(string_for_js)#

Mako filter that escapes text for use in a JavaScript string.

If None is provided, returns an empty string.

Usage:

Used as follows in a Mako template inside a <SCRIPT> tag:

var my_string_for_js = "${my_string_for_js | n, js_escaped_string}"

The surrounding quotes for the string must be included.

Use the “n” Mako filter above. It is possible that the default filter may include html escaping in the future, and this ensures proper escaping.

Mako’s default filter decode.utf8 is applied here since this default filter is skipped in the Mako template with “n”.

Parameters:

string_for_js (string) – Text to be properly escaped for use in a JavaScript string.

Returns:

(string) Text properly escaped for use in a JavaScript string as unicode. Returns empty string if argument is None.

openedx.core.djangolib.markup module#

Utilities for use in Mako markup.

openedx.core.djangolib.markup.HTML(html)#

Mark a string as already HTML, so that it won’t be escaped before output.

Use this function when formatting HTML into other strings. It must be used in conjunction with Text(), and both HTML() and Text() must be closed before any calls to format():

<%page expression_filter="h"/>
<%!
from django.utils.translation import gettext as _

from openedx.core.djangolib.markup import HTML, Text
%>
${Text(_("Write & send {start}email{end}")).format(
    start=HTML("<a href='mailto:{}'>").format(user.email),
    end=HTML("</a>"),
)}
openedx.core.djangolib.markup.clean_dangerous_html(html)#
Mark a string as already HTML and remove unsafe tags, so that it won’t be escaped before output.

Usage: <%page expression_filter=”h”/> <%! from openedx.core.djangolib.markup import clean_dangerous_html %> ${course_details.overview | n, clean_dangerous_html}

openedx.core.djangolib.markup.strip_all_tags_but_br(string_to_strip)#

Strips all tags from a string except <br/> and marks as HTML.

Usage:

<%page expression_filter=”h”/> <%! from openedx.core.djangolib.markup import strip_all_tags_but_br %> ${accomplishment_course_title | n, strip_all_tags_but_br}

openedx.core.djangolib.model_mixins module#

Custom Django Model mixins.

class openedx.core.djangolib.model_mixins.DeletableByUserValue#

Bases: object

This mixin allows inheriting models to delete instances of the model associated with some specified user.

classmethod delete_by_user_value(value, field)#

Redacts as-needed and always deletes instances of this model where field equals value.

e.g.

delete_by_user_value(value='learner@example.com', field='email')

If redact_before_delete_fields() returns a non-empty dict, the returned PII fields are redacted before any records are deleted.

Returns True if any instances were deleted. Returns False otherwise.

classmethod redact_before_delete_fields()#

Returns dict of PII fields and their redacted values.

Always returns an empty dict unless overridden by the inheriting model. Results are used by delete_by_user_value to redact PII before delete.

class openedx.core.djangolib.model_mixins.DeprecatedModelMixin(*args, **kwargs)#

Bases: object

Used to make a class unusable in practice, but leave database tables intact.

openedx.core.djangolib.oauth2_retirement_utils module#

Removes user PII from OAuth2 models.

class openedx.core.djangolib.oauth2_retirement_utils.ModelRetirer(models_to_retire)#

Bases: object

Given a list of model names, provides methods for deleting instances of those models.

retire_user_by_id(user_id)#
openedx.core.djangolib.oauth2_retirement_utils.retire_dot_oauth2_models(user)#

openedx.core.djangolib.translation_utils module#

i18n utility functions

openedx.core.djangolib.translation_utils.translate_date(date, language, date_format='DATE_FORMAT')#

Converts the provided date object into a string, while translating its value for the given language. Both the format of the date as well as its values (i.e., name of the Month) are translated.

If language is Spainish, then the entire date string is returned in lowercase. This is used to work around a bug in the Spanish django month translations. See EDUCATOR-2328 for more details.

For example:

date = datetime.datetime(2017, 12, 23) date_in_spanish = translate_date(date, ‘es’) assert date_in_spanish == ‘23 de deciembre de 2017’

Module contents#