add basic migration script participants -> person

This commit is contained in:
Sanj 2012-07-04 13:22:00 +05:30
parent 48ecce9c31
commit f250789e82
7 changed files with 128 additions and 28 deletions

View File

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

View File

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

View File

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

View File

17
itf/migrations/migrate.py Normal file
View File

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

View File

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

View File

@ -20,6 +20,8 @@
<!-- uni-form JS library, optional -->
<script src="/static/uni_form/uni-form.jquery.js" type="text/javascript"></script>
<script src="/static/js/select2.js" type="text/javascript"></script>
<script src="/static/js/jquery.formset.min.js"></script>
<script type="text/javascript">
$(function(){
@ -38,9 +40,9 @@ $(function(){
}
];
$('#id_connections').select2({
$('.select2class').select2({
//placeholder: 'Select your connections',
multiple: true,
//multiple: true,
ajax: {
url: '/autocompletes/itfprofiles/',
dataType: 'json',
@ -66,8 +68,8 @@ $(function(){
},
formatSelection: function(item) {
//console.log("foo");
return "<div class='itfselect2_selected' data-id='" + item.id + "'>" + item.first_name + " " + item.last_name + "</div>";
return item.first_name + " " + item.last_name;
//return "<div data-id='" + item.id + "'>" + item.first_name + " " + item.last_name + "</div>";
},
createSearchChoice: function(term) {
s = term.split(" ");
@ -82,11 +84,11 @@ $(function(){
}
});
// console.log(sampleData);
$('#id_connections').select2("val", sampleData);
var val = $('#id_connections').select2("val");
$('#id_connections').data("old_val", sampleData);
// $('#id_connections').select2("val", sampleData);
// var val = $('#id_connections').select2("val");
// $('#id_connections').data("old_val", sampleData);
/*
$('.select2-choices').on("click", function(e) {
console.log(e.target);
});
@ -132,7 +134,7 @@ $(function(){
});
//$('#id_connections').change();
*/
});
@ -151,9 +153,28 @@ $(function(){
</div>
{{ form|crispy }}
{% for inline in inlines %}
<h3> {{inline.title }} </h3>
{% if inline.help_text %}
<div class="formset_help_text"> {{ inline.help_text }}</div>
{% endif %}
<div class="itf_formset" id="formset_{{inline.prefix}}">
{% crispy inline inline.form.helper %}
</div>
{% endfor %}
</form>
</div>
</div>
<script type="text/javascript">
$(function() {
{% for inline in inlines %}
$('#formset_{{inline.prefix}}').formset({
prefix: '{{ inline.prefix }}',
formCssClass: 'dynamic-formset{{forloop.counter0}}'
});
{% endfor %}
});
</script>
{% endblock %}