From f250789e821135709fae101a0dabb2c41ab733b7 Mon Sep 17 00:00:00 2001 From: Sanj Date: Wed, 4 Jul 2012 13:22:00 +0530 Subject: [PATCH] add basic migration script participants -> person --- itf/itfprofiles/forms.py | 85 ++++++++++++++++++++++++----- itf/itfprofiles/models.py | 4 +- itf/itfprofiles/views.py | 5 +- itf/migrations/__init__.py | 0 itf/migrations/migrate.py | 17 ++++++ itf/scriptbank/models.py | 4 +- itf/templates/test/person_form.html | 41 ++++++++++---- 7 files changed, 128 insertions(+), 28 deletions(-) create mode 100644 itf/migrations/__init__.py create mode 100644 itf/migrations/migrate.py diff --git a/itf/itfprofiles/forms.py b/itf/itfprofiles/forms.py index 86650c6..7c729ff 100644 --- a/itf/itfprofiles/forms.py +++ b/itf/itfprofiles/forms.py @@ -3,16 +3,16 @@ 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 -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 - +class ItfForm(forms.ModelForm): + + def __init__(self, *args, **kwargs): + self.helper = FormHelper() + super(ItfForm, self).__init__(*args, **kwargs) class ConnectionsWidget(forms.SelectMultiple): @@ -27,22 +27,79 @@ class ConnectionsWidget(forms.SelectMultiple): ''' def get_context(self, name, value, attrs, *args, **kwargs): - import pdb - pdb.set_trace() ctx = super(ConnectionsWidget, self).get_context(name, value, attrs) - ctx['data_json'] = "hi" # json.dumps(choices) + + #selected_values = [{'id': obj[0], 'text': obj[1]} for obj in self.choices if obj[0] in value] + #ctx['data_json'] = json.dumps(selected_values) # json.dumps(choices) return ctx +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 + + +class ConnectionsForm(ItfForm): + person2 = forms.ModelMultipleChoiceField(Person.objects.all(), widget=ConnectionsWidget()) + + class Meta: + model = PersonPerson + exclude = ('disapproved',) + +class PersonProductionForm(ItfForm): + pass + + class Meta: + model = PersonProduction + +class PersonGroupForm(ItfForm): + pass + + 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.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 class PersonForm(forms.ModelForm): # 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()) email = forms.EmailField(widget=forms.EmailInput(attrs={'placeholder': 'john@example.com'})) +# inlines = [ConnectionsInline] + inlines = [ConnectionsInline, ProductionsInline, GroupsInline, PadmaClipsInline] -# inlines = [ConnectionFormset, ProductionFormset] class Meta: + exclude = ('connections', 'productions', 'groups', 'last_accessed',) model = Person widgets = { 'first_name': forms.TextInput, diff --git a/itf/itfprofiles/models.py b/itf/itfprofiles/models.py index c6754be..21b07a4 100644 --- a/itf/itfprofiles/models.py +++ b/itf/itfprofiles/models.py @@ -83,10 +83,11 @@ class PersonOccupation(models.Model): class PersonPerson(models.Model): person1 = models.ForeignKey(Person, related_name='PersonFrom', db_index=True) - person2 = models.ForeignKey(Person, related_name='PersonTo', db_index=True) + person2 = models.ForeignKey(Person, related_name='PersonTo', db_index=True, verbose_name='connection') relation = models.ForeignKey("Relation") disapproved = models.BooleanField(default=False) + class Relation(models.Model): name = models.CharField(max_length=255, db_index=True) reverse = models.CharField(max_length=255, db_index=True) @@ -122,6 +123,7 @@ class Play(ItfModel): class Production(ItfModel): +# user = models.ForeignKey(User) play = models.ForeignKey("Play", null=True, blank=True) name = models.CharField(max_length=255, db_index=True) language = models.ForeignKey("Language", blank=True, null=True) diff --git a/itf/itfprofiles/views.py b/itf/itfprofiles/views.py index eccabaf..d3110af 100644 --- a/itf/itfprofiles/views.py +++ b/itf/itfprofiles/views.py @@ -8,7 +8,10 @@ from django.core.paginator import Paginator, InvalidPage, EmptyPage def person_form(request): person = Person.objects.all()[0] form = PersonForm(instance=person) - return render_to_response("test/person_form.html", {'form': form}) + inlines = [inline(instance=person) for inline in form.inlines] + #import pdb + #pdb.set_trace() + return render_to_response("test/person_form.html", {'form': form, 'inlines': inlines}) def autocomplete(request): diff --git a/itf/migrations/__init__.py b/itf/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/itf/migrations/migrate.py b/itf/migrations/migrate.py new file mode 100644 index 0000000..f3b34f0 --- /dev/null +++ b/itf/migrations/migrate.py @@ -0,0 +1,17 @@ +from festival.models import Participant, Meeting +from itfprofiles.models import Person + +def migrate_participants(): + for p in Participant.objects.all(): + name = p.name + names = name.split(" ") + person = Person() + if len(names) > 1: + person.last_name = names[-1] + person.first_name = " ".join(names[0:-1]) + else: + person.first_name = names[0] + person.about = p.short_bio + person.save() + print "saved %s %s" % (person.first_name, person.last_name,) + diff --git a/itf/scriptbank/models.py b/itf/scriptbank/models.py index 13a7c6f..d63aabd 100755 --- a/itf/scriptbank/models.py +++ b/itf/scriptbank/models.py @@ -40,7 +40,7 @@ class Script(ItfModel): fts_fields = ['title', 'synopsis', 'author'] fk_fields = ['license_adapt', 'license_perform'] - add_form = 'ScriptForm' +# add_form = 'ScriptForm' #Meta added = models.DateTimeField(auto_now_add=True) @@ -55,7 +55,7 @@ class Script(ItfModel): 'title': self.title, } - def info_dict(self): + def get_dict(self): return { 'title': self.title, 'synopsis': self.synopsis, diff --git a/itf/templates/test/person_form.html b/itf/templates/test/person_form.html index 48cc635..ff58b71 100644 --- a/itf/templates/test/person_form.html +++ b/itf/templates/test/person_form.html @@ -20,6 +20,8 @@ + + + {% endblock %}