shuup.admin.utils package

Submodules

shuup.admin.utils.bs3_renderers module

class shuup.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]
append_to_field(html)[source]

shuup.admin.utils.forms module

shuup.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.

shuup.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

shuup.admin.utils.forms.flatatt_filter(attrs)[source]
shuup.admin.utils.forms.get_possible_name_fields_for_model(model)[source]

Get possible name fields for given model

This function yields strings of field names that could possible be identified as name fields for model.

For example get_possible_name_fields_for_model(Coupon) yields string “code”

Parameters:Class object of the model (model) –
Returns:Yield strings of possible name fields
Return type:str

shuup.admin.utils.permissions module

shuup.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]
shuup.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]

shuup.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]
shuup.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

shuup.admin.utils.picotable module

shuup.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.

shuup.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 shuup.admin.utils.picotable.Filter[source]

Bases: object

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

Bases: shuup.admin.utils.picotable.Filter

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

Bases: shuup.admin.utils.picotable.ChoicesFilter

type = 'select2'
to_json(context)[source]
class shuup.admin.utils.picotable.MPTTFilter(choices=None, filter_field=None, default=None)[source]

Bases: shuup.admin.utils.picotable.Select2Filter

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

Bases: shuup.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, context)[source]
class shuup.admin.utils.picotable.DateRangeFilter(*args, **kwargs)[source]

Bases: shuup.admin.utils.picotable.RangeFilter

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

Bases: shuup.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, context)[source]
class shuup.admin.utils.picotable.MultiFieldTextFilter(filter_fields, **kwargs)[source]

Bases: shuup.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, context)[source]
class shuup.admin.utils.picotable.Column(id, title, **kwargs)[source]

Bases: object

to_json(context=None)[source]
get_sort_field(sort_field)[source]
set_sort_field()[source]
set_context(context)[source]
sort_queryset(queryset, desc=False)[source]
filter_queryset(queryset, value)[source]
get_display_value(context, object)[source]
check_different_types(value)[source]
search_from_provided_contexts(object)[source]
class shuup.admin.utils.picotable.Picotable(request, columns, mass_actions, queryset, context)[source]

Bases: object

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

Bases: object

url_identifier = None
default_columns = []
columns = []
mass_actions = []
picotable_class

alias of Picotable

related_objects = []
template_name = 'shuup/admin/base_picotable.jinja'
process_picotable(query_json)[source]
get(request, *args, **kwargs)[source]
post(request, *args, **kwargs)[source]

Post action is where Mass Actions post their data

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]
load_mass_actions()[source]
class shuup.admin.utils.picotable.PicotableMassAction[source]

Bases: object

Simple Mass Action

This action only processes the given id’s in subclass.

Examples: * shuup.admin.modules.orders.mass_actions.CancelOrderAction * shuup.admin.modules.products.mass_actions.VisibleMassAction

label = <django.utils.functional.lazy.<locals>.__proxy__ object>
identifier = 'mass_action'
process(request, ids)[source]

Process the given ids in masses

Parameters:
  • requestWSGIRequest
  • ids – list of ids
Returns:

None

class shuup.admin.utils.picotable.PicotableFileMassAction[source]

Bases: shuup.admin.utils.picotable.PicotableMassAction

File Mass Action

This action returns file as a response.

Examples: * shuup.admin.modules.orders.mass_actions.OrderConfirmationPdfAction * shuup.admin.modules.products.mass_actions.FileResponseAction

process(request, ids)[source]

Process and return HttpResponse

Example:
response = HttpResponse(content_type=”text/csv”) response[‘Content-Disposition’] = ‘attachment; filename=”mass_action.csv”’ writer = csv.writer(response) writer.writerow([‘First row’, ‘Foo’, ‘Bar’, ‘Baz’]) return response
Parameters:
  • requestWSGIRequest
  • ids – list of ids
Returns:

HttpResponse

class shuup.admin.utils.picotable.PicotableRedirectMassAction[source]

Bases: shuup.admin.utils.picotable.PicotableMassAction

Redirect Mass Action

This view saves selected id’s into session which are then further processed in the mass action view.

Redirect of this view is handled in picotable.js

To use this action, your admin module must supply admin_url and a view for the action.

Examples: * shuup.admin.modules.contacts.mass_actions.EditContactsAction * shuup.admin.modules.products.mass_actions.EditProductAttributesAction

redirect_url = None
process(request, ids)[source]

shuup.admin.utils.search module

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

Bases: object

test(text)[source]
shuup.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]

shuup.admin.utils.str_utils module

shuup.admin.utils.str_utils.camelcase_to_snakecase(string_to_convert)[source]

Convert CamelCase string to snake_case

Original solution in http://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case

shuup.admin.utils.tour module

shuup.admin.utils.tour.is_tour_complete(tour_key)[source]

Check if the tour is complete

Parameters:tour_key – The tour key.
Returns:whether tour is complete
Return type:Boolean

shuup.admin.utils.urls module

class shuup.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
shuup.admin.utils.urls.admin_url(regex, view, kwargs=None, name=None, prefix='', require_authentication=True, permissions=())[source]
shuup.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. “shuup.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 shuup.admin.utils.urls.NoModelUrl[source]

Bases: ValueError

shuup.admin.utils.urls.get_model_url(object, kind='detail', user=None, required_permissions=None)[source]

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

If a user is provided, checks whether user has correct permissions before returning URL.

Raises NoModelUrl if lookup fails

Parameters:
  • object (class) – Model or object class.
  • kind (str) – URL kind. Currently “new”, “list”, “edit”, “detail”.
  • user (django.contrib.auth.models.User|None) – Optional instance to check for permissions
  • required_permissions (Iterable[str]|None) – Optional iterable of permission strings
Returns:

Resolved URL.

Return type:

str

shuup.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, shuup_admin:shop_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

shuup.admin.utils.urls.manipulate_query_string(url, **qs)[source]
shuup.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

shuup.admin.utils.views module

class shuup.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]
shuup.admin.utils.views.add_create_or_change_message(request, instance, is_new)[source]
shuup.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

shuup.admin.utils.views.check_and_raise_if_only_one_allowed(setting_name, obj)[source]
class shuup.admin.utils.views.PicotableListView[source]

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

get_toolbar()[source]
get_context_data(**kwargs)[source]
get_object_abstract(instance, item)[source]
class shuup.admin.utils.views.MassEditMixin[source]

Bases: object

template_name = 'shuup/admin/mass_action/mass_edit.jinja'
title = <django.utils.functional.lazy.<locals>.__proxy__ object>
dispatch(request, *args, **kwargs)[source]
get_context_data(**kwargs)[source]

shuup.admin.utils.wizard module

shuup.admin.utils.wizard.load_setup_wizard_panes(shop, request=None, visible_only=True)[source]

Load the setup Wizard panes. The result will be a list of valid pane instances.

Parameters:visible_only (bool) – whether to return only visible panes
shuup.admin.utils.wizard.load_setup_wizard_pane(shop, request, pane_id)[source]

Search, load and return a valid Wizard Pane by its identifier.

Parameters:pane_id (str) – the pane identifier
Returns:the pane instance or None
Return type:shuup.admin.views.wizard.WizardPane|None
shuup.admin.utils.wizard.setup_wizard_complete()[source]

Check if shop wizard should be run.

Returns:whether setup wizard needs to be run
Return type:boolean
shuup.admin.utils.wizard.setup_wizard_visible_panes(shop)[source]

Check if shop wizard has visible panes that require merchant configuration.

Returns:whether the setup wizard has visible panes
Return type:Boolean
shuup.admin.utils.wizard.setup_blocks_complete(request)[source]

Check if any incomplete setup blocks remain.

Returns:whether all setup blocks are complete
Return type:Boolean
shuup.admin.utils.wizard.onboarding_complete(request)[source]

Check if the shop wizard and all setup blocks are complete

Returns:whether onboarding is complete
Return type:Boolean

Module contents