More bug fixes. And added 'auto_id' parameter for auto fields.

This commit is contained in:
AppleGrew (applegrew) 2012-08-22 11:19:30 +05:30
parent 6a2c96668d
commit 878ddb839a
10 changed files with 42 additions and 19 deletions

View file

@ -24,4 +24,6 @@ Changelog Summary
### v2.0
Mostly major bug fixes in code and design. The changes were many, raising the possibility of backward incompatibilty. However, the backward incompatibilty would be subtle.
* Mostly major bug fixes in code and design. The changes were many, raising the possibility of backward incompatibilty. However, the backward incompatibilty would be subtle.
* Auto fields (sub-classes of AutoViewFieldMixin) now accepts `auto_id` parameter. This can be used to provide custom id for the field. The default is 'module.field_class_name'. Ideally only the first instance of an auto field is registered. This parameter can be used to force registration of additional instances by passing a unique value.

View file

@ -5,12 +5,12 @@ logger = logging.getLogger(__name__)
class AutoViewFieldMixin(object):
"""Registers itself with AutoResponseView."""
def __init__(self, *args, **kwargs):
name = self.__class__.__name__
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Registering auto field: %s.%s", self.__module__, name)
name = kwargs.pop('auto_id', u"%s.%s" % (self.__module__, self.__class__.__name__))
if logger.isEnabledFor(logging.INFO):
logger.info("Registering auto field: %s", name)
from .util import register_field
id_ = register_field("%s.%s" % (self.__module__, name), self)
id_ = register_field(name, self)
self.widget.field_id = id_
super(AutoViewFieldMixin, self).__init__(*args, **kwargs)

View file

@ -221,10 +221,16 @@ var django_select2 = {
// value, based on if element is multiple type.
$.fn.txt = function(val) {
if (typeof(val) !== 'undefined') {
if (val instanceof Array) {
val = django_select2.convertArrToStr(val);
if (val) {
if (val instanceof Array) {
if (this.attr('multiple')) {
val = django_select2.convertArrToStr(val);
} else {
val = val[0]
}
}
this.attr('txt', val);
}
this.attr('txt', val);
return this;
} else {
val = this.attr('txt');

View file

@ -1,6 +1,9 @@
import types
import logging
from django.utils.html import escape
from django.utils.encoding import force_unicode
logger = logging.getLogger(__name__)
def render_js_script(inner_code):
@ -42,7 +45,7 @@ def convert_py_to_js_data(val, id_):
if type(val) == types.BooleanType:
return u'true' if val else u'false'
elif type(val) in [types.IntType, types.LongType, types.FloatType]:
return unicode(val)
return force_unicode(val)
elif isinstance(val, JSFunctionInContext):
return u"django_select2.runInContextHelper(%s, '%s')" % (val, id_)
elif isinstance(val, JSVar):
@ -52,7 +55,7 @@ def convert_py_to_js_data(val, id_):
elif isinstance(val, list):
return convert_to_js_arr(val, id_)
else:
return u"'%s'" % unicode(val)
return u"'%s'" % force_unicode(val)
def convert_dict_to_js_map(dct, id_):
out = u'{'
@ -82,7 +85,7 @@ def convert_to_js_arr(lst, id_):
return out + u']'
def convert_to_js_string_arr(lst):
lst = ['"%s"' % l for l in lst]
lst = [u'"%s"' % force_unicode(l) for l in lst]
return u"[%s]" % (",".join(lst))
### Auto view helper utils ###
@ -119,17 +122,17 @@ def register_field(name, field):
if name not in __field_store:
# Generating id
id_ = "%d:%s" % (len(__id_store), str(datetime.datetime.now()))
id_ = u"%d:%s" % (len(__id_store), unicode(datetime.datetime.now()))
__field_store[name] = id_
__id_store[id_] = field
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Registering new field: %s; With actual id: %s", name, id_)
if logger.isEnabledFor(logging.INFO):
logger.info("Registering new field: %s; With actual id: %s", name, id_)
else:
id_ = __field_store[name]
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Field already registered: %s; With actual id: %s", name, id_)
if logger.isEnabledFor(logging.INFO):
logger.info("Field already registered: %s; With actual id: %s", name, id_)
return id_
def get_field(id_):

View file

@ -2,6 +2,7 @@ import logging
from itertools import chain
from django import forms
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse
from django.utils.datastructures import MultiValueDict, MergeDict
@ -149,9 +150,11 @@ class HeavySelect2Mixin(Select2Mixin):
super(HeavySelect2Mixin, self).__init__(**kwargs)
def render_texts(self, selected_choices, choices):
selected_choices = list(force_unicode(v) for v in selected_choices)
txts = []
all_choices = choices if choices else []
for val, txt in chain(self.choices, all_choices):
val = force_unicode(val)
if val in selected_choices:
txts.append(txt)
if txts:
@ -169,7 +172,7 @@ class HeavySelect2Mixin(Select2Mixin):
values = [value] # Just like forms.Select.render() it assumes that value will be single valued.
texts = self.render_texts(values, choices)
if texts:
return u"$('#%s').attr('txt', %s);" % (id_, texts)
return u"$('#%s').txt(%s);" % (id_, texts)
def render_inner_js_code(self, id_, name, value, attrs=None, choices=(), *args):
js = u"$('#%s').change(django_select2.onValChange).data('userGetValText', %s);" \

Binary file not shown.

View file

@ -162,7 +162,7 @@ LOGGING = {
'django_select2': {
'handlers':['console'],
'propagate': True,
'level':'DEBUG',
'level':'INFO',
},
'django.request': {
'handlers': ['mail_admins'],

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,8 @@
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="{{ STATIC_URL }}jquery-1.7.2.min.js"></script>
</head>
<body>
<form method="post" action="">

View file

@ -26,3 +26,7 @@ class DeptForm(forms.ModelForm):
class Meta:
model = Dept
# These are just for testing Auto registration of fields
EmployeeChoices() # Should already be registered
EmployeeChoices(auto_id="EmployeeChoices_CustomAutoId") # Should get registered