This library contains useful functions and classes mainly for a web development with Django (form and model fields, shortcuts, advanced datastructure, decoraters etc.). For more details see examples below.
django.forms.DecimalField
with step
parameter and validated min
and max
parameters.
Widget for safe rendering of readonly form values.
Mixin for automatic South migration of custom model fields.
django.db.models.DecimalField
with step
, min
and max
parameters. Uses chamber.forms.fields.DecimalField
as default form field.
Same as FileField, but you can specify:
content_types
- list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']max_upload_size
- a number indicating the maximum file size allowed for upload in MB. Maximum upload size can be specified in project settings underMAX_FILE_UPLOAD_SIZE
constant
django.db.models.FileField
with RestrictedFileFieldMixin
options.
sorl.thumbnail.ImageField
(fallback to django.db.models.ImageField
) with RestrictedFileFieldMixin
options.
django.db.models.CharField
that stores NULL
but returns ''.
remove_accent('ěščřžýáíé') # 'escrzyaie'
It returns a method of a given class or instance.
Base enumeration class with controlled __getattr__
.
Python's set
with AbstractEnum
behaviour.
Python's dict
with AbstractEnum
behaviour.
>>> NumEnum('a', 'b')
{'a': 1, 'b': 2}
Base choices class (as used in Model field's choices
argument).
django.utils.datastructures.SortedDict
with AbstractEnum
and AbstractChoicesEnum
behaviour. Useful for string based choices.
>>> enum = ChoicesEnum(('OK', 'ok'), ('KO', 'ko'))
>>> enum
{'OK': 'ok', 'KO': 'ko'}
>>> enum.OK
'ok'
>>> enum.choices
[('OK', 'ok'), ('KO', 'ko')]
django.utils.datastructures.SortedDict
with AbstractEnum
and AbstractChoicesEnum
behaviour. Useful for integer based choices.
>>> enum = ChoicesNumEnum(('OK', 'ok', 1), ('KO', 'ko', 2))
>>> enum.KO
2
>>> enum.choices
[(1, 'ok'), (2, 'ko')]
>>> enum.get_label(2)
'ko'
Ties property to class, usefull for usage in class methods.
class RestResource(BaseResource):
@classproperty
def csrf_exempt(cls):
return not cls.login_required
@classmethod
def as_view(cls, allowed_methods=None, **initkwargs):
view.csrf_exempt = cls.csrf_exempt
Class decorator, which allows for only one instance of class to exist.
Sets short_description
attribute (this attribute is used by list_display and formulars).
@short_description('amount')
def absolute_amount(self):
return abs(self.amount)
is equivalent of
def absolute_amount(self):
return abs(self.amount)
absolute_amount.short_description = 'amount'
Returns True if passed formset contains FileField (or ImageField).
Assemble query string from dict
of parameters.
>>> query_string_from_dict({'q': 'query1', 'user': 'test'})
u'q=query1&user=test'
Takes Model or QuerySet and arguments and returns instance of Model if exists, None
otherwise.
>>> get_object_or_none(User, pk=1)
<User: Gaul Asterix>
>>> get_object_or_none(User.objects.exclude(pk=1), pk=1) or ''
''
Takes Model or QuerySet and arguments and returns instance of Model if exists, raises django.http.response.Http404
otherwise.
>>> get_object_or_404(User, pk=1)
<User: Gaul Asterix>
>>> get_object_or_404(User.objects.exclude(pk=1), pk=1)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/var/ve/lib/python2.7/site-packages/chamber/shortcuts.py", line 21, in get_object_or_404
raise Http404
Http404
Takes Model or QuerySet and distinction parameters and returns list of unique values.
>>> User.objects.filter(last_name='Gaul')
[<User: Gaul Obelix>, <User: Gaul Asterix>]
>>> distinct_field(User.objects.filter(last_name='Gaul'), 'last_name')
[(u'Gaul',)]
Takes negate bool
(True for exclude, False for filter), Model or QuerySet and date parameters and return queryset filtered or excluded by date parameters.
>>> Order.objects.values_list('created_at', flat=True)
[datetime.datetime(2014, 4, 6, 15, 56, 16, 727000, tzinfo=<UTC>),
datetime.datetime(2014, 2, 6, 15, 56, 16, 727000, tzinfo=<UTC>),
datetime.datetime(2014, 1, 11, 23, 15, 43, 727000, tzinfo=<UTC>)]
>>> filter_or_exclude_by_date(False, Order, created_at=date(2014, 2, 6))
[<Order: MI-1234567>]
>>> filter_or_exclude_by_date(False, Order, created_at=date(2014, 2, 6))[0].created_at
datetime.datetime(2014, 2, 6, 15, 56, 16, 727000, tzinfo=<UTC>)
Shortcut for chamber.shortcuts.filter_or_exclude_by_date
with first parameter False.
Shortcut for chamber.shortcuts.filter_or_exclude_by_date
with first parameter True.
For contribution go to example
directory and call make install
to install an example project.
To run tests go to example
directory and call make test
.
See LICENSE file.