openedx.core.djangolib package#
Subpackages#
Submodules#
openedx.core.djangolib.blockstore_cache module#
An API for caching data related to Blockstore bundles
The whole point of this is to make the hard problem of cache invalidation somewhat less hard.
This cache prefixes all keys with the bundle/draft version number, so that when any change is made to the bundle/draft, we will look up entries using a new key and won’t find the now-invalid cached data.
- class openedx.core.djangolib.blockstore_cache.BundleCache(bundle_uuid, draft_name=None)#
Bases:
objectData cache that ties every key-value to a particular version of a blockstore bundle/draft, so that if/when the bundle/draft is updated, the cache is automatically invalidated.
The automatic invalidation may take up to MAX_BLOCKSTORE_CACHE_DELAY seconds, although the cache can also be manually invalidated for any particular bundle versoin/draft by calling .clear()
- clear()#
Clear the cache for the specified bundle or draft.
This doesn’t actually delete keys from the cache, but if the bundle or draft has been modified, this will ensure we use the latest version number, which will change the key prefix used by this cache, causing the old version’s keys to become unaddressable and eventually expire.
- get(key_parts, default=None)#
Get a cached value related to this Blockstore bundle/draft.
- key_parts: an arbitrary list of strings to identify the cached value.
For example, if caching the XBlock type of an OLX file, one could request:
get(bundle_uuid, [“olx_type”, “/path/to/file”])
default: default value if the key is not set in the cache draft_name: read keys related to the specified draft
- set(key_parts, value)#
Set a cached value related to this Blockstore bundle/draft.
- key_parts: an arbitrary list of strings to identify the cached value.
For example, if caching the XBlock type of an OLX file, one could request:
set(bundle_uuid, [“olx_type”, “/path/to/file”], “html”)
value: value to set in the cache
- openedx.core.djangolib.blockstore_cache.get_bundle_direct_links_with_cache(bundle_uuid, bundle_version=None, draft_name=None)#
Get a dictionary of the direct links of the specified bundle, from cache if possible.
- openedx.core.djangolib.blockstore_cache.get_bundle_draft_direct_links_cached(bundle_uuid, draft_name)#
Get the direct links in the specified bundle draft. Cached using BundleCache so we get automatic cache invalidation when the draft is updated.
- openedx.core.djangolib.blockstore_cache.get_bundle_draft_files_cached(bundle_uuid, draft_name)#
Get the files in the specified bundle draft. Cached using BundleCache so we get automatic cache invalidation when the draft is updated.
- openedx.core.djangolib.blockstore_cache.get_bundle_file_data_with_cache(bundle_uuid, path, bundle_version=None, draft_name=None)#
Method to read a file out of a Blockstore Bundle[Version] or Draft, using the cached list of files in each bundle if available.
- openedx.core.djangolib.blockstore_cache.get_bundle_file_metadata_with_cache(bundle_uuid, path, bundle_version=None, draft_name=None)#
Get metadata about a file in a Blockstore Bundle[Version] or Draft, using the cached list of files in each bundle if available.
- openedx.core.djangolib.blockstore_cache.get_bundle_files_cached(bundle_uuid, bundle_version=None, draft_name=None)#
Get the list of files in the bundle, optionally with a version and/or draft specified.
- openedx.core.djangolib.blockstore_cache.get_bundle_version_direct_links_cached(bundle_uuid, bundle_version)#
Get the direct links in the specified BundleVersion. Since BundleVersions are immutable, this should be cached as aggressively as possible.
- openedx.core.djangolib.blockstore_cache.get_bundle_version_files_cached(bundle_uuid, bundle_version)#
Get the files in the specified BundleVersion. Since BundleVersions are immutable, this should be cached as aggressively as possible.
- openedx.core.djangolib.blockstore_cache.get_bundle_version_number(bundle_uuid, draft_name=None)#
Get the current version number of the specified bundle/draft. If a draft is specified, the update timestamp is used in lieu of a version number.
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:
JwtAuthenticationDefault 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.
openedx.core.djangolib.fields module#
Custom Django fields.
- class openedx.core.djangolib.fields.BigAutoField(*args, **kwargs)#
Bases:
AutoFieldAutoField 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:
CharFieldCharField 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 bothHTML()andText()must be closed before any calls toformat():<%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:
objectThis mixin allows inheriting models to delete instances of the model associated with some specified user.
- classmethod delete_by_user_value(value, field)#
Deletes instances of this model where
fieldequalsvalue.- e.g.
delete_by_user_value(value='learner@example.com', field='email')
Returns True if any instances were deleted. Returns False otherwise.
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:
objectGiven 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’