src/jocondelab/forms.py
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--
Add other wikipedia. change joconde notice preview (as in bug #17508)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Jun 13, 2013
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
15
8440c36660e5 add translation,
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     7
from core.models import Thesaurus, Term, TERM_URL_STATUS_CHOICES_TRANS
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
     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
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    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
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
class ValidateTermForm(Form):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    term_id = fields.IntegerField(required=True)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
class ModifyWpLinkForm(Form):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    term_id = fields.IntegerField(required=True)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    label = fields.CharField(required=True, min_length=1)
67
5d9223bb3aab Add other wikipedia.
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    63
    wp_lang = fields.ChoiceField(label=_('Wikipedia version'), required=False, choices=tuple([(k,k) for k in settings.WIKIPEDIA_URLS]))
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
class RemoveWpLinkForm(Form):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    term_id = fields.IntegerField(required=True)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
def validated_to_bool(val):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    if isinstance(val, basestring) and len(val)==0:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        return None
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    else:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        return bool(int(val))
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
         
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
class TermFilterForm(Form):
43
b9baab399b4d add nb_notice columns
ymh <ymh.work@gmail.com>
parents: 35
diff changeset
    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
8440c36660e5 add translation,
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    81
    label = fields.CharField(label=_("label"), required=False)
8440c36660e5 add translation,
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    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]))
8440c36660e5 add translation,
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    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
8440c36660e5 add translation,
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    85
    order_dir = fields.ChoiceField(label=_("order_dir"), required=False, choices=(('asc',_('asc')), ('desc',_('desc'))))
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    def get_filter_qs(self, base_qs=None):
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        qs = base_qs
62
33fd91a414cc selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    89
        
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        thes = self.cleaned_data.get('thesaurus',None)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        if thes:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        lk_status = self.cleaned_data.get('link_status',-1)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        if lk_status>=0:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
            qs = qs.filter(url_status=lk_status)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
            
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        validated = self.cleaned_data.get('validated', None)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        if validated is not None:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
            qs = qs.filter(validated=validated)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
            
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        label_regexp = self.cleaned_data.get('label', None)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        if label_regexp:
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
            qs = qs.filter(label__iregex=label_regexp)
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        order_by = self.cleaned_data.get('order_by', 'normalized_label') or 'normalized_label'
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        order_dir = self.cleaned_data.get('order_dir', 'asc') or 'asc'
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
        if order_dir == 'desc':
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
            order_by = "-"+order_by
51
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   117
        if order_by == "normalized_label" or order_by == "label":
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   118
            order_by = [order_by, 'nb_notice', 'id']
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   119
        else:
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   120
            order_by = [order_by, 'normalized_label', 'id']
8890db197b37 add prev and next buttons
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   121
        qs = qs.order_by(*order_by)
0
4095911a7830 Jocondelab first commit before design
ymh <ymh.work@gmail.com>
parents:
diff changeset
   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