From 1bf13ef6efae5296122d4665e66e0764104ad1c8 Mon Sep 17 00:00:00 2001 From: Sanj Date: Thu, 19 Jul 2012 03:36:31 +0530 Subject: [PATCH] get initial values on autocompletes to work as well as getting title on popup close --- itf/app/forms.py | 9 +++++++++ itf/app/models.py | 6 ++++-- itf/app/views.py | 3 ++- itf/itfprofiles/forms.py | 2 +- itf/itfprofiles/models.py | 2 +- itf/static/js/RelatedObjectLookups.js | 2 +- itf/templates/formwidgets/select2.html | 2 +- itf/templates/test/person_form.html | 11 +++++++++-- itf/templates/test/popup.html | 2 +- 9 files changed, 29 insertions(+), 10 deletions(-) diff --git a/itf/app/forms.py b/itf/app/forms.py index cfd37bd..7e5a16e 100644 --- a/itf/app/forms.py +++ b/itf/app/forms.py @@ -33,7 +33,16 @@ class AutocompleteAddWidget(forms.Select): super(AutocompleteAddWidget, self).__init__(*args, **kwargs) def get_context(self, name, value, attrs, *args, **kwargs): + + def get_matched_choice_title(choices, id): + for choice in choices: + if choice[0] == id: + return choice[1] + return '' + ctx = super(AutocompleteAddWidget, self).get_context(name, value, attrs) + if value: + ctx['title'] = get_matched_choice_title(self.choices, value) ctx['ctype'] = self.ctype return ctx diff --git a/itf/app/models.py b/itf/app/models.py index 3558cdd..4d267ad 100755 --- a/itf/app/models.py +++ b/itf/app/models.py @@ -83,8 +83,10 @@ class ItfModel(models.Model): #Returns the title for this object, used as title of page, title of object, and by default, title in left hand display. By default, is a field called 'title', but subclasses can over-ride either title field or the get_title method and return anything. def get_title(self): - return self.get(self.title_field) - + try: + return self.get(self.title_field) + except: + return self.__unicode__() #Returns "text" for this object - like a "description" or "about" field, or nothing - ideally would be over-ridden by subclasses. def get_text(self): diff --git a/itf/app/views.py b/itf/app/views.py index e54b3f6..2c77e83 100755 --- a/itf/app/views.py +++ b/itf/app/views.py @@ -6,6 +6,7 @@ from ox.django.shortcuts import json_response, render_to_json_response, get_obje #from api.views import html_snapshot from django.contrib.contenttypes.models import ContentType +from django.http import HttpResponse ''' def index(request): @@ -44,7 +45,7 @@ def popup_form(request, ctype_id): form = form_class(request.POST, request.FILES) if form.is_valid(): instance = form.save() - text = instance.autocomplete_dict()['text'] + text = instance.get_title() return HttpResponse("" % (instance.id, text,)) else: form = form_class() diff --git a/itf/itfprofiles/forms.py b/itf/itfprofiles/forms.py index 266ee13..2901a3e 100644 --- a/itf/itfprofiles/forms.py +++ b/itf/itfprofiles/forms.py @@ -15,7 +15,7 @@ class PopupPersonForm(PopupForm): class Meta: model = Person - + fields = ('first_name', 'last_name', 'email', 'dob',) class PopupGroupForm(PopupForm): diff --git a/itf/itfprofiles/models.py b/itf/itfprofiles/models.py index aa65222..112131a 100644 --- a/itf/itfprofiles/models.py +++ b/itf/itfprofiles/models.py @@ -20,7 +20,7 @@ class Person(ItfModel): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) email = models.EmailField(blank=True, null=True, unique=True, db_index=True) -# email_validated = models.BooleanField(default=False, editable=False) + email_validated = models.BooleanField(default=False, editable=False) tel_no = models.CharField(max_length=100, blank=True) about = models.TextField(blank=True, null=True) dob = models.DateField(null=True, verbose_name="Date of Birth") diff --git a/itf/static/js/RelatedObjectLookups.js b/itf/static/js/RelatedObjectLookups.js index 2e34be9..c7d96af 100644 --- a/itf/static/js/RelatedObjectLookups.js +++ b/itf/static/js/RelatedObjectLookups.js @@ -77,7 +77,7 @@ function dismissAddAnotherPopup(win, newId, newRepr) { //console.log(elem); //alert(newRepr); //alert(newId); - elem.select2("val", {'id': newId, 'text': newRepr}); + elem.select2("val", {'id': newId, 'title': newRepr}); //var elem = document.getElementById(name); //console.log(elem); diff --git a/itf/templates/formwidgets/select2.html b/itf/templates/formwidgets/select2.html index b9e6386..b673845 100644 --- a/itf/templates/formwidgets/select2.html +++ b/itf/templates/formwidgets/select2.html @@ -1,2 +1,2 @@ - Add + Add diff --git a/itf/templates/test/person_form.html b/itf/templates/test/person_form.html index 4bb2518..87a4a86 100644 --- a/itf/templates/test/person_form.html +++ b/itf/templates/test/person_form.html @@ -43,9 +43,10 @@ $(function(){ ]; */ $('.select2class').each(function() { - var ctype_id = $(this).attr("data-ctype"); + var $this = $(this); + var ctype_id = $this.attr("data-ctype"); var url = '/autocomplete/' + ctype_id; - $(this).select2({ + $this.select2({ ajax: { 'url': url, dataType: 'json', @@ -75,6 +76,12 @@ $(function(){ //return "
" + item.first_name + " " + item.last_name + "
"; } }); + var id = $this.attr("data-id"); + if ($.trim(id) !== '') { + var title = $this.attr("data-title"); + $this.select2("val", {'id': id, 'title': title}); + } + }); /* diff --git a/itf/templates/test/popup.html b/itf/templates/test/popup.html index 6c27286..9226b00 100644 --- a/itf/templates/test/popup.html +++ b/itf/templates/test/popup.html @@ -4,7 +4,7 @@ -
+ {% csrf_token %} {{ form.as_p }}