Core functionality¶
This page details the core classes and functions that Oscar uses. These aren’t specific to one particular app, but are used throughout Oscar.
Dynamic class loading¶
The key to Oscar’s flexibility is dynamically loading classes. This allows projects to provide their own versions of classes that extend and override the core functionality.
- oscar.core.loading.get_class(module_label, classname, module_prefix='oscar.apps')[source]¶
Dynamically import a single class from the given module.
This is a simple wrapper around get_classes for the case of loading a single class.
- Parameters
module_label (str) – Module label comprising the app label and the module name, separated by a dot. For example, ‘catalogue.forms’.
classname (str) – Name of the class to be imported.
- Returns
The requested class object or None if it can’t be found
URL patterns and views¶
Oscar’s apps organise their URLs and associated views using a “OscarConfig” class instance. This works in a similar way to Django’s admin app, and allows Oscar projects to subclass and customised URLs and views.
- class oscar.core.application.OscarConfig(app_name, app_module, namespace=None, **kwargs)[source]¶
Base Oscar app configuration.
This is subclassed by each app to provide a customisable container for its configuration, URL configurations, and permissions.
- class oscar.core.application.OscarConfigMixin(app_name, app_module, namespace=None, **kwargs)[source]¶
Base Oscar app configuration mixin, used to extend
django.apps.AppConfig
to also provide URL configurations and permissions.- default_permissions = None¶
Default permission for any view not in permissions_map
- get_permissions(url)[source]¶
Return a list of permissions for a given URL name
- Parameters
url (str) – A URL name (e.g.,
basket.basket
)- Returns
A list of permission strings.
- Return type
list
- get_url_decorator(pattern)[source]¶
Return the appropriate decorator for the view function with the passed URL name. Mainly used for access-protecting views.
It’s possible to specify:
no permissions necessary: use None
a set of permissions: use a list
two set of permissions (or): use a two-tuple of lists
See permissions_required decorator for details
- hidable_feature_name = None¶
A name that allows the functionality within this app to be disabled
- permissions_map = {}¶
Maps view names to lists of permissions. We expect tuples of lists as dictionary values. A list is a set of permissions that all need to be fulfilled (AND). Only one set of permissions has to be fulfilled (OR). If there’s only one set of permissions, as a shortcut, you can also just define one list.
- post_process_urls(urlpatterns)[source]¶
Customise URL patterns.
This method allows decorators to be wrapped around an apps URL patterns.
By default, this only allows custom decorators to be specified, but you could override this method to do anything you want.
- Parameters
urlpatterns (list) – A list of URL patterns
Prices¶
Oscar uses a custom price object for easier tax handling.
- class oscar.core.prices.Price(currency, excl_tax, incl_tax=None, tax=None, tax_code=None)[source]¶
Simple price class that encapsulates a price and its tax information
- incl_tax¶
Price including taxes
- Type
Decimal
- excl_tax¶
Price excluding taxes
- Type
Decimal
- tax¶
Tax amount
- Type
Decimal
- is_tax_known¶
Whether tax is known
- Type
bool
- currency¶
3 character currency code
- Type
str
Custom model fields¶
Oscar uses a few custom model fields.
- class oscar.models.fields.Creator(field)[source]¶
A placeholder class that provides a way to set the attribute on the model.
- class oscar.models.fields.NullCharField(*args, **kwargs)[source]¶
CharField that stores ‘’ as None and returns None as ‘’ Useful when using unique=True and forms. Implies null==blank==True.
Django’s CharField stores ‘’ as None, but does not return None as ‘’.
Form helpers¶
- class oscar.forms.mixins.PhoneNumberMixin(*args, **kwargs)[source]¶
Validation mixin for forms with a phone numbers, and optionally a country.
It tries to validate the phone numbers, and on failure tries to validate them using a hint (the country provided), and treating it as a local number.
Specify which fields to treat as phone numbers by specifying them in phone_number_fields, a dictionary of fields names and default kwargs for instantiation of the field.