cms.utils package¶
Submodules¶
cms.utils.archive module¶
-
class
cms.utils.archive.
Archive
(file, ext='')¶ Bases:
object
The external API class that encapsulates an archive implementation.
-
close
()¶
-
extract
(*args, **kwargs)¶
-
filenames
()¶
-
list
()¶
-
read
(name)¶
-
-
exception
cms.utils.archive.
ArchiveException
¶ Bases:
exceptions.Exception
Base exception class for all archive errors.
-
class
cms.utils.archive.
BaseArchive
(file)¶ Bases:
object
Base Archive class. Implementations should inherit this class.
-
check_files
(to_path=None)¶ Check that all of the files contained in the archive are within the target directory.
-
close
()¶
-
extract
(to_path='', method='safe')¶
-
filenames
()¶ Return a list of the filenames contained in the archive.
-
list
()¶
-
read
(name)¶
-
-
class
cms.utils.archive.
TarArchive
(file)¶ Bases:
cms.utils.archive.BaseArchive
-
filenames
()¶
-
list
(*args, **kwargs)¶
-
read
(name)¶
-
-
exception
cms.utils.archive.
UnrecognizedArchiveFormat
¶ Bases:
cms.utils.archive.ArchiveException
Error raised when passed file is not a recognized archive format.
-
exception
cms.utils.archive.
UnsafeArchive
¶ Bases:
cms.utils.archive.ArchiveException
Error raised when passed file contains paths that would be extracted outside of the target directory.
-
class
cms.utils.archive.
ZipArchive
(file)¶ Bases:
cms.utils.archive.BaseArchive
-
filenames
()¶
-
list
(*args, **kwargs)¶
-
read
(name)¶
-
-
cms.utils.archive.
extract
(path, to_path='', ext='', **kwargs)¶ Unpack the tar or zip file at the specified path to the directory specified by to_path.
-
cms.utils.archive.
get_allowed_type
(pretty=False)¶
-
cms.utils.archive.
is_valid
(ext)¶
cms.utils.base module¶
-
cms.utils.base.
convert_bytes
(value, decimal_places=2)¶
-
cms.utils.base.
merge_sum_dictionaries
(*dicts)¶
-
cms.utils.base.
random_string
(length=25, allowed_chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_!:.&')¶
-
cms.utils.base.
slice_per
(source, step)¶
-
cms.utils.base.
unique
(seq)¶ >>> list(unique([1, 2, 3, 2, 2, 4, 1])) [1, 2, 3, 4]
cms.utils.cms_check module¶
-
class
cms.utils.cms_check.
CMSCheck
(plain=False, fail_silently=False)¶ Bases:
object
-
output
¶
-
registry
= []¶
-
-
exception
cms.utils.cms_check.
CMSChecksException
¶ Bases:
exceptions.Exception
cms.utils.countries module¶
cms.utils.dev module¶
-
cms.utils.dev.
run_dev_or_demo_installation
(argv, installtype)¶
-
cms.utils.dev.
supervisor_shutdown
(path)¶
cms.utils.docs module¶
-
cms.utils.docs.
build_modelgraph
(docs_path, settings)¶ Creates a diagram of all the models for and the given package name, generates a smaller version and add it to the docs directory for use in model-graph.rst
-
cms.utils.docs.
build_plugin_docs
(docs_path)¶
-
cms.utils.docs.
build_settings_docs
(docs_path, prefix=None)¶ Converts names, descriptions and defaults for settings in
cms.core.plugins.settingsdict
into RST format for use in docs, optionally filtered by setting names with the given prefix.
-
cms.utils.docs.
deep_force_unicode
(value)¶ Recursively call force_text on value.
cms.utils.html module¶
-
cms.utils.html.
bleachify
(text, level='HIGH', dolinkify=False)¶
-
cms.utils.html.
decode_entities
(html)¶ Remove HTML entities from a string. Adapted from http://effbot.org/zone/re-sub.htm#unescape-html
-
cms.utils.html.
descriptify
(text=None, length=None)¶
-
cms.utils.html.
get_title_from_url
(url)¶
-
cms.utils.html.
linkify_callback
(attrs, new=False)¶
-
cms.utils.html.
pygmentize
(value)¶ Expects the raw html content of a post as value. This function will look through the content for any <pre> tags and update their contents to include css classes as per syntax highlighting according to pygments. Can take an optional argument that dictates what css class the resulting ‘pre’ pre tag will have. Returns the html passed in with the updated <pre> tags included. Loosely based on: http://www.ofbrooklyn.com/2010/01/15/syntax-highlighting-django-using-pygments/
-
cms.utils.html.
teasify
(text, limit=100, truncate=True)¶
Truncates a string after a certain number of chars, strips all [X]HTML tags and removes whitespaces.
cms.utils.importing module¶
-
cms.utils.importing.
import_dotted_path
(path)¶ Takes a dotted path to a member name in a module, and returns the member after importing it.
cms.utils.logger module¶
-
class
cms.utils.logger.
LoggerDBRequireDebugFalseFilter
(name='')¶ Bases:
logging.Filter
-
filter
(record)¶
-
-
class
cms.utils.logger.
LoggerRequireDebugFalseFilter
(name='')¶ Bases:
logging.Filter
-
filter
(record)¶
-
-
class
cms.utils.logger.
RequireDebugFalse
(name='')¶ Bases:
logging.Filter
-
filter
(record)¶
-
-
class
cms.utils.logger.
RequireDebugTrue
(name='')¶ Bases:
logging.Filter
-
filter
(record)¶
-
cms.utils.models module¶
-
class
cms.utils.models.
LazyModelOperations
¶ Bases:
object
This class connects itself to Django’s class_prepared signal. Pass a function and a model or model name to its
add()
method, and the function will be called with the model as its only parameter once the model has been loaded. If the model is already loaded, the function is called immediately.Adapted from
django.db.models.fields.related
and used inmezzanine.generic.fields
.-
add
(function, *models_or_names)¶ The function passed to this method should accept n arguments, where n=len(models_or_names). When all the models are ready, the function will be called with the models as arguments, in the order they appear in this argument list.
-
static
model_key
(model_or_name)¶ Returns an (app_label, model_name) tuple from a model or string.
-
signal_receiver
(sender, **kwargs)¶ Receive
class_prepared
, and pass the freshly prepared model to each function waiting for it.
-
-
cms.utils.models.
add_group_permission
(group, model_permissions)¶
-
cms.utils.models.
base_concrete_model
(abstract, instance)¶ Used in methods of abstract models to find the super-most concrete (non abstract) model in the inheritance chain that inherits from the given abstract model. This is so the methods in the abstract model can query data consistently across the correct concrete model.
Consider the following:
class Abstract(models.Model) class Meta: abstract = True def concrete(self): return base_concrete_model(Abstract, self) class Super(Abstract): pass class Sub(Super): pass sub = Sub.objects.create() sub.concrete() # returns Super
In actual Mezzanine usage, this allows methods in the
Displayable
andOrderable
abstract models to access thePage
instance when instances of custom content types, (eg: models that inherit fromPage
) need to query thePage
model to determine correct values forslug
and_order
which are only relevant in the context of thePage
model and not the model of the custom content type.
-
cms.utils.models.
create_groups
(sender)¶ Autocreate the groups (normal and admin) for the requested model and adding extra permissions for extra admin-group.
-
cms.utils.models.
get_parent_choices
(objects, obj=None)¶ Returns flat list of tuples (possible_parent.pk, possible_parent.title_with_spacer). If ‘item’ is not given or None, returns every item of the menu. If given, intentionally omit it and its descendant in the list.
-
cms.utils.models.
humanize_filesize
(size)¶
cms.utils.names module¶
-
cms.utils.names.
name_allowed
(username)¶ Returns True if the given username is not a blatent bad word.
cms.utils.packages module¶
-
cms.utils.packages.
get_3rdparty_packages
(include_local=False, types=('python', 'javascript', 'fonts', 'other'))¶
-
cms.utils.packages.
get_installed_packages
()¶
-
cms.utils.packages.
get_metadata
(dist)¶ Return dictionary of metadata for given dist
@param dist: distribution @type dist: pkg_resources Distribution object
@returns: dict of metadata or None
-
cms.utils.packages.
get_updateable_packages
(pretty=False)¶
cms.utils.pdf module¶
cms.utils.sphinx_apidoc module¶
-
cms.utils.sphinx_apidoc.
main
(opts=None)¶
-
cms.utils.sphinx_apidoc.
normalize_excludes
(rootpath, excludes)¶ Normalize the excluded directory list.
cms.utils.template module¶
-
cms.utils.template.
device_from_request
(request)¶ Determine’s the device name from the request by first looking for an overridding cookie, and if not found then matching the user agent. Used at both the template level for choosing the template to load and also at the cache level as a cache key prefix.
-
cms.utils.template.
get_templatedirs_for_model
(model)¶
-
cms.utils.template.
get_templates_for_model
(model, dirs=None)¶
-
cms.utils.template.
get_templates_for_model_choices
(model)¶
-
cms.utils.template.
templates_for_device
(request, templates)¶ Given a template name (or list of them), returns the template names as a list, with each name prefixed with the device directory inserted before it’s associate default in the list.
cms.utils.timezone module¶
-
cms.utils.timezone.
convert_to_iso
(end, start)¶ Convert a
relativedelta
to aDuration
.
-
cms.utils.timezone.
datetime_to_jsdate
(date)¶
-
cms.utils.timezone.
jsdate_to_datetime
(date)¶
-
cms.utils.timezone.
utcoffset_normalize
(date, delta=None, dstmode='adjust')¶ Fixes invalid UTC offsets from recurrence calculations.
Parameters: - date – datetime instance to normalize.
- delta – datetime.timedelta instance. Mode DSTADJUST: When crossing daylight saving time changes, the start time of the date before DST change will be the same in value as afterwards. It is adjusted relative to UTC. So 8:00 GMT+1 before will also result in 8:00 GMT+2 afterwards. This is what humans might expect when recurring rules are defined. Mode DSTKEEP: When crossing daylight saving time changes, the start time of the date before and after DST change will be the same relative to UTC. So, 8:00 GMT+1 before will result in 7:00 GMT+2 afterwards. This behavior might be what machines expect, when recurrence rules are defined. Mode DSTAUTO: If the relative delta between two occurences of a reucurrence sequence is less than a day, DSTKEEP will be used - otherwise DSTADJUST. This behavior is the default.
cms.utils.translations module¶
-
cms.utils.translations.
get_default_language
(language_code=None)¶ Returns default language depending on settings.LANGUAGE_CODE merged with best match from settings.LANGUAGES
Returns: language_code
-
cms.utils.translations.
get_language
()¶ Return an active language code that is guaranteed to be in settings.LANGUAGES (Django does not seem to guarantee this for us).
-
cms.utils.translations.
split_lang_from_path
(path)¶
cms.utils.urls module¶
-
cms.utils.urls.
admin_url
(model, url, object_id=None)¶ Returns the URL for the given model and admin url name. Use _meta.concrete_model._meta.model_name as _meta.model_name did not work for deferred models.
-
cms.utils.urls.
better_slugify
(value, slugify=True, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False, separator='-')¶
-
cms.utils.urls.
patterns_never_cache
(prefix, *args)¶
-
cms.utils.urls.
prepend_protocol
(request, url)¶
-
cms.utils.urls.
request_get_host
(request)¶
-
cms.utils.urls.
request_get_host_secure
(request)¶
-
cms.utils.urls.
resolve_url
(to, *args, **kwargs)¶ Return a URL appropriate for the arguments passed.
The arguments could be:
- A model: the model’s get_absolute_url() function will be called.
- A view name, possibly with arguments: urlresolvers.reverse() will be used to reverse-resolve the name.
- A URL, which will be returned as-is.
cms.utils.versioning module¶
-
cms.utils.versioning.
get_cloc
(prettify=False)¶
-
cms.utils.versioning.
get_git_sha
(path, head='master', short=False)¶
cms.utils.views module¶
-
class
cms.utils.views.
SortHeaders
(request, headers, default_order_field=None, default_order_type='asc', additional_params=None)¶ Handles generation of an argument for the Django ORM’s
order_by
method and generation of table headers which reflect the currently selected sort, based on defined table headers with matching sort criteria.Based in part on the Django Admin application’s
ChangeList
functionality.-
ORDER_TYPE_VAR
= 'otrend'¶
-
ORDER_VAR
= 'otype'¶
-
get_order_by
()¶ Creates an ordering criterion based on the current order field and order type, for use with the Django ORM’s
order_by
method.
-
get_query_string
(params)¶ Creates a query string from the given dictionary of parameters, including any additonal parameters which should always be present.
-
headers
()¶ Generates dicts containing header and sort link details for all defined headers.
-
-
class
cms.utils.views.
hashify
(string, ts=None)¶ Bases:
object
-
as_dict
()¶
-
as_mac
()¶
-
as_querystring
()¶
-
-
cms.utils.views.
initial_validation
(request, model, setting)¶ Returns the related model instance and post data to use in the comment/rating views below.
Both comments and ratings have a
prefix_ACCOUNT_REQUIRED
setting. If this isTrue
and the user is unauthenticated, we store their post data in their session, and redirect to login with the view’s url (also defined by the prefix arg) as thenext
param. We can then check the session data once they log in, and complete the action authenticated.On successful post, we pass the related object and post data back, which may have come from the session, for each of the comments and ratings view functions to deal with as needed.
-
cms.utils.views.
paginate
(objects, page_num, per_page)¶ Return a paginated page for the given objects, giving it a custom
visible_page_range
attribute calculated frommax_paging_links
.
Set cookie wrapper that allows number of seconds to be given as the expiry time, and ensures values are correctly encoded.
cms.utils.wsgi module¶
-
cms.utils.wsgi.
application
(environ, start_response)¶
-
cms.utils.wsgi.
maintenance_application
(environ, start_response)¶