|
1 """ |
|
2 Some useful form widgets |
|
3 """ |
|
4 |
|
5 from django import forms |
|
6 from django.core.exceptions import ValidationError |
|
7 from django.forms.util import flatatt |
|
8 from django.utils.html import format_html |
|
9 from django.utils.encoding import force_text |
|
10 from django.utils.translation import ugettext as _ |
|
11 |
|
12 import logging |
|
13 logger = logging.getLogger(__name__) |
|
14 |
|
15 class SemanticTreeWidget(forms.TextInput): |
|
16 """ |
|
17 A widget that enables to request semantic trees |
|
18 """ |
|
19 |
|
20 class Media: |
|
21 css = { |
|
22 'all': ('semantictree/semantictree.css',) |
|
23 } |
|
24 js = ('semantictree/semantictree.js',) |
|
25 |
|
26 # The user can add css classes but we always add "semantic-tree" class |
|
27 def render(self, name, value, attrs=None): |
|
28 if value is None: |
|
29 value = '' |
|
30 final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) |
|
31 # Test if class attr is here |
|
32 if "class" in final_attrs: |
|
33 final_attrs["class"] += " semantic-tree" |
|
34 else: |
|
35 final_attrs["class"] = "semantic-tree" |
|
36 if value != '': |
|
37 # Only add the 'value' attribute if a value is non-empty. |
|
38 final_attrs['value'] = force_text(self._format_value(value)) |
|
39 final_attrs['placeholder'] = _("Search") |
|
40 input_res = format_html('<input{0} />', flatatt(final_attrs)) |
|
41 |
|
42 if "data-url" not in final_attrs or "data-query" not in final_attrs or "data-root-query" not in final_attrs or "data-childs-query" not in final_attrs or "data-child-count-query" not in final_attrs: |
|
43 raise ValidationError(_('"data-url", "data-query", "data-root-query", "data-childs-query" and "data-child-count-query" must be set in CharField configuration')) |
|
44 dialog_text = _("Browse") |
|
45 |
|
46 dialog_res = "<span id=\"dialog-link-container-" + final_attrs["name"] + "\" class=\"dialog-link-container ui-state-default ui-corner-all\"><a href=\"#\" id=\"dialog-link-" + final_attrs["name"] + "\" class=\"dialog-link\" title=\"%s\">%s</a></span>" % (dialog_text,dialog_text) |
|
47 dialog_res += '<span id="dialog-' + final_attrs["name"] + '" class="dialog" title="Select term"><span id="term-tree-' + final_attrs["name"] + '"></span></span>' |
|
48 |
|
49 return input_res + " " + _("or") + " " + dialog_res |
|
50 |
|
51 |
|
52 class SemanticTagItWidget(forms.TextInput): |
|
53 """ |
|
54 A widget that enables to request semantic trees |
|
55 """ |
|
56 |
|
57 class Media: |
|
58 css = { |
|
59 'all': ('semantictree/semantictree.css',) |
|
60 } |
|
61 js = ('semantictree/semantictree.js',) |
|
62 |
|
63 # The user can add css classes but we always add "semantic-tree" class |
|
64 def render(self, name, value, attrs=None): |
|
65 if value is None: |
|
66 value = '' |
|
67 final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) |
|
68 # Test if class attr is here |
|
69 if "class" in final_attrs: |
|
70 final_attrs["class"] += " semantic-tree-tagit" |
|
71 else: |
|
72 final_attrs["class"] = "semantic-tree-tagit" |
|
73 if value != '': |
|
74 # Only add the 'value' attribute if a value is non-empty. |
|
75 final_attrs['value'] = force_text(self._format_value(value)) |
|
76 |
|
77 input_res = format_html('<input{0} />', flatatt(final_attrs)) |
|
78 |
|
79 if "data-url" not in final_attrs or "data-query" not in final_attrs or "data-root-query" not in final_attrs or "data-childs-query" not in final_attrs or "data-child-count-query" not in final_attrs: |
|
80 raise ValidationError(_('"data-url", "data-query", "data-root-query", "data-childs-query" and "data-child-count-query" must be set in CharField configuration')) |
|
81 dialog_text = _("Browse") |
|
82 |
|
83 dialog_res = "<span id=\"dialog-link-container-" + final_attrs["name"] + "\" class=\"dialog-link-container ui-state-default ui-corner-all\"><a href=\"#\" id=\"dialog-link-" + final_attrs["name"] + "\" class=\"dialog-link\" title=\"%s\">%s</a></span>" % (dialog_text,dialog_text) |
|
84 dialog_res += '<span id="dialog-' + final_attrs["name"] + '" class="dialog" title="Select term"><span id="term-tree-' + final_attrs["name"] + '"></span></span>' |
|
85 |
|
86 return input_res + " " + _("or") + " " + dialog_res |