first step of semantic tree, just to save. it will not be used as this in this project.
authorcavaliet
Wed, 04 Sep 2013 16:56:55 +0200
changeset 36 08cffedf6e60
parent 35 544bc92e6fe7
child 37 7ff73789a851
first step of semantic tree, just to save. it will not be used as this in this project.
src/p4l/semantictree/__init__.py
src/p4l/semantictree/forms/__init__.py
src/p4l/semantictree/forms/forms.py
src/p4l/semantictree/forms/widgets.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/p4l/semantictree/forms/forms.py	Wed Sep 04 16:56:55 2013 +0200
@@ -0,0 +1,289 @@
+from django import forms
+from .widgets import SemanticTreeWidget, SemanticTagItWidget
+
+
+class SubjectForm(forms.Form):
+    # __reg__ is updated with the real regexp by the js
+    subject = forms.CharField(required=True, label="Subjects", 
+                          widget=SemanticTreeWidget(attrs={
+                            'size':  25, 
+                            'class': "form-control",
+                            'data-url': "http://localhost:8080/openrdf-sesame/repositories/plan4learning",
+                            'data-query':  
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+    ?uri a skos:Concept.
+    ?uri skos:inScheme <http://skos.um.es/unescothes/CS000> .
+    ?uri skos:prefLabel ?label.
+    FILTER (lang(?label) = ?language).
+    ?uri skos:prefLabel ?lab.
+    FILTER regex (str(?lab), ?reg, 'i').
+    FILTER (lang (?lab) = ?language).
+    BIND (STRLEN(STRBEFORE (str(?lab), ?reg)) AS ?place).
+    BIND (STRLEN(STR(?lab)) AS ?len)
+}
+ORDER BY ?place ?len ?lab
+""",
+                            'data-root-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+    ?uri a skos:Collection ;
+    skos:inScheme <http://skos.um.es/unescothes/CS000> ;    
+    rdfs:label ?label .
+    FILTER (lang(?label) = ?language). 
+    FILTER NOT EXISTS { [skos:member ?uri] }
+}
+""",
+                            'data-childs-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+  ?uri skos:inScheme <http://skos.um.es/unescothes/CS000> .
+  { ?uri a ?type
+    FILTER (?type = skos:Collection || ?type = skos:Concept) }.
+  ?root skos:narrower|skos:member ?uri.
+  ?uri skos:prefLabel|rdfs:label ?label.
+  FILTER (lang(?label) = ?language).
+}
+""",
+                            'data-child-count-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT (COUNT(?uri) as ?nb)
+WHERE {
+    ?uri skos:inScheme <http://skos.um.es/unescothes/CS000> .
+    ?root skos:narrower|skos:member ?uri.
+}
+"""
+                            }))
+
+
+
+
+class SemForm(forms.Form):
+    # __reg__ is updated with the real regexp by the js
+    sem = forms.CharField(required=True, label="Dude, sem", 
+                          widget=SemanticTreeWidget(attrs={
+                            'size':  25, 
+                            'class': "dude",
+                            'data-url': "http://localhost:8080/openrdf-sesame/repositories/plan4learning",
+                            'data-query':  
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+    ?uri a skos:Concept.
+    ?uri skos:inScheme <http://skos.um.es/unescothes/CS000> .
+    ?uri skos:prefLabel ?label.
+    FILTER (lang(?label) = ?language).
+    ?uri skos:prefLabel ?lab.
+    FILTER regex (str(?lab), ?reg, 'i').
+    FILTER (lang (?lab) = ?language).
+    BIND (STRLEN(STRBEFORE (str(?lab), ?reg)) AS ?place).
+    BIND (STRLEN(STR(?lab)) AS ?len)
+}
+ORDER BY ?place ?len ?lab
+""",
+                            'data-root-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+    ?uri a skos:Collection ;
+    skos:inScheme <http://skos.um.es/unescothes/CS000> ;    
+    rdfs:label ?label .
+    FILTER (lang(?label) = ?language). 
+    FILTER NOT EXISTS { [skos:member ?uri] }
+}
+""",
+                            'data-childs-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+  ?uri skos:inScheme <http://skos.um.es/unescothes/CS000> .
+  { ?uri a ?type
+    FILTER (?type = skos:Collection || ?type = skos:Concept) }.
+  ?root skos:narrower|skos:member ?uri.
+  ?uri skos:prefLabel|rdfs:label ?label.
+  FILTER (lang(?label) = ?language).
+}
+""",
+                            'data-child-count-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT (COUNT(?uri) as ?nb)
+WHERE {
+    ?uri skos:inScheme <http://skos.um.es/unescothes/CS000> .
+    ?root skos:narrower|skos:member ?uri.
+}
+"""
+                            }))
+    iam = forms.CharField(required=True, label="Dude, iam", widget=SemanticTreeWidget(attrs={
+                            'size':  25, 
+                            'class': "dude",
+                            'data-url': "http://localhost:8080/openrdf-sesame/repositories/plan4learning",
+                            'data-query':  
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+    ?uri a skos:Concept.
+    ?uri skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> .
+    ?uri skos:prefLabel ?label.
+    FILTER (lang(?label) = ?language).
+    ?uri skos:prefLabel ?lab.
+    FILTER regex (str(?lab), ?reg, 'i').
+    FILTER (lang (?lab) = ?language).
+    BIND (STRLEN(STRBEFORE (str(?lab), ?reg)) AS ?place).
+    BIND (STRLEN(STR(?lab)) AS ?len)
+}
+ORDER BY ?place ?len ?lab
+""",
+                            'data-root-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+    ?uri a skos:Concept ;
+    skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> ;    
+    skos:prefLabel ?label .
+    FILTER (lang(?label) = ?language). 
+    FILTER NOT EXISTS { [skos:narrower ?uri] }
+}
+""",
+                            'data-childs-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+  ?uri skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> .
+  { ?uri a ?type
+    FILTER (?type = skos:Collection || ?type = skos:Concept) }.
+  ?root skos:narrower|skos:member ?uri.
+  ?uri skos:prefLabel|rdfs:label ?label.
+  FILTER (lang(?label) = ?language).
+}
+""",
+                            'data-child-count-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT (COUNT(?uri) as ?nb)
+WHERE {
+    ?uri skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> .
+    ?root skos:narrower|skos:member ?uri.
+}
+"""
+                            }))
+    ntm = forms.CharField(required=True, label="Dude, ntm", widget=SemanticTagItWidget(attrs={
+                            'size':  25, 
+                            'class': "dude",
+                            'data-url': "http://localhost:8080/openrdf-sesame/repositories/plan4learning",
+                            'data-query':  
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+    ?uri a skos:Concept.
+    ?uri skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> .
+    ?uri skos:prefLabel ?label.
+    FILTER (lang(?label) = ?language).
+    ?uri skos:prefLabel ?lab.
+    FILTER regex (str(?lab), ?reg, 'i').
+    FILTER (lang (?lab) = ?language).
+    BIND (STRLEN(STRBEFORE (str(?lab), ?reg)) AS ?place).
+    BIND (STRLEN(STR(?lab)) AS ?len)
+}
+ORDER BY ?place ?len ?lab
+""",
+                            'data-root-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+    ?uri a skos:Concept ;
+    skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> ;    
+    skos:prefLabel ?label .
+    FILTER (lang(?label) = ?language). 
+    FILTER NOT EXISTS { [skos:narrower ?uri] }
+}
+""",
+                            'data-childs-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT DISTINCT ?uri ?label
+WHERE {
+  ?uri skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> .
+  { ?uri a ?type
+    FILTER (?type = skos:Collection || ?type = skos:Concept) }.
+  ?root skos:narrower|skos:member ?uri.
+  ?uri skos:prefLabel|rdfs:label ?label.
+  FILTER (lang(?label) = ?language).
+}
+""",
+                            'data-child-count-query':
+"""
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT (COUNT(?uri) as ?nb)
+WHERE {
+    ?uri skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> .
+    ?root skos:narrower|skos:member ?uri.
+}
+"""
+                            }))
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/p4l/semantictree/forms/widgets.py	Wed Sep 04 16:56:55 2013 +0200
@@ -0,0 +1,86 @@
+"""
+Some useful form widgets
+"""
+
+from django import forms
+from django.core.exceptions import ValidationError
+from django.forms.util import flatatt
+from django.utils.html import format_html
+from django.utils.encoding import force_text
+from django.utils.translation import ugettext as _
+
+import logging
+logger = logging.getLogger(__name__)
+
+class SemanticTreeWidget(forms.TextInput):
+    """
+    A widget that enables to request semantic trees
+    """
+    
+    class Media:
+        css = {
+            'all': ('semantictree/semantictree.css',)
+        }
+        js = ('semantictree/semantictree.js',)
+    
+    # The user can add css classes but we always add "semantic-tree" class
+    def render(self, name, value, attrs=None):
+        if value is None:
+            value = ''
+        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
+        # Test if class attr is here
+        if "class" in final_attrs:
+            final_attrs["class"] += " semantic-tree"
+        else:
+            final_attrs["class"] = "semantic-tree"
+        if value != '':
+            # Only add the 'value' attribute if a value is non-empty.
+            final_attrs['value'] = force_text(self._format_value(value))
+        final_attrs['placeholder'] = _("Search")
+        input_res = format_html('<input{0} />', flatatt(final_attrs))
+        
+        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:
+            raise ValidationError(_('"data-url", "data-query", "data-root-query", "data-childs-query" and "data-child-count-query" must be set in CharField configuration'))
+        dialog_text = _("Browse")
+        
+        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)
+        dialog_res += '<span id="dialog-' + final_attrs["name"] + '" class="dialog" title="Select term"><span id="term-tree-' + final_attrs["name"] + '"></span></span>'
+        
+        return input_res + " " + _("or") + " " + dialog_res
+
+
+class SemanticTagItWidget(forms.TextInput):
+    """
+    A widget that enables to request semantic trees
+    """
+    
+    class Media:
+        css = {
+            'all': ('semantictree/semantictree.css',)
+        }
+        js = ('semantictree/semantictree.js',)
+    
+    # The user can add css classes but we always add "semantic-tree" class
+    def render(self, name, value, attrs=None):
+        if value is None:
+            value = ''
+        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
+        # Test if class attr is here
+        if "class" in final_attrs:
+            final_attrs["class"] += " semantic-tree-tagit"
+        else:
+            final_attrs["class"] = "semantic-tree-tagit"
+        if value != '':
+            # Only add the 'value' attribute if a value is non-empty.
+            final_attrs['value'] = force_text(self._format_value(value))
+        
+        input_res = format_html('<input{0} />', flatatt(final_attrs))
+        
+        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:
+            raise ValidationError(_('"data-url", "data-query", "data-root-query", "data-childs-query" and "data-child-count-query" must be set in CharField configuration'))
+        dialog_text = _("Browse")
+        
+        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)
+        dialog_res += '<span id="dialog-' + final_attrs["name"] + '" class="dialog" title="Select term"><span id="term-tree-' + final_attrs["name"] + '"></span></span>'
+        
+        return input_res + " " + _("or") + " " + dialog_res
\ No newline at end of file