Commit | Line | Data |
---|---|---|
0958f822 PP |
1 | # -*- coding: utf-8 -*- |
2 | from django.forms.util import flatatt | |
3 | from django.forms.widgets import MultiWidget, Select, TextInput, Widget | |
4 | from django.utils.safestring import mark_safe | |
5 | from django.utils.translation import ugettext as _ | |
6 | ||
7 | ||
8 | OPERATOR_CHOICES = ( | |
9 | ('', ''), | |
10 | ('exact', _('is equal to')), | |
11 | ('contains', _('contains')), | |
12 | ('regex', _('matchs regex')), | |
13 | ('startswith', _('starts with')), | |
14 | ('endswith', _('ends with')), | |
15 | ('gt', _('is greater than')), | |
16 | ('gte', _('is greater than or equal to')), | |
17 | ('lt', _('is less than')), | |
18 | ('lte', _('is less than or equal to')), | |
19 | ('iexact', _('(i) is equal to')), | |
20 | ('icontains', _('(i) contains')), | |
21 | ('iregex', _('(i) matchs regex')), | |
22 | ('istartswith', _('(i) starts with')), | |
23 | ('endswith', _('(i) ends with')), | |
24 | ('join', _('joins to')), | |
25 | ) | |
26 | ||
27 | ||
28 | class CheckboxLabelWidget(Widget): | |
29 | ||
30 | def __init__(self, attrs=None, label=None, prelabel=None, *args, **kwargs): | |
31 | super(CheckboxLabelWidget, self).__init__(*args, **kwargs) | |
32 | self.attrs = attrs or {} | |
33 | self.label = label or _('Check this') | |
34 | self.prelabel = prelabel | |
35 | ||
36 | def render(self, name, value=None, attrs=None, prelabel=None): | |
37 | self.attrs.update(attrs or {}) | |
38 | final_attrs = self.build_attrs(self.attrs, name=name) | |
39 | prelabel = prelabel or self.prelabel | |
40 | if prelabel: | |
41 | out = u'<label for="%s" >%s</label><input type="checkbox"%s >' \ | |
42 | % (self.attrs.get("id", ""), value or self.label, | |
43 | flatatt(final_attrs)) | |
44 | else: | |
45 | out = u'<input type="checkbox"%s ><label for="%s" >%s</label>' \ | |
46 | % (flatatt(final_attrs), self.attrs.get("id", ""), | |
47 | value or self.label) | |
48 | return mark_safe(out) | |
49 | ||
50 | ||
51 | class CriteriaInput(MultiWidget): | |
52 | ||
53 | class Media: | |
54 | js = ('django_qbe/js/qbe.widgets.js', ) | |
55 | ||
56 | def __init__(self, *args, **kwargs): | |
57 | # widgets = [CheckboxLabelWidget(label=_("Inverse")), | |
58 | # Select(choices=OPERATOR_CHOICES), TextInput(), | |
59 | # CheckboxLabelWidget(label=_("Nothing"))] | |
60 | widgets = [Select(choices=OPERATOR_CHOICES), TextInput()] | |
61 | super(CriteriaInput, self).__init__(widgets=widgets, *args, **kwargs) | |
62 | ||
63 | def decompress(self, value): | |
64 | if value: | |
65 | return value | |
66 | return (None, None) |