| author | cavaliet |
| Mon, 18 Nov 2013 15:53:43 +0100 | |
| changeset 191 | d6c92b37252f |
| parent 91 | 3bbf7371378a |
| child 334 | 169b7cfd1f58 |
| permissions | -rw-r--r-- |
| 0 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on Jun 13, 2013 |
|
4 |
||
5 |
@author: ymh |
|
6 |
''' |
|
|
71
3fde7d26ad08
Add link_semantic_level to filter (bug #17542)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
7 |
from core.models import (Thesaurus, Term, TERM_URL_STATUS_CHOICES_TRANS, |
|
3fde7d26ad08
Add link_semantic_level to filter (bug #17542)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
8 |
TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES_TRANS) |
|
91
3bbf7371378a
Model reorganization for user + migration.
ymh <ymh.work@gmail.com>
parents:
82
diff
changeset
|
9 |
from django.forms import Form, fields, ModelChoiceField |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
10 |
from django.forms.util import flatatt |
| 75 | 11 |
from django.forms.widgets import (Widget, Select, |
12 |
NullBooleanSelect as DjangoNullBooleanSelect) |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
13 |
from django.utils import formats |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
14 |
from django.utils.encoding import force_text |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
15 |
from django.utils.html import format_html, escape |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
16 |
from django.utils.safestring import mark_safe |
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
17 |
from django.utils.translation import ugettext as _, ugettext_lazy |
| 67 | 18 |
from jocondelab import settings |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
19 |
import json |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
20 |
|
| 75 | 21 |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
22 |
class ThesaurusTreeWidget(Widget): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
23 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
24 |
def _format_value(self, value): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
25 |
if self.is_localized: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
26 |
return formats.localize_input(value) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
27 |
return value |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
28 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
29 |
def render(self, name, value, attrs=None): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
30 |
term = None |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
31 |
if isinstance(value, Term): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
32 |
term = value |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
33 |
elif value and isinstance(value, (int, basestring)): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
34 |
terms = list(Term.objects.filter(id=int(value))) # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
35 |
if terms: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
36 |
term = terms[0] |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
37 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
38 |
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
|
39 |
if term is not None: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
40 |
# 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
|
41 |
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
|
42 |
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
|
43 |
input_res = format_html('<input{0} />', flatatt(final_attrs)) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
44 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
45 |
if term is not None: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
46 |
dialog_text = term.label |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
47 |
else: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
48 |
dialog_text = _("Open Dialog") |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
49 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
50 |
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
|
51 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
52 |
return input_res + dialog_res |
| 0 | 53 |
|
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
54 |
class NullBooleanSelect(DjangoNullBooleanSelect): |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
55 |
""" |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
56 |
A Select Widget intended to be used with NullBooleanField. |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
57 |
""" |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
58 |
def __init__(self, attrs=None): |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
59 |
choices = (('1', ('---')), |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
60 |
('2', ugettext_lazy('yes')), |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
61 |
('3', ugettext_lazy('no'))) |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
62 |
Select.__init__(self, attrs, choices) |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
63 |
|
| 0 | 64 |
|
65 |
class ValidateTermForm(Form): |
|
66 |
term_id = fields.IntegerField(required=True) |
|
67 |
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
|
68 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
wikipedia_edition = fields.BooleanField(required=False) |
| 0 | 72 |
|
73 |
class ModifyWpLinkForm(Form): |
|
74 |
term_id = fields.IntegerField(required=True) |
|
75 |
label = fields.CharField(required=True, min_length=1) |
|
| 67 | 76 |
wp_lang = fields.ChoiceField(label=_('Wikipedia version'), required=False, choices=tuple([(k,k) for k in settings.WIKIPEDIA_URLS])) |
| 0 | 77 |
|
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
78 |
class LinkSemanticLevelForm(Form): |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
79 |
term_id = fields.IntegerField(required=True) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
80 |
link_semantic_level = fields.IntegerField(required=True) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
81 |
|
| 0 | 82 |
class RemoveWpLinkForm(Form): |
83 |
term_id = fields.IntegerField(required=True) |
|
84 |
||
85 |
class TermFilterForm(Form): |
|
| 43 | 86 |
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
|
87 |
thesaurus_tree = ModelChoiceField(label=_("thesaurus tree"), queryset=Term.objects.all(), required=False, widget=ThesaurusTreeWidget) # @UndefinedVariable |
| 15 | 88 |
label = fields.CharField(label=_("label"), required=False) |
89 |
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])) |
|
|
71
3fde7d26ad08
Add link_semantic_level to filter (bug #17542)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
90 |
link_semantic_level = fields.TypedChoiceField(label=_("link_semantic_level"), required=False, empty_value=0, coerce=int, choices=TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES_TRANS) |
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
91 |
wikipedia_edition = fields.NullBooleanField(label=_("wikipedia_edition"), required=False, widget=NullBooleanSelect) |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
92 |
validated = fields.NullBooleanField(label=_("validated"), required=False, widget=NullBooleanSelect) |
|
81
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
93 |
order_by = fields.ChoiceField(label=_("order_by"), required=False, choices=(('normalized_label',_('label')),('nb_notice',_('nb notice')),('level',_('level')),('lft', _('order_lft')))) |
| 15 | 94 |
order_dir = fields.ChoiceField(label=_("order_dir"), required=False, choices=(('asc',_('asc')), ('desc',_('desc')))) |
| 0 | 95 |
|
|
82
8a67acd1f3aa
disable sort order when sorting by node (not used in any case)
ymh <ymh.work@gmail.com>
parents:
81
diff
changeset
|
96 |
def __init__(self, *args, **kwargs): |
|
8a67acd1f3aa
disable sort order when sorting by node (not used in any case)
ymh <ymh.work@gmail.com>
parents:
81
diff
changeset
|
97 |
super(TermFilterForm, self).__init__(*args, **kwargs) |
|
8a67acd1f3aa
disable sort order when sorting by node (not used in any case)
ymh <ymh.work@gmail.com>
parents:
81
diff
changeset
|
98 |
if self.data.get('order_by', 'normalized_label') == 'lft': |
|
8a67acd1f3aa
disable sort order when sorting by node (not used in any case)
ymh <ymh.work@gmail.com>
parents:
81
diff
changeset
|
99 |
self.fields['order_dir'].widget.attrs['disabled'] = 'disabled' |
|
8a67acd1f3aa
disable sort order when sorting by node (not used in any case)
ymh <ymh.work@gmail.com>
parents:
81
diff
changeset
|
100 |
|
|
81
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
101 |
def clean(self): |
|
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
102 |
fields_not_empty = any([v for k,v in self.cleaned_data.items() if k not in ('thesaurus','thesaurus_tree', 'link_status', 'order_by', 'order_dir')] + [self.cleaned_data.get('link_status', -1) >= 0]) |
|
82
8a67acd1f3aa
disable sort order when sorting by node (not used in any case)
ymh <ymh.work@gmail.com>
parents:
81
diff
changeset
|
103 |
self.can_display_level = (not fields_not_empty) and (self.cleaned_data.get('thesaurus',None) is not None) and (self.cleaned_data.get('order_by','normalized_label') == 'lft') |
|
8a67acd1f3aa
disable sort order when sorting by node (not used in any case)
ymh <ymh.work@gmail.com>
parents:
81
diff
changeset
|
104 |
self.selected_thesaurus = self.cleaned_data.get('thesaurus', None) |
|
81
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
105 |
return super(TermFilterForm, self).clean() |
|
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
106 |
|
|
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
107 |
|
| 0 | 108 |
def get_filter_qs(self, base_qs=None): |
109 |
qs = base_qs |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
110 |
|
| 0 | 111 |
if qs is None: |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
112 |
qs = Term.objects.all() # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
113 |
|
| 0 | 114 |
thes = self.cleaned_data.get('thesaurus',None) |
115 |
if thes: |
|
116 |
qs = qs.filter(thesaurus=thes) |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
117 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
118 |
thes_tree = self.cleaned_data.get('thesaurus_tree',None) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
119 |
if thes_tree: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
120 |
qs = qs & thes_tree.get_descendants() # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
121 |
|
| 0 | 122 |
lk_status = self.cleaned_data.get('link_status',-1) |
123 |
if lk_status>=0: |
|
124 |
qs = qs.filter(url_status=lk_status) |
|
125 |
||
|
71
3fde7d26ad08
Add link_semantic_level to filter (bug #17542)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
126 |
lk_semantic_level = self.cleaned_data.get('link_semantic_level', 0) |
|
3fde7d26ad08
Add link_semantic_level to filter (bug #17542)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
127 |
if lk_semantic_level: |
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
128 |
qs = qs.filter(link_semantic_level=lk_semantic_level) |
|
71
3fde7d26ad08
Add link_semantic_level to filter (bug #17542)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
129 |
|
| 0 | 130 |
validated = self.cleaned_data.get('validated', None) |
131 |
if validated is not None: |
|
132 |
qs = qs.filter(validated=validated) |
|
133 |
||
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
134 |
wikipedia_edition = self.cleaned_data.get('wikipedia_edition', None) |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
135 |
if wikipedia_edition is not None: |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
136 |
qs = qs.filter(wikipedia_edition=wikipedia_edition) |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
137 |
|
| 0 | 138 |
label_regexp = self.cleaned_data.get('label', None) |
139 |
if label_regexp: |
|
140 |
qs = qs.filter(label__iregex=label_regexp) |
|
141 |
||
142 |
order_by = self.cleaned_data.get('order_by', 'normalized_label') or 'normalized_label' |
|
143 |
order_dir = self.cleaned_data.get('order_dir', 'asc') or 'asc' |
|
144 |
if order_dir == 'desc': |
|
|
81
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
145 |
dir_order_by = "-"+order_by |
|
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
146 |
else: |
|
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
147 |
dir_order_by = order_by |
| 51 | 148 |
if order_by == "normalized_label" or order_by == "label": |
|
81
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
149 |
order_by = [dir_order_by, 'nb_notice', 'id'] |
|
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
150 |
elif order_by == "lft": |
|
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
151 |
order_by = ['tree_id', order_by, 'nb_notice', 'id'] |
| 51 | 152 |
else: |
|
81
e78e5a2017b6
Add a way to show trees in the result list for terms.
ymh <ymh.work@gmail.com>
parents:
75
diff
changeset
|
153 |
order_by = [dir_order_by, 'normalized_label', 'id'] |
| 51 | 154 |
qs = qs.order_by(*order_by) |
| 0 | 155 |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
156 |
return qs |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
157 |