src/p4l/semantictree/forms/widgets.py
author cavaliet
Wed, 04 Sep 2013 16:56:55 +0200
changeset 36 08cffedf6e60
child 126 a345f1a67bf1
permissions -rw-r--r--
first step of semantic tree, just to save. it will not be used as this in this project.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     1
"""
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     2
Some useful form widgets
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     3
"""
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     4
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     5
from django import forms
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     6
from django.core.exceptions import ValidationError
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     7
from django.forms.util import flatatt
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     8
from django.utils.html import format_html
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
     9
from django.utils.encoding import force_text
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    10
from django.utils.translation import ugettext as _
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    11
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    12
import logging
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    13
logger = logging.getLogger(__name__)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    14
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    15
class SemanticTreeWidget(forms.TextInput):
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    16
    """
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    17
    A widget that enables to request semantic trees
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    18
    """
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    19
    
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    20
    class Media:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    21
        css = {
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    22
            'all': ('semantictree/semantictree.css',)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    23
        }
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    24
        js = ('semantictree/semantictree.js',)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    25
    
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    26
    # The user can add css classes but we always add "semantic-tree" class
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    27
    def render(self, name, value, attrs=None):
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    28
        if value is None:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    29
            value = ''
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    30
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    31
        # Test if class attr is here
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    32
        if "class" in final_attrs:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    33
            final_attrs["class"] += " semantic-tree"
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    34
        else:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    35
            final_attrs["class"] = "semantic-tree"
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    36
        if value != '':
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    37
            # Only add the 'value' attribute if a value is non-empty.
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    38
            final_attrs['value'] = force_text(self._format_value(value))
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    39
        final_attrs['placeholder'] = _("Search")
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    40
        input_res = format_html('<input{0} />', flatatt(final_attrs))
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    41
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    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:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    43
            raise ValidationError(_('"data-url", "data-query", "data-root-query", "data-childs-query" and "data-child-count-query" must be set in CharField configuration'))
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    44
        dialog_text = _("Browse")
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    45
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    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)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    47
        dialog_res += '<span id="dialog-' + final_attrs["name"] + '" class="dialog" title="Select term"><span id="term-tree-' + final_attrs["name"] + '"></span></span>'
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    48
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    49
        return input_res + " " + _("or") + " " + dialog_res
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    50
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    51
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    52
class SemanticTagItWidget(forms.TextInput):
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    53
    """
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    54
    A widget that enables to request semantic trees
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    55
    """
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    56
    
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    57
    class Media:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    58
        css = {
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    59
            'all': ('semantictree/semantictree.css',)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    60
        }
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    61
        js = ('semantictree/semantictree.js',)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    62
    
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    63
    # The user can add css classes but we always add "semantic-tree" class
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    64
    def render(self, name, value, attrs=None):
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    65
        if value is None:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    66
            value = ''
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    67
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    68
        # Test if class attr is here
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    69
        if "class" in final_attrs:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    70
            final_attrs["class"] += " semantic-tree-tagit"
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    71
        else:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    72
            final_attrs["class"] = "semantic-tree-tagit"
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    73
        if value != '':
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    74
            # Only add the 'value' attribute if a value is non-empty.
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    75
            final_attrs['value'] = force_text(self._format_value(value))
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    76
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    77
        input_res = format_html('<input{0} />', flatatt(final_attrs))
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    78
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    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:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    80
            raise ValidationError(_('"data-url", "data-query", "data-root-query", "data-childs-query" and "data-child-count-query" must be set in CharField configuration'))
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    81
        dialog_text = _("Browse")
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    82
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    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)
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    84
        dialog_res += '<span id="dialog-' + final_attrs["name"] + '" class="dialog" title="Select term"><span id="term-tree-' + final_attrs["name"] + '"></span></span>'
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    85
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    86
        return input_res + " " + _("or") + " " + dialog_res