mirror of
https://github.com/Hopiu/django-select2.git
synced 2026-05-19 02:11:06 +00:00
Adds render_inner_js_code to HeavySelect2TagWidget
Add a test for HeavyModelSelect2TagField in the testapp. The labels for selected options aren't displayed correctly in the test. I updated HeavySelect2TagWidget to override render_inner_js_code, and now it seems to work properly. Closes #133 Squashed commit of the following: commit 94895a8743b0c4ebdf888da08827486106a5b15d Author: Ben Cail <benjamin_cail@brown.edu> Date: Tue Nov 18 15:11:24 2014 -0500 use view name instead of hard-coded url commit fb9e1444ed73f2c8bad071182e8919dbf4f6bfe6 Author: Ben Cail <benjamin_cail@brown.edu> Date: Mon Nov 17 14:31:50 2014 -0500 override render_inner_js_code in HeavySelect2TagWidget (similar to AutoHeavySelect2Mixin) commit feff9c2a76474121aa2ff9c9b98a4f7dbfb0ab49 Author: Ben Cail <benjamin_cail@brown.edu> Date: Mon Nov 17 14:21:29 2014 -0500 add test for HeavyModelSelect2TagField
This commit is contained in:
parent
f637d4ec49
commit
91dad724fb
5 changed files with 64 additions and 4 deletions
|
|
@ -601,6 +601,21 @@ class HeavySelect2TagWidget(HeavySelect2MultipleWidget):
|
|||
self.options['tokenSeparators'] = [",", " "]
|
||||
self.options['createSearchChoice'] = '*START*django_select2.createSearchChoice*END*'
|
||||
|
||||
def render_inner_js_code(self, id_, *args):
|
||||
fieldset_id = re.sub(r'-\d+-', '_', id_).replace('-', '_')
|
||||
if '__prefix__' in id_:
|
||||
return ''
|
||||
else:
|
||||
js = u'''
|
||||
window.django_select2.%s = function (selector, fieldID) {
|
||||
var hashedSelector = "#" + selector;
|
||||
$(hashedSelector).data("field_id", fieldID);
|
||||
''' % (fieldset_id)
|
||||
js += super(HeavySelect2TagWidget, self).render_inner_js_code(id_, *args)
|
||||
js += '};'
|
||||
js += 'django_select2.%s("%s", "%s");' % (fieldset_id, id_, id_)
|
||||
return js
|
||||
|
||||
|
||||
### Auto Heavy widgets ###
|
||||
|
||||
|
|
|
|||
|
|
@ -9,9 +9,17 @@
|
|||
<a href="{% url create_new_href %}">Create New</a>
|
||||
<br/>
|
||||
{% endif %}
|
||||
<p>Auto Tags</p>
|
||||
<ul>
|
||||
{% for e in object_list %}
|
||||
<li><a href="{% url href e.id %}">{{ e }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<p>Non-Auto Tags</p>
|
||||
<ul>
|
||||
{% for e in object_list %}
|
||||
<li><a href="{% url href_non_auto e.id %}">{{ e }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -39,6 +39,10 @@ class TagField(AutoModelSelect2TagField):
|
|||
def get_model_field_values(self, value):
|
||||
return {'tag': value}
|
||||
|
||||
class TagNAField(HeavyModelSelect2TagField):
|
||||
def get_model_field_values(self, value):
|
||||
return {'tag': value}
|
||||
|
||||
class SelfChoices(AutoSelect2Field):
|
||||
def get_val_txt(self, value):
|
||||
if not hasattr(self, 'res_map'):
|
||||
|
|
@ -166,6 +170,16 @@ class QuestionForm(forms.ModelForm):
|
|||
class Meta:
|
||||
model = Question
|
||||
|
||||
class QuestionNonAutoForm(forms.ModelForm):
|
||||
question = forms.CharField()
|
||||
description = forms.CharField(widget=forms.Textarea)
|
||||
tags = TagNAField(queryset=Tag.objects,
|
||||
search_fields=['tag__icontains'],
|
||||
widget = HeavySelect2TagWidget(data_view='test_tagging_tags'))
|
||||
|
||||
class Meta:
|
||||
model = Question
|
||||
|
||||
class WordsForm(forms.ModelForm):
|
||||
word = WordChoices()
|
||||
words = MultiWordChoices()
|
||||
|
|
|
|||
|
|
@ -13,7 +13,9 @@ urlpatterns = patterns('testapp.testmain.views',
|
|||
|
||||
url(r'question/$', 'test_list_questions', name='test_list_questions'),
|
||||
url(r'question/form/([0-9]+)/$', 'test_tagging', name='test_tagging'),
|
||||
url(r'question/form/([0-9]+)/na/$', 'test_tagging_non_auto', name='test_tagging_non_auto'),
|
||||
url(r'question/form/$', 'test_tagging_new', name='test_tagging_new'),
|
||||
url(r'question/tags/$', 'test_tagging_tags', name='test_tagging_tags'),
|
||||
|
||||
url(r'auto_model/form/$', 'test_auto_multivalue_field', name='test_auto_multivalue_field'),
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
import json
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
|
||||
from .forms import EmployeeForm, DeptForm, MixedForm, InitialValueForm, QuestionForm, WordsForm, SchoolForm, GetSearchTestForm, \
|
||||
AnotherWordForm
|
||||
from .models import Employee, Dept, Question, WordList, School
|
||||
from .forms import EmployeeForm, DeptForm, MixedForm, InitialValueForm, QuestionForm, QuestionNonAutoForm, WordsForm, SchoolForm, \
|
||||
GetSearchTestForm, AnotherWordForm
|
||||
from .models import Employee, Dept, Question, WordList, School, Tag
|
||||
|
||||
def test_single_value_model_field(request):
|
||||
return render(request, 'list.html', {
|
||||
|
|
@ -60,6 +61,7 @@ def test_list_questions(request):
|
|||
return render(request, 'list.html', {
|
||||
'title': 'Questions',
|
||||
'href': 'test_tagging',
|
||||
'href_non_auto': 'test_tagging_non_auto',
|
||||
'object_list': Question.objects.all(),
|
||||
'create_new_href': 'test_tagging_new'
|
||||
})
|
||||
|
|
@ -81,6 +83,25 @@ def test_tagging(request, id):
|
|||
form = QuestionForm(instance=question)
|
||||
return render(request, 'form.html', {'form': form})
|
||||
|
||||
def test_tagging_non_auto(request, id):
|
||||
if id is None:
|
||||
question = Question()
|
||||
else:
|
||||
question = get_object_or_404(Question, pk=id)
|
||||
if request.POST:
|
||||
form = QuestionNonAutoForm(data=request.POST, instance=question)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return HttpResponseRedirect(reverse('home'))
|
||||
else:
|
||||
form = QuestionNonAutoForm(instance=question)
|
||||
return render(request, 'form.html', {'form': form})
|
||||
|
||||
def test_tagging_tags(request):
|
||||
tags = Tag.objects.all()
|
||||
results = [{'id': t.id, 'text': t.tag} for t in tags]
|
||||
return HttpResponse(json.dumps({'err': 'nil', 'results': results}), content_type='application/json')
|
||||
|
||||
def test_auto_multivalue_field(request):
|
||||
try:
|
||||
s = School.objects.get(id=1)
|
||||
|
|
|
|||
Loading…
Reference in a new issue