oioioi.base.utils

Submodules

Package Contents

Classes

ClassInitMeta

Meta class triggering __classinit__ on class intialization.

ClassInitBase

Abstract base class injecting ClassInitMeta meta class.

RegisteredSubclassesBase

A base class for classes which should have a list of subclasses

_RemoveMixinsFromInitMixin

ObjectWithMixins

Base class for objects which support mixins.

memoized_property

A read-only @property that is only evaluated once.

ProgressBar

Displays simple textual progress bar.

Functions

memoized(fn)

Simple wrapper that adds result caching for functions with positional

reset_memoized(memoized_fn)

Clear the memoization cache of a function decorated by

request_cached(fn)

Adds per-request caching for functions which operate on sole request.

make_html_link(href, name[, method, extra_attrs])

make_html_links(links[, extra_attrs])

make_navbar_badge(link, text[, id])

tabbed_view(request, template, context, tabs, ...)

A framework for building pages that are split into tabs.

uploaded_file_name(uploaded_file)

split_extension(filename)

strip_num_or_hash(filename)

naturalsort_key(key)

jsonify(view)

A decorator to serialize view result with JSON.

add_header(header, value)

allow_cross_origin([arg])

Add Access-Control-Allow-Origin header with given value,

is_ajax(request)

Check if 'request' is an jQuery AJAX call.

generate_key()

Generate an random key, encoded in url-safe way.

get_user_display_name(user)

This method returns the full user name if available and

find_closure(groups)

Finds closure of sets.

Attributes

class oioioi.base.utils.ClassInitMeta(class_name, bases, new_attrs)[source]

Bases: type

Meta class triggering __classinit__ on class intialization.

class oioioi.base.utils.ClassInitBase[source]

Bases: object

Abstract base class injecting ClassInitMeta meta class.

classmethod __classinit__()[source]

Empty __classinit__ implementation.

This must be a no-op as subclasses can’t reliably call base class’s __classinit__ from their __classinit__s.

Subclasses of __classinit__ should look like:

class oioioi.base.utils.RegisteredSubclassesBase[source]

Bases: ClassInitBase

A base class for classes which should have a list of subclasses available.

The list of subclasses is available in their subclasses class attributes. Classes which have explicitly set abstract class attribute to True are not added to subclasses.

If a class has modules_with_subclasses attribute (list or string), then specified modules for all installed applications can be loaded by calling load_subclasses().

_subclasses_loaded = False[source]
classmethod __classinit__()[source]

Empty __classinit__ implementation.

This must be a no-op as subclasses can’t reliably call base class’s __classinit__ from their __classinit__s.

Subclasses of __classinit__ should look like:

classmethod load_subclasses()[source]
class oioioi.base.utils._RemoveMixinsFromInitMixin(*args, **kwargs)[source]

Bases: object

class oioioi.base.utils.ObjectWithMixins[source]

Bases: ClassInitBase

Base class for objects which support mixins.

Mixins are nice tools in Python. But they have one drawback – you have to specify new class’ mixins at the point where you declare it. This class solves this problem. Mixins can be now be added on the fly by mix_in() method. This allows for a more flexible modular design.

For example:

# base.py
class UserController(ObjectWithMixins):
    def render_user_info(self, user):
        return "Login: " + user.username

# some_external_module.py
class UserControllerBeautifier(object):
    def render_user_info(self, user):
        super_info = super(UserControllerBeautifier, self)                                 .render_user_info(user)
        return '<font color="red">' + super_info + '</font>'
UserController.mix_in(UserControllerBeautifier)

Mixins can also be specified by providing a mixins class attribute or by passing an additional keyword argument mixins to the constructor.

A class with a mixin behave as if it was replaced with a subclass which bases are the mixin and the original class.

The actual class with the mixins is created when the constructor is called or a subclass defined. Mixing in a new mixin to a class which have instances has an undefined effect on them.

mixins = [][source]
allow_too_late_mixins = False[source]
classmethod __classinit__()[source]

Empty __classinit__ implementation.

This must be a no-op as subclasses can’t reliably call base class’s __classinit__ from their __classinit__s.

Subclasses of __classinit__ should look like:

classmethod _make_mx_class(mixins)[source]
classmethod _get_mx_class()[source]
classmethod _fixup_subclasses()[source]
classmethod _fixup_subclass(subclass)[source]
classmethod mix_in(mixin)[source]

Appends the given mixin to the list of class mixins.

class oioioi.base.utils.memoized_property(fget, doc=None)[source]

Bases: object

A read-only @property that is only evaluated once.

__get__(obj, cls)[source]
oioioi.base.utils.memoized(fn)[source]

Simple wrapper that adds result caching for functions with positional arguments only.

The arguments must be hashable so that they can be stored as keys in a dict.

oioioi.base.utils.reset_memoized(memoized_fn)[source]

Clear the memoization cache of a function decorated by :fun:`memoized`.

oioioi.base.utils.request_cached(fn)[source]

Adds per-request caching for functions which operate on sole request.

oioioi.base.utils.make_navbar_badge(link, text, id=None)[source]
oioioi.base.utils.tabbed_view(request, template, context, tabs, tab_kwargs, link_builder)[source]

A framework for building pages that are split into tabs.

The current tab is picked using the ‘key’ GET parameter. The given template is rendered using the given context, which is extended by ‘current_tab’, representing the opened tab, ‘tabs’, a set of ‘obj’ and ‘link’ pairs for each existing tab, where ‘obj’ represents the tab and ‘link’ is a link to the tab’s page, and ‘content’, the tab’s rendered content.

Parameters
  • request – a HttpRequest object given to the view

  • template – the rendered template

  • context – additional context to be passed to the template

  • tabs – an iterable of tabs. Each tab must have a unique ‘key’ attribute that will be used to create an URL to the tab, a ‘view’ attribute returning either HttpResponseRedirect, TemplateResponse or rendered html, and an optional ‘condition’ attribute: a function taking a request and returning if the tab should be accessible for this request. If there is no condition then it is assumed to be always returning True.

  • tab_kwargs – a dict to be passed as kwargs to each tab’s view

  • link_builder – a function which receives a tab and returns a link to the tab. It should contain a proper path and the appropriate ‘key’ parameter.

oioioi.base.utils.uploaded_file_name(uploaded_file)[source]
oioioi.base.utils.split_extension(filename)[source]
oioioi.base.utils._STRIP_NUM_RE[source]
oioioi.base.utils._STRIP_HASH_RE[source]
oioioi.base.utils.strip_num_or_hash(filename)[source]
oioioi.base.utils.naturalsort_key(key)[source]
class oioioi.base.utils.ProgressBar(max_value, length=20)[source]

Bases: object

Displays simple textual progress bar.

_show(preserve=False)[source]
_clear()[source]
update(value=None, preserve=False)[source]

Set new value (if given) and redraw the bar.

Parameters

preserve – controls if bar will end with a new line and stay after next update.

oioioi.base.utils.jsonify(view)[source]

A decorator to serialize view result with JSON.

The object returned by view will be converted to JSON and returned as an appropriate django.http.HttpResponse.

oioioi.base.utils.add_header(header, value)[source]
oioioi.base.utils.allow_cross_origin(arg='*')[source]

Add Access-Control-Allow-Origin header with given value, or ‘*’ if none given.

May be used as any of:

@allow_cross_origin @allow_cross_origin() @allow_cross_origin(’http://example.com’)

oioioi.base.utils.is_ajax(request)[source]

Check if ‘request’ is an jQuery AJAX call.

oioioi.base.utils.generate_key()[source]

Generate an random key, encoded in url-safe way.

oioioi.base.utils.get_user_display_name(user)[source]

This method returns the full user name if available and the username otherwise.

oioioi.base.utils.find_closure(groups)[source]

Finds closure of sets.

If any two elements were within same input set,they will be in one unique set in the output.

>>> find_closure([[1, 2], [2, 3], [4]])
[[1, 2, 3], [4],]