generic views for add_object, edit_object; TheatreGroup form basics done.

This commit is contained in:
Sanj 2012-07-27 04:46:44 +05:30
parent 9c9ee607aa
commit 8137abad92
9 changed files with 108 additions and 22 deletions

View File

@ -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

View File

@ -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.

View File

@ -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):

View File

@ -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',)

View File

@ -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)

View File

@ -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.

View File

@ -1,2 +1,5 @@
<input type="hidden" id="id_{{name}}" name="{{name}}" data-ctype="{{ ctype.id }}" class="select2class" data-id="{% if value %}{% for v in value %}{{v}}{% endfor %}{% endif %}" data-title="{% if title %}{{ title }}{% endif %}" style="width:600px;" value="" /> <a href="/popup_form/{{ ctype.id }}" class="popup_add_btn" title="Add new" id="add_{{name}}" onclick="return showAddAnotherPopup(this);">+</a>
<input type="hidden" id="id_{{name}}" name="{{name}}" data-ctype="{{ ctype.id }}" class="select2class" data-id="{% if value %}{% for v in value %}{{v}}{% endfor %}{% endif %}" data-title="{% if title %}{{ title }}{% endif %}" style="width:600px;" value="" />
{% if has_hadd %}
<a href="/popup_form/{{ ctype.id }}" class="popup_add_btn" title="Add new" id="add_{{name}}" onclick="return showAddAnotherPopup(this);">+</a>
{% endif %}

View File

@ -19,7 +19,7 @@
}
.delete-row {
display:none;
display:none !important;
}
</style>
<!-- uni-form JS library, optional -->

View File

@ -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;