generic views for add_object, edit_object; TheatreGroup form basics done.
This commit is contained in:
parent
9c9ee607aa
commit
8137abad92
|
@ -29,6 +29,11 @@ class AutocompleteAddWidget(forms.Select):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
model_class = kwargs.pop('model_class')
|
model_class = kwargs.pop('model_class')
|
||||||
self.ctype = ContentType.objects.get_for_model(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
|
#self.ctype = ctype
|
||||||
super(AutocompleteAddWidget, self).__init__(*args, **kwargs)
|
super(AutocompleteAddWidget, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -48,5 +53,6 @@ class AutocompleteAddWidget(forms.Select):
|
||||||
#pdb.set_trace()
|
#pdb.set_trace()
|
||||||
ctx['title'] = get_matched_choice_title(self.choices, value)
|
ctx['title'] = get_matched_choice_title(self.choices, value)
|
||||||
ctx['ctype'] = self.ctype
|
ctx['ctype'] = self.ctype
|
||||||
|
ctx['has_add'] = self.has_add
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
|
|
|
@ -71,6 +71,10 @@ class ItfModel(models.Model):
|
||||||
def autocomplete_dict(self):
|
def autocomplete_dict(self):
|
||||||
d = self.list_dict()
|
d = self.list_dict()
|
||||||
d['text'] = self.get_text()
|
d['text'] = self.get_text()
|
||||||
|
if not d['text']:
|
||||||
|
return ''
|
||||||
|
if len(d['text']) > 150:
|
||||||
|
d['text'] = d['text'][:150] + "..."
|
||||||
return d
|
return d
|
||||||
|
|
||||||
#This should return all the context for the object that will get passed to the sub-template for this object.
|
#This should return all the context for the object that will get passed to the sub-template for this object.
|
||||||
|
|
|
@ -13,15 +13,32 @@ def add_object(request, module_slug, tab_slug):
|
||||||
form = model_class.get_forms()[add_form_name]
|
form = model_class.get_forms()[add_form_name]
|
||||||
if request.POST:
|
if request.POST:
|
||||||
f = form(request.POST, request.FILES)
|
f = form(request.POST, request.FILES)
|
||||||
|
|
||||||
if f.is_valid():
|
if f.is_valid():
|
||||||
f.save()
|
instance = f.save(commit=False)
|
||||||
return HttpResponseRedirect('/') #TODO: get the saved object and redirect to .get_absolute_url()
|
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:
|
else:
|
||||||
f = form()
|
f = form()
|
||||||
|
inlines = [inline() for inline in f.inlines]
|
||||||
|
|
||||||
context = RequestContext(request, {
|
context = RequestContext(request, {
|
||||||
'form': f,
|
'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):
|
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:
|
if request.POST:
|
||||||
f = form(request.POST, request.FILES, instance=obj)
|
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():
|
if f.is_valid():
|
||||||
f.save()
|
instance = f.save()
|
||||||
return HttpResponseRedirect(obj.get_absolute_url())
|
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:
|
else:
|
||||||
f = form(instance=obj)
|
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, {
|
context = RequestContext(request, {
|
||||||
'form': f,
|
'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):
|
def render_object(request, module_slug):
|
||||||
|
|
|
@ -85,6 +85,7 @@ def itf_inlineformset_factory(model, *args, **kwargs):
|
||||||
title = kwargs.pop('title', '')
|
title = kwargs.pop('title', '')
|
||||||
help_text = kwargs.pop('help_text', '')
|
help_text = kwargs.pop('help_text', '')
|
||||||
is_generic = kwargs.pop('is_generic', False)
|
is_generic = kwargs.pop('is_generic', False)
|
||||||
|
kwargs['extra'] = 1
|
||||||
#kwargs.delete("title")
|
#kwargs.delete("title")
|
||||||
if is_generic:
|
if is_generic:
|
||||||
FormSet = generic_inlineformset_factory(model, *args, **kwargs)
|
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
|
#Actual person form definition
|
||||||
class PersonForm(forms.ModelForm):
|
class PersonForm(ItfForm):
|
||||||
# occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple())
|
# occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple())
|
||||||
# connections = forms.ModelMultipleChoiceField(Person.objects.all(), widget=ConnectionsWidget())
|
# connections = forms.ModelMultipleChoiceField(Person.objects.all(), widget=ConnectionsWidget())
|
||||||
# occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple())
|
# occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple())
|
||||||
|
@ -126,4 +127,31 @@ class PersonForm(forms.ModelForm):
|
||||||
}
|
}
|
||||||
#exclude = ('connections', 'productions', 'occupations', 'groups',)
|
#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',)
|
||||||
|
|
|
@ -4,6 +4,8 @@ from django.contrib.auth.models import User
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from django.contrib.localflavor.in_.forms import INZipCodeField
|
from django.contrib.localflavor.in_.forms import INZipCodeField
|
||||||
#from ox.django.fields import DictField
|
#from ox.django.fields import DictField
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
from django.contrib.contenttypes import generic
|
||||||
|
|
||||||
GENDER_CHOICES = (
|
GENDER_CHOICES = (
|
||||||
('M', 'Male'),
|
('M', 'Male'),
|
||||||
|
@ -35,8 +37,8 @@ class Person(ItfModel):
|
||||||
#Personal info
|
#Personal info
|
||||||
gender = models.CharField(max_length=255, choices=GENDER_CHOICES, blank=True)
|
gender = models.CharField(max_length=255, choices=GENDER_CHOICES, blank=True)
|
||||||
image = models.ImageField(upload_to='images/', blank=True, null=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 and Connections
|
||||||
groups = models.ManyToManyField("TheatreGroup", blank=True, null=True, through='PersonGroup')
|
groups = models.ManyToManyField("TheatreGroup", blank=True, null=True, through='PersonGroup')
|
||||||
connections = models.ManyToManyField('Person', blank=True, null=True, through='PersonPerson')
|
connections = models.ManyToManyField('Person', blank=True, null=True, through='PersonPerson')
|
||||||
|
@ -153,7 +155,9 @@ class Location(models.Model):
|
||||||
city = models.ForeignKey(City)
|
city = models.ForeignKey(City)
|
||||||
pincode= INZipCodeField()
|
pincode= INZipCodeField()
|
||||||
address= models.TextField()
|
address= models.TextField()
|
||||||
|
content_type = models.ForeignKey(ContentType)
|
||||||
|
object_id = models.PositiveIntegerField()
|
||||||
|
content_object = generic.GenericForeignKey('content_type', 'object_id')
|
||||||
|
|
||||||
class PersonOccupation(models.Model):
|
class PersonOccupation(models.Model):
|
||||||
person = models.ForeignKey(Person, db_index=True)
|
person = models.ForeignKey(Person, db_index=True)
|
||||||
|
@ -237,7 +241,8 @@ class PersonProduction(models.Model):
|
||||||
|
|
||||||
class TheatreGroup(ItfModel):
|
class TheatreGroup(ItfModel):
|
||||||
fts_fields = ['name', 'about']
|
fts_fields = ['name', 'about']
|
||||||
form_names = ['PopupGroupForm']
|
form_names = ['TheatreGroupForm', 'PopupGroupForm']
|
||||||
|
main_form = 'TheatreGroupForm'
|
||||||
added_by = models.ForeignKey(User)
|
added_by = models.ForeignKey(User)
|
||||||
name = models.CharField(max_length=255, db_index=True) # name + location is unique
|
name = models.CharField(max_length=255, db_index=True) # name + location is unique
|
||||||
email = models.EmailField(blank=True, null=True)
|
email = models.EmailField(blank=True, null=True)
|
||||||
|
@ -260,6 +265,12 @@ class TheatreGroup(ItfModel):
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
def get_dict(self):
|
||||||
|
return {
|
||||||
|
'name': self.name,
|
||||||
|
'about': self.about,
|
||||||
|
}
|
||||||
|
|
||||||
class PersonGroup(models.Model):
|
class PersonGroup(models.Model):
|
||||||
person = models.ForeignKey(Person, db_index=True)
|
person = models.ForeignKey(Person, db_index=True)
|
||||||
group = models.ForeignKey(TheatreGroup, db_index=True)
|
group = models.ForeignKey(TheatreGroup, db_index=True)
|
||||||
|
|
|
@ -75,7 +75,7 @@
|
||||||
$addBtn.click(function() {
|
$addBtn.click(function() {
|
||||||
var nextIndex = parseInt($('#id_' + options.prefix + '-TOTAL_FORMS').val());
|
var nextIndex = parseInt($('#id_' + options.prefix + '-TOTAL_FORMS').val());
|
||||||
var row = $('.' + options.formCssClass + ' fieldset:first').clone().get(0);
|
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() {
|
$(row).find('input,select,textarea,label').each(function() {
|
||||||
updateElementIndex(this, options.prefix, nextIndex);
|
updateElementIndex(this, options.prefix, nextIndex);
|
||||||
// If this is a checkbox or radiobutton, set uncheck it.
|
// If this is a checkbox or radiobutton, set uncheck it.
|
||||||
|
|
|
@ -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 %}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.delete-row {
|
.delete-row {
|
||||||
display:none;
|
display:none !important;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<!-- uni-form JS library, optional -->
|
<!-- uni-form JS library, optional -->
|
||||||
|
|
|
@ -1,13 +1,7 @@
|
||||||
drop table itfprofiles_production;
|
drop table itfprofiles_production;
|
||||||
drop table itfprofiles_theatregroup;
|
drop table itfprofiles_theatregroup;
|
||||||
drop table itfprofiles_play;
|
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` (
|
CREATE TABLE `itfprofiles_person_languages` (
|
||||||
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
|
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
|
||||||
`person_id` integer NOT NULL,
|
`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;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user