ajax select
authorOlivier Larchevêque <olivier.larcheveque@auf.org>
Wed, 8 Feb 2012 20:29:06 +0000 (15:29 -0500)
committerOlivier Larchevêque <olivier.larcheveque@auf.org>
Wed, 8 Feb 2012 20:29:06 +0000 (15:29 -0500)
project/monkey.py

index 888b205..a6a96e2 100644 (file)
@@ -65,3 +65,55 @@ def value_from_datadict(self, data, files, name):
         return None
 
 AutoCompleteSelectWidget.value_from_datadict = value_from_datadict
+
+from django.conf import settings
+from django.core.urlresolvers import reverse
+from django.utils.safestring import mark_safe
+from django.forms.util import flatatt
+from django.template.loader import render_to_string
+
+def patched_render(self, name, value, attrs=None):
+    """
+    prevention contre les choses supprimees   
+    """
+    # precheck existence valeur
+    lookup = get_lookup(self.channel)
+    if value:
+        objs = lookup.get_objects([value])
+        if len(objs) == 0:
+            value = ''
+
+    # copie de django_ajax_selects-1.1.4-py2.6.egg/ajax_select/fields.py
+    value = value or ''
+    final_attrs = self.build_attrs(attrs)
+    self.html_id = final_attrs.pop('id', name)
+
+    lookup = get_lookup(self.channel)
+    if value:
+        objs = lookup.get_objects([value])
+        try:
+            obj = objs[0]
+        except IndexError:
+            raise Exception("%s cannot find object:%s" % (lookup, value))
+        current_result = mark_safe(lookup.format_item( obj ) )
+    else:
+        current_result = ''
+
+    context = {
+            'name': name,
+            'html_id' : self.html_id,
+            'lookup_url': reverse('ajax_lookup',kwargs={'channel':self.channel}),
+            'current_id': value,
+            'current_result': current_result,
+            'help_text': self.help_text,
+            'extra_attrs': mark_safe(flatatt(final_attrs)),
+            'func_slug': self.html_id.replace("-",""),
+            'add_link' : self.add_link,
+            'admin_media_prefix' : settings.ADMIN_MEDIA_PREFIX
+            }
+
+    return mark_safe(render_to_string(('autocompleteselect_%s.html' % self.channel, 'autocompleteselect.html'),context))
+
+
+
+AutoCompleteSelectWidget.render = patched_render