| author | ymh <ymh.work@gmail.com> |
| Thu, 18 Jul 2013 10:39:26 +0200 | |
| changeset 67 | 5d9223bb3aab |
| parent 62 | 33fd91a414cc |
| child 71 | 3fde7d26ad08 |
| permissions | -rw-r--r-- |
| 0 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on Jun 13, 2013 |
|
4 |
||
5 |
@author: ymh |
|
6 |
''' |
|
| 15 | 7 |
from core.models import Thesaurus, Term, TERM_URL_STATUS_CHOICES_TRANS |
| 0 | 8 |
from django.forms import Form, fields, ModelChoiceField |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
9 |
from django.forms.util import flatatt |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
10 |
from django.forms.widgets import Widget |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
11 |
from django.utils import formats |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
12 |
from django.utils.encoding import force_text |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
13 |
from django.utils.html import format_html, escape |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
14 |
from django.utils.safestring import mark_safe |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
15 |
from django.utils.translation import ugettext as _ |
| 67 | 16 |
from jocondelab import settings |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
17 |
import json |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
18 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
19 |
class ThesaurusTreeWidget(Widget): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
20 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
21 |
def _format_value(self, value): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
22 |
if self.is_localized: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
23 |
return formats.localize_input(value) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
24 |
return value |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
25 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
26 |
def render(self, name, value, attrs=None): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
27 |
term = None |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
28 |
if isinstance(value, Term): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
29 |
term = value |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
30 |
elif value and isinstance(value, (int, basestring)): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
31 |
terms = list(Term.objects.filter(id=int(value))) # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
32 |
if terms: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
33 |
term = terms[0] |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
34 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
35 |
final_attrs = self.build_attrs(attrs, type="hidden", name=name) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
36 |
if term is not None: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
37 |
# Only add the 'value' attribute if a value is non-empty. |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
38 |
final_attrs['value'] = force_text(self._format_value(term.id)) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
39 |
final_attrs['data-term_tree_node'] = mark_safe(json.dumps({'id': term.id, 'label': escape(term.label)}).replace("\"", "'")); |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
40 |
input_res = format_html('<input{0} />', flatatt(final_attrs)) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
41 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
42 |
if term is not None: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
43 |
dialog_text = term.label |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
44 |
else: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
45 |
dialog_text = _("Open Dialog") |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
46 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
47 |
dialog_res = "<div id=\"dialog-link-container\" class=\"ui-state-default ui-corner-all\"><a href=\"#\" id=\"dialog-link\" title=\"%s\">%s</a><span class=\"ui-icon ui-icon-closethick\" id=\"dialog-deselect\"></span></div>" % (_("Open Dialog"),dialog_text) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
48 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
49 |
return input_res + dialog_res |
| 0 | 50 |
|
51 |
||
52 |
class ValidateTermForm(Form): |
|
53 |
term_id = fields.IntegerField(required=True) |
|
54 |
validation_val = fields.BooleanField(required=False) |
|
|
26
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
55 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
56 |
class WikipediaEditionForm(Form): |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
57 |
term_id = fields.IntegerField(required=True) |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
58 |
wikipedia_edition = fields.BooleanField(required=False) |
| 0 | 59 |
|
60 |
class ModifyWpLinkForm(Form): |
|
61 |
term_id = fields.IntegerField(required=True) |
|
62 |
label = fields.CharField(required=True, min_length=1) |
|
| 67 | 63 |
wp_lang = fields.ChoiceField(label=_('Wikipedia version'), required=False, choices=tuple([(k,k) for k in settings.WIKIPEDIA_URLS])) |
| 0 | 64 |
|
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
65 |
class LinkSemanticLevelForm(Form): |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
66 |
term_id = fields.IntegerField(required=True) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
67 |
link_semantic_level = fields.IntegerField(required=True) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
68 |
|
| 0 | 69 |
class RemoveWpLinkForm(Form): |
70 |
term_id = fields.IntegerField(required=True) |
|
71 |
||
72 |
def validated_to_bool(val): |
|
73 |
if isinstance(val, basestring) and len(val)==0: |
|
74 |
return None |
|
75 |
else: |
|
76 |
return bool(int(val)) |
|
77 |
||
78 |
class TermFilterForm(Form): |
|
| 43 | 79 |
thesaurus = ModelChoiceField(label=_("thesaurus"), required=False, queryset=Thesaurus.objects.all().order_by('label')) |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
80 |
thesaurus_tree = ModelChoiceField(label=_("thesaurus tree"), queryset=Term.objects.all(), required=False, widget=ThesaurusTreeWidget) # @UndefinedVariable |
| 15 | 81 |
label = fields.CharField(label=_("label"), required=False) |
82 |
link_status = fields.TypedChoiceField(label=_("link_status"), required=False, empty_value=-1, coerce=int, choices=tuple([(-1,'---------')]+[(v, l) for v,l in TERM_URL_STATUS_CHOICES_TRANS])) |
|
83 |
validated = fields.TypedChoiceField(label=_("validated"), required=False, empty_value=None, coerce=validated_to_bool, choices=(("", "---"),("1", _("yes")), ("0", _("no")))) |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
84 |
order_by = fields.ChoiceField(label=_("order_by"), required=False, choices=(('normalized_label',_('label')),('nb_notice',_('nb notice')),('level',_('level')))) |
| 15 | 85 |
order_dir = fields.ChoiceField(label=_("order_dir"), required=False, choices=(('asc',_('asc')), ('desc',_('desc')))) |
| 0 | 86 |
|
87 |
def get_filter_qs(self, base_qs=None): |
|
88 |
qs = base_qs |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
89 |
|
| 0 | 90 |
if qs is None: |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
91 |
qs = Term.objects.all() # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
92 |
|
| 0 | 93 |
thes = self.cleaned_data.get('thesaurus',None) |
94 |
if thes: |
|
95 |
qs = qs.filter(thesaurus=thes) |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
96 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
97 |
thes_tree = self.cleaned_data.get('thesaurus_tree',None) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
98 |
if thes_tree: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
99 |
qs = qs & thes_tree.get_descendants() # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
100 |
|
| 0 | 101 |
lk_status = self.cleaned_data.get('link_status',-1) |
102 |
if lk_status>=0: |
|
103 |
qs = qs.filter(url_status=lk_status) |
|
104 |
||
105 |
validated = self.cleaned_data.get('validated', None) |
|
106 |
if validated is not None: |
|
107 |
qs = qs.filter(validated=validated) |
|
108 |
||
109 |
label_regexp = self.cleaned_data.get('label', None) |
|
110 |
if label_regexp: |
|
111 |
qs = qs.filter(label__iregex=label_regexp) |
|
112 |
||
113 |
order_by = self.cleaned_data.get('order_by', 'normalized_label') or 'normalized_label' |
|
114 |
order_dir = self.cleaned_data.get('order_dir', 'asc') or 'asc' |
|
115 |
if order_dir == 'desc': |
|
116 |
order_by = "-"+order_by |
|
| 51 | 117 |
if order_by == "normalized_label" or order_by == "label": |
118 |
order_by = [order_by, 'nb_notice', 'id'] |
|
119 |
else: |
|
120 |
order_by = [order_by, 'normalized_label', 'id'] |
|
121 |
qs = qs.order_by(*order_by) |
|
| 0 | 122 |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
123 |
return qs |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
124 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
125 |