| author | ymh <ymh.work@gmail.com> |
| Mon, 22 Jul 2013 15:31:17 +0200 | |
| changeset 75 | 702760606a16 |
| parent 72 | db80ba79fb52 |
| child 81 | e78e5a2017b6 |
| 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) |
| 75 | 9 |
from django.contrib.auth import get_user_model |
10 |
from django.contrib.auth.forms import (UserChangeForm as AuthUserChangeForm, |
|
11 |
UserCreationForm as AuthUserCreationForm) |
|
12 |
from django.forms import Form, fields, ModelChoiceField, ValidationError |
|
13 |
from django.forms.fields import ChoiceField |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
14 |
from django.forms.util import flatatt |
| 75 | 15 |
from django.forms.widgets import (Widget, Select, |
16 |
NullBooleanSelect as DjangoNullBooleanSelect) |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
17 |
from django.utils import formats |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
18 |
from django.utils.encoding import force_text |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
19 |
from django.utils.html import format_html, escape |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
20 |
from django.utils.safestring import mark_safe |
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
21 |
from django.utils.translation import ugettext as _, ugettext_lazy |
| 67 | 22 |
from jocondelab import settings |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
23 |
import json |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
24 |
|
| 75 | 25 |
User = get_user_model() |
26 |
||
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
27 |
class ThesaurusTreeWidget(Widget): |
|
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 _format_value(self, value): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
30 |
if self.is_localized: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
31 |
return formats.localize_input(value) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
32 |
return value |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
33 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
34 |
def render(self, name, value, attrs=None): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
35 |
term = None |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
36 |
if isinstance(value, Term): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
37 |
term = value |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
38 |
elif value and isinstance(value, (int, basestring)): |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
39 |
terms = list(Term.objects.filter(id=int(value))) # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
40 |
if terms: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
41 |
term = terms[0] |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
42 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
43 |
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
|
44 |
if term is not None: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
45 |
# 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
|
46 |
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
|
47 |
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
|
48 |
input_res = format_html('<input{0} />', flatatt(final_attrs)) |
|
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 |
if term is not None: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
51 |
dialog_text = term.label |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
52 |
else: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
53 |
dialog_text = _("Open Dialog") |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
54 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
55 |
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
|
56 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
57 |
return input_res + dialog_res |
| 0 | 58 |
|
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
59 |
class NullBooleanSelect(DjangoNullBooleanSelect): |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
60 |
""" |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
61 |
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
|
62 |
""" |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
63 |
def __init__(self, attrs=None): |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
64 |
choices = (('1', ('---')), |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
65 |
('2', ugettext_lazy('yes')), |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
66 |
('3', ugettext_lazy('no'))) |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
67 |
Select.__init__(self, attrs, choices) |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
68 |
|
| 0 | 69 |
|
70 |
class ValidateTermForm(Form): |
|
71 |
term_id = fields.IntegerField(required=True) |
|
72 |
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
|
73 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
wikipedia_edition = fields.BooleanField(required=False) |
| 0 | 77 |
|
78 |
class ModifyWpLinkForm(Form): |
|
79 |
term_id = fields.IntegerField(required=True) |
|
80 |
label = fields.CharField(required=True, min_length=1) |
|
| 67 | 81 |
wp_lang = fields.ChoiceField(label=_('Wikipedia version'), required=False, choices=tuple([(k,k) for k in settings.WIKIPEDIA_URLS])) |
| 0 | 82 |
|
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
83 |
class LinkSemanticLevelForm(Form): |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
84 |
term_id = fields.IntegerField(required=True) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
85 |
link_semantic_level = fields.IntegerField(required=True) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
86 |
|
| 0 | 87 |
class RemoveWpLinkForm(Form): |
88 |
term_id = fields.IntegerField(required=True) |
|
89 |
||
90 |
class TermFilterForm(Form): |
|
| 43 | 91 |
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
|
92 |
thesaurus_tree = ModelChoiceField(label=_("thesaurus tree"), queryset=Term.objects.all(), required=False, widget=ThesaurusTreeWidget) # @UndefinedVariable |
| 15 | 93 |
label = fields.CharField(label=_("label"), required=False) |
94 |
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
|
95 |
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
|
96 |
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
|
97 |
validated = fields.NullBooleanField(label=_("validated"), required=False, widget=NullBooleanSelect) |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
98 |
order_by = fields.ChoiceField(label=_("order_by"), required=False, choices=(('normalized_label',_('label')),('nb_notice',_('nb notice')),('level',_('level')))) |
| 15 | 99 |
order_dir = fields.ChoiceField(label=_("order_dir"), required=False, choices=(('asc',_('asc')), ('desc',_('desc')))) |
| 0 | 100 |
|
101 |
def get_filter_qs(self, base_qs=None): |
|
102 |
qs = base_qs |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
103 |
|
| 0 | 104 |
if qs is None: |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
105 |
qs = Term.objects.all() # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
106 |
|
| 0 | 107 |
thes = self.cleaned_data.get('thesaurus',None) |
108 |
if thes: |
|
109 |
qs = qs.filter(thesaurus=thes) |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
110 |
|
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
111 |
thes_tree = self.cleaned_data.get('thesaurus_tree',None) |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
112 |
if thes_tree: |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
113 |
qs = qs & thes_tree.get_descendants() # @UndefinedVariable |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
114 |
|
| 0 | 115 |
lk_status = self.cleaned_data.get('link_status',-1) |
116 |
if lk_status>=0: |
|
117 |
qs = qs.filter(url_status=lk_status) |
|
118 |
||
|
71
3fde7d26ad08
Add link_semantic_level to filter (bug #17542)
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
119 |
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
|
120 |
if lk_semantic_level: |
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
121 |
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
|
122 |
|
| 0 | 123 |
validated = self.cleaned_data.get('validated', None) |
124 |
if validated is not None: |
|
125 |
qs = qs.filter(validated=validated) |
|
126 |
||
|
72
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
127 |
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
|
128 |
if wikipedia_edition is not None: |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
129 |
qs = qs.filter(wikipedia_edition=wikipedia_edition) |
|
db80ba79fb52
Add filter on "wikipedia edition" field.
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
130 |
|
| 0 | 131 |
label_regexp = self.cleaned_data.get('label', None) |
132 |
if label_regexp: |
|
133 |
qs = qs.filter(label__iregex=label_regexp) |
|
134 |
||
135 |
order_by = self.cleaned_data.get('order_by', 'normalized_label') or 'normalized_label' |
|
136 |
order_dir = self.cleaned_data.get('order_dir', 'asc') or 'asc' |
|
137 |
if order_dir == 'desc': |
|
138 |
order_by = "-"+order_by |
|
| 51 | 139 |
if order_by == "normalized_label" or order_by == "label": |
140 |
order_by = [order_by, 'nb_notice', 'id'] |
|
141 |
else: |
|
142 |
order_by = [order_by, 'normalized_label', 'id'] |
|
143 |
qs = qs.order_by(*order_by) |
|
| 0 | 144 |
|
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
145 |
return qs |
|
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
146 |
|
| 75 | 147 |
class UserCreationform(AuthUserCreationForm): |
148 |
class Meta: |
|
149 |
model = User |
|
150 |
||
151 |
def clean_username(self): |
|
152 |
# Since User.username is unique, this check is redundant, |
|
153 |
# but it sets a nicer error message than the ORM. See #13147. |
|
154 |
username = self.cleaned_data["username"] |
|
155 |
try: |
|
156 |
User.objects.get(username=username) |
|
157 |
except User.DoesNotExist: |
|
158 |
return username |
|
159 |
raise ValidationError(self.error_messages['duplicate_username']) |
|
160 |
||
161 |
||
162 |
class UserChangeForm(AuthUserChangeForm): |
|
163 |
language = ChoiceField(label=_("language"), choices=[(k,_(v)) for k,v in settings.LANGUAGES], initial=settings.LANGUAGE_CODE[:2]) |
|
164 |
class Meta: |
|
165 |
model = User |