shoop.admin.utils package

Submodules

shoop.admin.utils.bs3_renderers module

class shoop.admin.utils.bs3_renderers.AdminFieldRenderer(field, **kwargs)[source]

Bases: bootstrap3.renderers.FieldRenderer

get_label()[source]
add_class_attrs(*args, **kwargs)[source]
add_help_attrs(*args, **kwargs)[source]

shoop.admin.utils.forms module

shoop.admin.utils.forms.filter_form_field_choices(field, predicate, invert=False)[source]

Filter choices of a form field and its widget by predicate.

The predicate may be a callable of the signature (pair) -> bool or an iterable of allowable values.

Parameters:
  • field (django.forms.Field) – Form field.
  • predicate (function|Iterable) – Predicate
  • invert (bool) – Invert the semantics of the predicate, i.e. items matching it will be rejected.
Returns:

Nothing. The field is modified in-place.

shoop.admin.utils.forms.add_form_errors_as_messages(request, form)[source]

Add the form’s errors, if any, into the request as messages.

Parameters:
Returns:

Number of messages added. May be thousands, for a very unlucky form.

Return type:

int

shoop.admin.utils.forms.flatatt_filter(attrs)[source]

shoop.admin.utils.permissions module

shoop.admin.utils.permissions.get_default_model_permissions(model)[source]

Return a set of all default permissions for a given model.

Parameters:model (django.db.Model) – Model class
Returns:Set of default model permissions as strings
Return type:set[str]
shoop.admin.utils.permissions.get_missing_permissions(user, permissions)[source]

Return a set of missing permissions for a given iterable of permission strings.

Parameters:
Returns:

Set of missing permission strings

Return type:

set[str]

shoop.admin.utils.permissions.get_permissions_from_urls(urls)[source]

Return a set of permissions for a given iterable of urls.

Parameters:urls (Iterable[django.core.urlresolvers.RegexURLPattern]) – Iterable of url objects to check for permissions
Returns:Set of permissions for urls as strings
Return type:set[str]
shoop.admin.utils.permissions.get_permission_object_from_string(permission_string)[source]

Given a permission string of the form app_label.permission_string, get actual permission object.

Parameters:permission_string – Permission string
Returns:Permission object
Return type:django.contrib.auth.models.Permission

shoop.admin.utils.picotable module

shoop.admin.utils.picotable.maybe_callable(thing, context=None)[source]

If thing is callable, return it. If thing names a callable attribute of context, return it.

shoop.admin.utils.picotable.maybe_call(thing, context, args=None, kwargs=None)[source]

If thing is callable, call it with args and kwargs and return the value. If thing names a callable attribute of context, call it with args and kwargs and return the value. Otherwise return thing.

class shoop.admin.utils.picotable.Filter[source]

Bases: object

type = None
to_json(context)[source]
filter_queryset(queryset, column, value)[source]
class shoop.admin.utils.picotable.ChoicesFilter(choices=None, filter_field=None, default=None)[source]

Bases: shoop.admin.utils.picotable.Filter

type = 'choices'
to_json(context)[source]
filter_queryset(queryset, column, value)[source]
class shoop.admin.utils.picotable.RangeFilter(min=None, max=None, step=None, field_type=None, filter_field=None)[source]

Bases: shoop.admin.utils.picotable.Filter

Parameters:
  • filter_field (str|None) – Filter field (Django query expression). If None, column ID is used.
  • min – Minimum value.
  • max – Maximum value.
  • step – Step value. See the HTML5 documentation for semantics.
  • field_type (str|None) – Field type string. See the HTML5 documentation for semantics.
type = 'range'
to_json(context)[source]
filter_queryset(queryset, column, value)[source]
class shoop.admin.utils.picotable.DateRangeFilter(*args, **kwargs)[source]

Bases: shoop.admin.utils.picotable.RangeFilter

filter_queryset(queryset, column, value)[source]
class shoop.admin.utils.picotable.TextFilter(field_type=None, placeholder=None, operator='icontains', filter_field=None)[source]

Bases: shoop.admin.utils.picotable.Filter

Parameters:
  • filter_field (str|None) – Filter field (Django query expression). If None, column ID is used.
  • field_type (str|None) – Field type string. See the HTML5 documentation for semantics.
  • placeholder (str|None) – Field placeholder string.
  • operator (str) – Django operator for the queryset.
type = 'text'
to_json(context)[source]
filter_queryset(queryset, column, value)[source]
class shoop.admin.utils.picotable.MultiFieldTextFilter(filter_fields, **kwargs)[source]

Bases: shoop.admin.utils.picotable.TextFilter

Parameters:
  • filter_field (list<str>) – List of Filter fields (Django query expression).
  • kwargs – Kwargs for TextFilter.
filter_queryset(queryset, column, value)[source]
class shoop.admin.utils.picotable.Column(id, title, **kwargs)[source]

Bases: object

to_json(context=None)[source]
sort_queryset(queryset, desc=False)[source]
filter_queryset(queryset, value)[source]
get_display_value(context, object)[source]
class shoop.admin.utils.picotable.Picotable(request, columns, queryset, context)[source]

Bases: object

process_queryset(query)[source]
get_data(query)[source]
process_item(object)[source]
get_verbose_name_plural()[source]
class shoop.admin.utils.picotable.PicotableViewMixin[source]

Bases: object

columns = []
picotable_class

alias of Picotable

template_name = 'shoop/admin/base_picotable.jinja'
process_picotable(query_json)[source]
get(request, *args, **kwargs)[source]
get_object_url(instance)[source]
get_object_abstract(instance, item)[source]

Get the object abstract lines (used for mobile layouts) for this object.

Supported keys in abstract line dicts are:

  • text (required)
  • title
  • class (CSS class name – header for instance)
  • raw (boolean; whether or not the text is raw HTML)
Parameters:
  • instance – The instance
  • item – The item dict so far. Useful for reusing precalculated values.
Returns:

Iterable of dicts to pass through to the picotable javascript

Return type:

Iterable[dict]

get_filter()[source]

shoop.admin.utils.search module

class shoop.admin.utils.search.FuzzyMatcher(query)[source]

Bases: object

test(text)[source]
shoop.admin.utils.search.split_query(query_string, minimum_part_length=3)[source]

Split a string into a set of non-empty words, none shorter than minimum_part_length characters.

Parameters:
  • query_string (str) – Query string
  • minimum_part_length (int) – Minimum part length
Returns:

Set of query parts

Return type:

set[str]

shoop.admin.utils.urls module

class shoop.admin.utils.urls.AdminRegexURLPattern(regex, callback, default_args=None, name=None, require_authentication=True, permissions=())[source]

Bases: django.core.urlresolvers.RegexURLPattern

wrap_with_permissions(view_func)[source]
callback
shoop.admin.utils.urls.admin_url(regex, view, kwargs=None, name=None, prefix='', require_authentication=True, permissions=())[source]
shoop.admin.utils.urls.get_edit_and_list_urls(url_prefix, view_template, name_template, permissions=())[source]

Get a list of edit/new/list URLs for (presumably) an object type with standardized URLs and names.

Parameters:
  • url_prefix (str) – What to prefix the generated URLs with. E.g. "^taxes/tax"
  • view_template (str) – A template string for the dotted name of the view class. E.g. “shoop.admin.modules.taxes.views.Tax%sView”
  • name_template (str) – A template string for the URLnames. E.g. “tax.%s”
Returns:

List of URLs

Return type:

list[AdminRegexURLPattern]

exception shoop.admin.utils.urls.NoModelUrl[source]

Bases: ValueError

shoop.admin.utils.urls.get_model_url(object, kind='detail')[source]

Get a an admin object URL for the given object or object class by interrogating each admin module.

Raises NoModelUrl if lookup fails

Parameters:
  • object (class) – Model or object class.
  • kind (str) – URL kind. Currently “new”, “list”, “edit”, “detail”.
Returns:

Resolved URL.

Return type:

str

shoop.admin.utils.urls.derive_model_url(model_class, urlname_prefix, object, kind)[source]

Try to guess a model URL for the given object and kind.

An utility for people implementing get_model_url.

Parameters:
  • model_class (class) – The model class the object must be an instance or subclass of.
  • urlname_prefix (str) – URLname prefix. For instance, shoop_admin:product.
  • object (django.db.models.Model|class) – The model or model class as passed to get_model_url
  • kind (str) – URL kind as passed to get_model_url.
Returns:

Resolved URL or None.

Return type:

str|None

shoop.admin.utils.urls.manipulate_query_string(url, **qs)[source]
shoop.admin.utils.urls.get_model_front_url(request, object)[source]

Get a frontend URL for an object.

Parameters:
Returns:

URL or None

Return type:

str|None

shoop.admin.utils.views module

class shoop.admin.utils.views.CreateOrUpdateView(**kwargs)[source]

Bases: django.views.generic.edit.UpdateView

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

add_form_errors_as_messages = False
get_object(queryset=None)[source]
get_toolbar()[source]
get_context_data(**kwargs)[source]
get_save_form_id()[source]
get_return_url()[source]
get_new_url()[source]
get_success_url()[source]
get_form_kwargs()[source]
form_valid(form)[source]
save_form(form)[source]
form_invalid(form)[source]
shoop.admin.utils.views.add_create_or_change_message(request, instance, is_new)[source]
shoop.admin.utils.views.get_create_or_change_title(request, instance, name_field=None)[source]

Get a title suitable for an create-or-update view.

Parameters:
  • request (HttpRequest) – Request
  • instance (django.db.models.Model) – Model instance
  • name_field (str) – Which property to try to read the name from. If None, use str
Returns:

Title

Return type:

str

shoop.admin.utils.views.check_and_raise_if_only_one_allowed(setting_name, obj)[source]
class shoop.admin.utils.views.PicotableListView(**kwargs)[source]

Bases: shoop.admin.utils.picotable.PicotableViewMixin, django.views.generic.list.ListView

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

get_toolbar()[source]
get_context_data(**kwargs)[source]
get_object_abstract(instance, item)[source]

Module contents