From 8137abad92cc1f68424ebf80ec256c4ad0d4ef6b Mon Sep 17 00:00:00 2001 From: Sanj Date: Fri, 27 Jul 2012 04:46:44 +0530 Subject: [PATCH] generic views for add_object, edit_object; TheatreGroup form basics done. --- itf/app/forms.py | 6 ++++ itf/app/models.py | 4 +++ itf/insidepages/views.py | 44 ++++++++++++++++++++++---- itf/itfprofiles/forms.py | 30 +++++++++++++++++- itf/itfprofiles/models.py | 19 ++++++++--- itf/static/js/jquery.formset.js | 2 +- itf/templates/formwidgets/select2.html | 5 ++- itf/templates/test/person_form.html | 2 +- migrations/sqldiff250712.sql | 18 +++++++---- 9 files changed, 108 insertions(+), 22 deletions(-) diff --git a/itf/app/forms.py b/itf/app/forms.py index 924a1e8..ecbe486 100644 --- a/itf/app/forms.py +++ b/itf/app/forms.py @@ -29,6 +29,11 @@ class AutocompleteAddWidget(forms.Select): def __init__(self, *args, **kwargs): model_class = kwargs.pop('model_class') self.ctype = ContentType.objects.get_for_model(model_class) + if kwargs.has_key('has_add_btn'): + self.has_add = kwargs.pop('has_add_btn') + else: + self.has_add = True + #self.ctype = ctype super(AutocompleteAddWidget, self).__init__(*args, **kwargs) @@ -48,5 +53,6 @@ class AutocompleteAddWidget(forms.Select): #pdb.set_trace() ctx['title'] = get_matched_choice_title(self.choices, value) ctx['ctype'] = self.ctype + ctx['has_add'] = self.has_add return ctx diff --git a/itf/app/models.py b/itf/app/models.py index 4d267ad..83dd724 100755 --- a/itf/app/models.py +++ b/itf/app/models.py @@ -71,6 +71,10 @@ class ItfModel(models.Model): def autocomplete_dict(self): d = self.list_dict() d['text'] = self.get_text() + if not d['text']: + return '' + if len(d['text']) > 150: + d['text'] = d['text'][:150] + "..." return d #This should return all the context for the object that will get passed to the sub-template for this object. diff --git a/itf/insidepages/views.py b/itf/insidepages/views.py index d4d84a0..3480687 100755 --- a/itf/insidepages/views.py +++ b/itf/insidepages/views.py @@ -13,15 +13,32 @@ def add_object(request, module_slug, tab_slug): form = model_class.get_forms()[add_form_name] if request.POST: f = form(request.POST, request.FILES) + if f.is_valid(): - f.save() - return HttpResponseRedirect('/') #TODO: get the saved object and redirect to .get_absolute_url() + instance = f.save(commit=False) + if instance.__dict__.has_key('added_by_id'): + instance.added_by = request.user + instance.save() + inlines = [inline(request.POST, request.FILES, instance=instance) for inline in f.inlines] + all_valid = True + for inline in inlines: + if inline.is_valid(): + inline.save() + else: + all_valid = False + if all_valid: + return HttpResponseRedirect(instance.get_absolute_url()) #TODO: get the saved object and redirect to .get_absolute_url() + else: + inlines = [inline() for inline in f.inlines] else: f = form() + inlines = [inline() for inline in f.inlines] + context = RequestContext(request, { 'form': f, + 'inlines': inlines }) - return render_to_response("add_form.html", context) + return render_to_response("test/person_form.html", context) def edit_object(request, module_slug, tab_slug, object_id): @@ -33,16 +50,29 @@ def edit_object(request, module_slug, tab_slug, object_id): if request.POST: f = form(request.POST, request.FILES, instance=obj) + inlines = [inline(request.POST, request.FILES, instance=person) for inline in f.inlines] if f.is_valid(): - f.save() - return HttpResponseRedirect(obj.get_absolute_url()) + instance = f.save() + all_valid = True + for inline in inlines: + if inline.is_valid(): + inline.save() + else: + all_valid = False + if all_valid: + return HttpResponseRedirect(instance.get_absolute_url()) else: f = form(instance=obj) + inlines = [inline(instance=obj) for inline in f.inlines] + else: + f = form(instance=obj) + inlines = [inline(instance=obj) for inline in f.inlines] context = RequestContext(request, { 'form': f, - 'object': obj + 'object': obj, + 'inlines': inlines }) - return render_to_response("edit_form.html", context) + return render_to_response("test/person_form.html", context) def render_object(request, module_slug): diff --git a/itf/itfprofiles/forms.py b/itf/itfprofiles/forms.py index 796ce8a..5170e57 100644 --- a/itf/itfprofiles/forms.py +++ b/itf/itfprofiles/forms.py @@ -85,6 +85,7 @@ def itf_inlineformset_factory(model, *args, **kwargs): title = kwargs.pop('title', '') help_text = kwargs.pop('help_text', '') is_generic = kwargs.pop('is_generic', False) + kwargs['extra'] = 1 #kwargs.delete("title") if is_generic: FormSet = generic_inlineformset_factory(model, *args, **kwargs) @@ -101,7 +102,7 @@ PadmaClipsInline = itf_inlineformset_factory(PadmaClip, extra=1, is_generic=True #Actual person form definition -class PersonForm(forms.ModelForm): +class PersonForm(ItfForm): # occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple()) # connections = forms.ModelMultipleChoiceField(Person.objects.all(), widget=ConnectionsWidget()) # occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple()) @@ -126,4 +127,31 @@ class PersonForm(forms.ModelForm): } #exclude = ('connections', 'productions', 'occupations', 'groups',) +class GroupPersonForm(ItfForm): + person = forms.ModelChoiceField(Person.objects.all(), widget=AutocompleteAddWidget(model_class=Person)) + class Meta: + model = PersonGroup + +class GroupBuzzItemForm(ItfForm): + + class Meta: + model = GroupBuzzItem + +class GroupLocationForm(ItfForm): + + class Meta: + model = Location + exclude = ('lat', 'lon',) + +PersonsInline = itf_inlineformset_factory(TheatreGroup, PersonGroup, form=GroupPersonForm, title="People in the Group") +GroupBuzzItemsInline = itf_inlineformset_factory(TheatreGroup, GroupBuzzItem, form=GroupBuzzItemForm, title="Buzz Items") +LocationsInline = itf_inlineformset_factory(Location, form=GroupLocationForm, title="Locations", is_generic=True) + +class TheatreGroupForm(ItfForm): + + inlines = [PersonsInline, GroupBuzzItemsInline, LocationsInline] + + class Meta: + model = TheatreGroup + exclude = ('added_by', 'locations',) diff --git a/itf/itfprofiles/models.py b/itf/itfprofiles/models.py index 38f20eb..e3c737f 100644 --- a/itf/itfprofiles/models.py +++ b/itf/itfprofiles/models.py @@ -4,6 +4,8 @@ from django.contrib.auth.models import User from datetime import datetime from django.contrib.localflavor.in_.forms import INZipCodeField #from ox.django.fields import DictField +from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes import generic GENDER_CHOICES = ( ('M', 'Male'), @@ -35,8 +37,8 @@ class Person(ItfModel): #Personal info gender = models.CharField(max_length=255, choices=GENDER_CHOICES, blank=True) image = models.ImageField(upload_to='images/', blank=True, null=True) - locations = models.ManyToManyField("Location", blank=True, null=True) - +# locations = models.ManyToManyField("Location", blank=True, null=True) + locations = generic.GenericRelation("Location") #Groups and Connections groups = models.ManyToManyField("TheatreGroup", blank=True, null=True, through='PersonGroup') connections = models.ManyToManyField('Person', blank=True, null=True, through='PersonPerson') @@ -153,7 +155,9 @@ class Location(models.Model): city = models.ForeignKey(City) pincode= INZipCodeField() address= models.TextField() - + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + content_object = generic.GenericForeignKey('content_type', 'object_id') class PersonOccupation(models.Model): person = models.ForeignKey(Person, db_index=True) @@ -237,7 +241,8 @@ class PersonProduction(models.Model): class TheatreGroup(ItfModel): fts_fields = ['name', 'about'] - form_names = ['PopupGroupForm'] + form_names = ['TheatreGroupForm', 'PopupGroupForm'] + main_form = 'TheatreGroupForm' added_by = models.ForeignKey(User) name = models.CharField(max_length=255, db_index=True) # name + location is unique email = models.EmailField(blank=True, null=True) @@ -260,6 +265,12 @@ class TheatreGroup(ItfModel): def __unicode__(self): return self.name + def get_dict(self): + return { + 'name': self.name, + 'about': self.about, + } + class PersonGroup(models.Model): person = models.ForeignKey(Person, db_index=True) group = models.ForeignKey(TheatreGroup, db_index=True) diff --git a/itf/static/js/jquery.formset.js b/itf/static/js/jquery.formset.js index aba8062..eacee96 100644 --- a/itf/static/js/jquery.formset.js +++ b/itf/static/js/jquery.formset.js @@ -75,7 +75,7 @@ $addBtn.click(function() { var nextIndex = parseInt($('#id_' + options.prefix + '-TOTAL_FORMS').val()); var row = $('.' + options.formCssClass + ' fieldset:first').clone().get(0); - $(row).removeAttr('id').insertAfter($('.' + options.formCssClass + ':last')); + $(row).removeAttr('id').insertAfter($('.' + options.formCssClass + ' fieldset:last')); $(row).find('input,select,textarea,label').each(function() { updateElementIndex(this, options.prefix, nextIndex); // If this is a checkbox or radiobutton, set uncheck it. diff --git a/itf/templates/formwidgets/select2.html b/itf/templates/formwidgets/select2.html index 4e85236..e310fc0 100644 --- a/itf/templates/formwidgets/select2.html +++ b/itf/templates/formwidgets/select2.html @@ -1,2 +1,5 @@ - + + +{% if has_hadd %} + + +{% endif %} diff --git a/itf/templates/test/person_form.html b/itf/templates/test/person_form.html index d3414ec..8e3a234 100644 --- a/itf/templates/test/person_form.html +++ b/itf/templates/test/person_form.html @@ -19,7 +19,7 @@ } .delete-row { - display:none; + display:none !important; } diff --git a/migrations/sqldiff250712.sql b/migrations/sqldiff250712.sql index 7db8bcf..ffd87cf 100644 --- a/migrations/sqldiff250712.sql +++ b/migrations/sqldiff250712.sql @@ -1,13 +1,7 @@ drop table itfprofiles_production; drop table itfprofiles_theatregroup; drop table itfprofiles_play; -CREATE TABLE `itfprofiles_person_locations` ( - `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, - `person_id` integer NOT NULL, - `location_id` integer NOT NULL, - UNIQUE (`person_id`, `location_id`) -) -; + CREATE TABLE `itfprofiles_person_languages` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `person_id` integer NOT NULL, @@ -16,3 +10,13 @@ CREATE TABLE `itfprofiles_person_languages` ( ) ; +BEGIN; +-- Application: itfprofiles +-- Model: Location +ALTER TABLE `itfprofiles_location` + ADD `content_type_id` integer; +ALTER TABLE `itfprofiles_location` + ADD `object_id` integer UNSIGNED; +CREATE INDEX `itfprofiles_location_content_type_id_idx` + ON `itfprofiles_location` (`content_type_id`); +COMMIT;