Ejemplo n.º 1
0
 def _getAllChoices(self, value):
     value = value or []
     choices = list(self.choices)
     choices_keys = [i[0] for i in choices]
     for i in value:
         if not i in choices_keys:
             obj = utils.getObject(self.model, {"pk": i}, self.select_related)
             choices.append((i, unicode(obj)))
     choices.sort(key=operator.itemgetter(1))
     return choices
 def _getAllChoices(self, value):
     value = value or []
     choices = list(self.choices)
     # convert to unicode for safe comparisong during a ValidationError
     choices_keys = [unicode(i[0]) for i in choices]
     for i in value:
         if not unicode(i) in choices_keys:
             obj = utils.getObject(self.model, {"pk": i}, self.select_related)
             choices.append((i, unicode(obj)))
     choices.sort(key=operator.itemgetter(1))
     return choices
Ejemplo n.º 3
0
 def _getAllChoices(self, value):
     value = value or []
     choices = list(self.choices)
     choices_keys = [i[0] for i in choices]
     for i in value:
         if not i in choices_keys:
             obj = utils.getObject(self.model, {"pk": i},
                                   self.select_related)
             choices.append((i, unicode(obj)))
     choices.sort(key=operator.itemgetter(1))
     return choices
 def _getAllChoices(self, value):
     value = value or []
     choices = list(self.choices)
     # convert to unicode for safe comparisong during a ValidationError
     choices_keys = [unicode(i[0]) for i in choices]
     for i in value:
         if not unicode(i) in choices_keys:
             obj = utils.getObject(self.model, {"pk": i},
                                   self.select_related)
             choices.append((i, unicode(obj)))
     choices.sort(key=operator.itemgetter(1))
     return choices
    def render(self, name, value, attrs=None, choices=()):
        self._element_id = attrs['id']
        # choices links
        # if there is only one choice, then nothing will be rendered
        lookups_output = ""
        lookups = utils.getLookups(self.lookups)
        if len(lookups) > 1:
            js_method_name = "getForeignKeyJSON"
            lookups_output = "\n".join(
                _renderFilter(js_method_name, self._element_id, self.model, i,
                              self.select_related) for i in lookups)

        # get the selected object name
        selection = "-" * 9
        if value:
            selection = utils.getObject(self.model, {"pk": value},
                                        self.select_related)

        # filter selectbox input
        filter_id = "%s_input" % self._element_id

        # give a style to the final select widget
        _attrs = {"size": 2, "style": "width:270px;"}
        try:
            attrs.update(_attrs)
        except AttributeError:
            attrs = _attrs

        # normal widget output from the anchestor
        # create a field with a dummy name , the real value
        # will be retrieved from a hidden field
        parent_output = super(FilteredSelect,
                              self).render("dummy-%s" % name, value, attrs,
                                           choices)

        # output
        mapping = {
            "lookups_output": lookups_output,
            "selection": selection,
            "filter_id": filter_id,
            "parent_output": parent_output,
            "name": name,
            "element_id": self._element_id,
            "value": "" if value is None else value,
            "static_url": settings.STATIC_URL,
        }

        output = u"""
            <div class="selector">
                %(lookups_output)s
            </div>
            
            <div class="selector">
                <div class="selector-available">
                    <h2>%(selection)s</h2>
                    <p class="selector-filter">
                        <img src="%(static_url)sadmin/img/search.svg">
                        <input id="%(filter_id)s" type="text">
                    </p>
                    %(parent_output)s
                </div>
            </div>
            
            <input type="hidden" name="%(name)s" id="hidden-%(element_id)s" value="%(value)s" />
            
            <script type="text/javascript" charset="utf-8">
                (function($) {
                  $(document).ready(function(){
                    SelectBox.init('%(element_id)s');

                    $("#%(filter_id)s").bind("keyup", function(e) {
                        SelectBox.filter("%(element_id)s", $("#%(filter_id)s").val())
                    });
                    
                    $(".ajax_letter").click(function(e) {
                        $("#%(filter_id)s").val("");
                    });
                    
                    ajax_filtered_fields.bindForeignKeyOptions("%(element_id)s");
        	  });
                })(django.jQuery);
        	</script>
            """ % mapping

        return mark_safe(output)
Ejemplo n.º 6
0
    def render(self, name, value, attrs=None, choices=()):
        self._element_id = attrs['id']
        # choices links
        # if there is only one choice, then nothing will be rendered
        lookups_output = ""
        lookups = utils.getLookups(self.lookups)
        if len(lookups) > 1:
            js_method_name = "getForeignKeyJSON"
            lookups_output = "\n".join(
                _renderFilter(js_method_name, self._element_id, 
                    self.model, i, self.select_related) 
                for i in lookups)
                
        # get the selected object name
        selection = "-" * 9
        if value:
            selection = utils.getObject(self.model, {"pk": value}, 
                self.select_related)
        
        # filter selectbox input
        filter_id = "%s_input" % self._element_id
        
        # give a style to the final select widget
        _attrs = {"size": 2, "style": "width:670px;"}
        try:
            attrs.update(_attrs)
        except AttributeError:
            attrs = _attrs
            
        # normal widget output from the anchestor
        # create a field with a dummy name , the real value
        # will be retrieved from a hidden field
        parent_output = super(FilteredSelect, self
            ).render("dummy-%s" % name, value, attrs, choices)
        
        # output
        mapping = {
            "lookups_output": lookups_output,
            "selection": selection,
            "filter_id": filter_id,
            "parent_output": parent_output,
            "name": name,
            "element_id": self._element_id, 
            "value": "" if value is None else value,
            }
                            
        output = u"""
            <div class="selector">
                %(lookups_output)s
            </div>
            
            <div class="selector">
                <div class="selector-available">
                    <h2>%(selection)s</h2>
                    <p class="selector-filter">
                        <img src="/media/img/admin/selector-search.gif"> 
                        <input id="%(filter_id)s" type="text">
                    </p>
                    %(parent_output)s
                </div>
            </div>
            
            <input type="hidden" name="%(name)s" id="hidden-%(element_id)s" value="%(value)s" />
            
            <script type="text/javascript" charset="utf-8">
        		$(document).ready(function(){
                    SelectBox.init('%(element_id)s');

                    $("#%(filter_id)s").bind("keyup", function(e) {
                        SelectBox.filter("%(element_id)s", $("#%(filter_id)s").val())
                    });
                    
                    $(".ajax_letter").click(function(e) {
                        $("#%(filter_id)s").val("");
                    });
                    
                    ajax_filtered_fields.bindForeignKeyOptions("%(element_id)s");
        		});
        	</script>
            """ % mapping
            
        return mark_safe(output)