src/p4l/semantictree/forms/widgets.py
author ymh <ymh.work@gmail.com>
Thu, 06 Nov 2014 02:08:12 +0100
changeset 176 a343992c55fd
parent 131 f1854630734f
permissions -rw-r--r--
Added tag V00.08 for changeset 1ff6533ba75d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
126
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     1
# -*- coding: utf-8 -*-
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     2
#
131
f1854630734f change copyright notice
ymh <ymh.work@gmail.com>
parents: 126
diff changeset
     3
# Copyright IRI (c) 2013
126
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     4
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     5
# contact@iri.centrepompidou.fr
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     6
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     7
# This software is governed by the CeCILL-B license under French law and
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     8
# abiding by the rules of distribution of free software.  You can  use, 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
     9
# modify and/ or redistribute the software under the terms of the CeCILL-B
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    10
# license as circulated by CEA, CNRS and INRIA at the following URL
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    11
# "http://www.cecill.info". 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    12
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    13
# As a counterpart to the access to the source code and  rights to copy,
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    14
# modify and redistribute granted by the license, users are provided only
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    15
# with a limited warranty  and the software's author,  the holder of the
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    16
# economic rights,  and the successive licensors  have only  limited
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    17
# liability. 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    18
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    19
# In this respect, the user's attention is drawn to the risks associated
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    20
# with loading,  using,  modifying and/or developing or reproducing the
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    21
# software by the user in light of its specific status of free software,
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    22
# that may mean  that it is complicated to manipulate,  and  that  also
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    23
# therefore means  that it is reserved for developers  and  experienced
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    24
# professionals having in-depth computer knowledge. Users are therefore
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    25
# encouraged to load and test the software's suitability as regards their
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    26
# requirements in conditions enabling the security of their systems and/or 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    27
# data to be ensured and,  more generally, to use and operate it in the 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    28
# same conditions as regards security. 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    29
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    30
# The fact that you are presently reading this means that you have had
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    31
# knowledge of the CeCILL-B license and that you accept its terms.
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 36
diff changeset
    32
#
36
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    33
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    34
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
    35
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
    36
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
    37
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
    38
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
    39
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
    40
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    41
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
    42
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
    43
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    44
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
    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
    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
    47
    """
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
    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
    50
        css = {
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    51
            '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
    52
        }
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    53
        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
    54
    
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    55
    # 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
    56
    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
    57
        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
    58
            value = ''
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    59
        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
    60
        # 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
    61
        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
    62
            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
    63
        else:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    64
            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
    65
        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
    66
            # 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
    67
            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
    68
        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
    69
        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
    70
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    71
        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
    72
            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
    73
        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
    74
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    75
        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
    76
        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
    77
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    78
        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
    79
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    80
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    81
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
    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
    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
    84
    """
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
    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
    87
        css = {
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    88
            '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
    89
        }
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    90
        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
    91
    
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    92
    # 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
    93
    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
    94
        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
    95
            value = ''
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
    96
        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
    97
        # 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
    98
        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
    99
            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
   100
        else:
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
   101
            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
   102
        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
   103
            # 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
   104
            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
   105
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
   106
        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
   107
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
   108
        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
   109
            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
   110
        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
   111
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
   112
        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
   113
        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
   114
        
08cffedf6e60 first step of semantic tree, just to save. it will not be used as this in this project.
cavaliet
parents:
diff changeset
   115
        return input_res + " " + _("or") + " " + dialog_res