From 1dd9ea2b6f0308ecb3e19797524629b84c8ae8eb Mon Sep 17 00:00:00 2001 From: Sanj Date: Fri, 27 Jul 2012 23:22:00 +0530 Subject: [PATCH] Generic models w/ inlines for Awards, Locations, BuzzItems --- itf/app/forms.py | 17 +++++ itf/itfprofiles/forms.py | 133 ++++++++++++++++++++------------------ itf/itfprofiles/models.py | 30 ++++----- 3 files changed, 102 insertions(+), 78 deletions(-) diff --git a/itf/app/forms.py b/itf/app/forms.py index 3addc4a..db9c620 100644 --- a/itf/app/forms.py +++ b/itf/app/forms.py @@ -1,6 +1,23 @@ import floppyforms as forms from django.contrib.contenttypes.models import ContentType from crispy_forms.helper import FormHelper +from django.forms.models import inlineformset_factory +from django.contrib.contenttypes.generic import generic_inlineformset_factory + +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) + else: + FormSet = inlineformset_factory(model, *args, **kwargs) + FormSet.title = title + FormSet.help_text = help_text + return FormSet + class ItfForm(forms.ModelForm): inlines = [] diff --git a/itf/itfprofiles/forms.py b/itf/itfprofiles/forms.py index 9139534..a6f1c63 100644 --- a/itf/itfprofiles/forms.py +++ b/itf/itfprofiles/forms.py @@ -3,14 +3,50 @@ from registration.forms import RegistrationForm from models import * from registration.models import RegistrationProfile import json -from django.forms.models import inlineformset_factory -from django.contrib.contenttypes.generic import generic_inlineformset_factory + from padmavideos.models import PadmaVideo, PadmaClip from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit from django.contrib.contenttypes.models import ContentType from app.forms import * +#Forms and Inlines for Generic Classes + +class LocationForm(ItfForm): + + class Meta: + model = Location + exclude = ('lat', 'lon',) + +LocationsInline = itf_inlineformset_factory(Location, form=LocationForm, title="Locations", is_generic=True) + + +class BuzzItemsForm(ItfForm): + + class Meta: + model = BuzzItem + +BuzzItemsInline = itf_inlineformset_factory(BuzzItem, form=BuzzItemsForm, title="Buzz Items", is_generic=True) + + +class AwardsForm(ItfForm): + + class Meta: + model = Award + +AwardsInline = itf_inlineformset_factory(Award, form=AwardsForm, title="Awards", is_generic=True) + + +class PadmaClipForm(ItfForm): + pass + + class Meta: + model = PadmaClip + +PadmaClipsInline = itf_inlineformset_factory(PadmaClip, extra=1, is_generic=True, form=PadmaClipForm, title="Videos") + + +#Popup Form Classes: class PopupPersonForm(PopupForm): class Meta: @@ -30,30 +66,10 @@ class PopupProductionForm(PopupForm): class Meta: model = Production - fields = ('name', 'group',) + fields = ('name', 'group',) -class ItfAllAuthRegForm(forms.Form): - firstname=forms.CharField() - lastname=forms.CharField() - - def save(self, user): - first_name = self.cleaned_data['firstname'] - last_name = self.cleaned_data['lastname'] - p = Person(user=user, first_name=first_name, last_name=last_name, email=user.email) - p.save() - return self - -class ItfRegistrationForm(RegistrationForm): - subscribe = forms.BooleanField(help_text='Subscribe to newsletter?') - - def save(self, profile_callback=None): - new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'], password=self.cleaned_data['password1'], email=self.cleaned_data['email']) - new_profile = ItfProfile(user=new_user, subscribed=self.cleaned_data['subscribe']) - new_profile.save() - return new_user - - +#Inline form definitions and inlines for Person form class ConnectionsForm(ItfForm): #ctype = ContentType.objects.get_for_model(Person) person2 = forms.ModelChoiceField(Person.objects.all(), widget=AutocompleteAddWidget(model_class=Person)) @@ -74,31 +90,9 @@ class PersonGroupForm(ItfForm): class Meta: model = PersonGroup -class PadmaClipForm(ItfForm): - pass - - class Meta: - model = PadmaClip - - -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) - else: - FormSet = inlineformset_factory(model, *args, **kwargs) - FormSet.title = title - FormSet.help_text = help_text - return FormSet - ConnectionsInline = itf_inlineformset_factory(Person, PersonPerson, fk_name='person1', form=ConnectionsForm, extra=1, title="Add / Edit Connections", help_text="select the people you are connected with and how you are related in the theatre world") ProductionsInline = itf_inlineformset_factory(Person, PersonProduction, form=PersonProductionForm, extra=1, title="Productions you have worked in") GroupsInline = itf_inlineformset_factory(Person, PersonGroup, extra=1, form=PersonGroupForm, title="Theatre groups you are a part of or have worked with") -PadmaClipsInline = itf_inlineformset_factory(PadmaClip, extra=1, is_generic=True, form=PadmaClipForm, title="Videos you appear in") #Actual person form definition @@ -108,7 +102,7 @@ class PersonForm(ItfForm): # occupations = forms.ModelMultipleChoiceField(Occupation.objects.all(), widget=forms.CheckboxSelectMultiple()) email = forms.EmailField(widget=forms.EmailInput(attrs={'placeholder': 'john@example.com'})) # inlines = [ConnectionsInline] - inlines = [ConnectionsInline, ProductionsInline, GroupsInline, PadmaClipsInline] + inlines = [ConnectionsInline, ProductionsInline, GroupsInline, BuzzItemsInline, AwardsInline, PadmaClipsInline] def __init__(self, *args, **kwargs): self.helper = FormHelper() @@ -126,31 +120,20 @@ class PersonForm(ItfForm): # 'occupations': forms.CheckboxSelectMultiple } #exclude = ('connections', 'productions', 'occupations', 'groups',) - + + +#Inline definitions and form for TheatreGroup 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] + inlines = [PersonsInline, BuzzItemsInline, AwardsInline, LocationsInline] class Meta: model = TheatreGroup @@ -158,6 +141,8 @@ class TheatreGroupForm(ItfForm): +#Inline definitions and form for Production + class ProductionForm(ItfForm): inlines = [] director = forms.ModelChoiceField(Person.objects.all(), widget=AutocompleteAddWidget(model_class=Person)) @@ -166,3 +151,27 @@ class ProductionForm(ItfForm): class Meta: model = Production exclude = ('added_by',) + + +#Registration forms / not connected to Itf Profiles +class ItfAllAuthRegForm(forms.Form): + firstname=forms.CharField() + lastname=forms.CharField() + + def save(self, user): + first_name = self.cleaned_data['firstname'] + last_name = self.cleaned_data['lastname'] + p = Person(user=user, first_name=first_name, last_name=last_name, email=user.email) + p.save() + return self + + +class ItfRegistrationForm(RegistrationForm): + subscribe = forms.BooleanField(help_text='Subscribe to newsletter?') + + def save(self, profile_callback=None): + new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'], password=self.cleaned_data['password1'], email=self.cleaned_data['email']) + new_profile = ItfProfile(user=new_user, subscribed=self.cleaned_data['subscribe']) + new_profile.save() + return new_user + diff --git a/itf/itfprofiles/models.py b/itf/itfprofiles/models.py index c13f0e2..d3af108 100644 --- a/itf/itfprofiles/models.py +++ b/itf/itfprofiles/models.py @@ -47,7 +47,8 @@ class Person(ItfModel): trainings = models.ManyToManyField("Training", blank=True, null=True, related_name="trainee") # awards = models.ManyToManyField("Award", blank=True, null=True) languages = models.ManyToManyField("Language", blank=True, null=True) #s added - + awards = generic.GenericRelation("Award") + buzzitems = generic.GenericRelation("BuzzItem") # photos = models.ManyToManyField("PhotoAlbum", blank=True, null=True) # orphans = models.ManyToManyField("Orphan", blank=True, null=True, through='PersonOrphan') @@ -95,12 +96,14 @@ BUZZ_ITEM_TYPES = ( ('publicity', 'Publicity'), ) -class PersonBuzzItem(ItfModel): - person = models.ForeignKey("Person") +class BuzzItem(ItfModel): link = models.URLField(verify_exists=False) title = models.CharField(max_length=255) blurb = models.TextField(blank=True) typ = models.CharField(choices=BUZZ_ITEM_TYPES, max_length=128, blank=True) + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + content_object = generic.GenericForeignKey('content_type', 'object_id') def __unicode__(self): return self.title @@ -248,16 +251,18 @@ class TheatreGroup(ItfModel): 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) - location = models.ForeignKey(Location, blank=True, null=True, related_name="theatregroup_location") +# location = models.ForeignKey(Location, blank=True, null=True, related_name="theatregroup_location") tel = models.IntegerField(blank=True, null=True) nature_of_work = models.ManyToManyField("GroupOccupation", blank=True, null=True, through="GroupGroupOccupation") languages = models.ManyToManyField("Language", blank=True, null=True) year_founded = models.IntegerField(blank=True, null=True) about = models.TextField(blank=True) - awards = models.ManyToManyField("Award", blank=True, null=True) + awards = generic.GenericRelation("Award") + buzzitems = generic.GenericRelation("BuzzItem") website = models.URLField(blank=True, verify_exists=False) # resources = models.ManyToManyField("Resource", blank=True, null=True) - locations = models.ManyToManyField("Location", blank=True, null=True, related_name="theatregroup_locations") + locations = generic.GenericRelation("Location") +# locations = models.ManyToManyField("Location", blank=True, null=True, related_name="theatregroup_locations") about = models.TextField(blank=True, null=True) nature_of_work = models.CharField(max_length=255) @@ -280,16 +285,6 @@ class PersonGroup(models.Model): role = models.CharField(max_length=255, blank=True) -class GroupBuzzItem(ItfModel): - group = models.ForeignKey("TheatreGroup") - link = models.URLField(verify_exists=False) - title = models.CharField(max_length=255) - blurb = models.TextField(blank=True) - typ = models.CharField(choices=BUZZ_ITEM_TYPES, max_length=128, blank=True) - - def __unicode__(self): - return self.title - @@ -299,6 +294,9 @@ class Award(models.Model): title = models.CharField(max_length=255) year = models.IntegerField(blank=True) link = models.URLField(verify_exists=False, blank=True, null=True) + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + content_object = generic.GenericForeignKey('content_type', 'object_id') def __unicode__(self): return self.title