basic format of edit working; remove preferred field?

svn: r13843
This commit is contained in:
Doug Blank 2009-12-18 03:57:48 +00:00
parent 383cf27b8b
commit f31b20e7a9
2 changed files with 39 additions and 26 deletions

View File

@ -25,13 +25,13 @@ class NameForm(forms.ModelForm):
"preferred") "preferred")
# Add these because they are TextFields, which render as # Add these because they are TextFields, which render as
# Textareas: # Textareas:
surname = forms.CharField(required=False, widget=TextInput()) surname = forms.CharField(required=False, widget=TextInput(attrs={'size':'30'}))
first_name = forms.CharField(required=False, widget=TextInput()) first_name = forms.CharField(required=False, widget=TextInput(attrs={'size':'30'}))
title = forms.CharField(required=False, widget=TextInput()) title = forms.CharField(required=False, widget=TextInput(attrs={'size':'30'}))
prefix = forms.CharField(required=False, widget=TextInput()) prefix = forms.CharField(required=False, widget=TextInput(attrs={'size':'30'}))
suffix = forms.CharField(required=False, widget=TextInput()) suffix = forms.CharField(required=False, widget=TextInput(attrs={'size':'30'}))
call = forms.CharField(required=False, widget=TextInput()) call = forms.CharField(required=False, widget=TextInput(attrs={'size':'30'}))
patronymic = forms.CharField(required=False, widget=TextInput()) patronymic = forms.CharField(required=False, widget=TextInput(attrs={'size':'30'}))
'''class NameFormset(BaseModelFormSet): '''class NameFormset(BaseModelFormSet):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View File

@ -121,7 +121,18 @@ def view_name_detail(request, handle, order, action="view"):
form.model = name form.model = name
elif action == "delete": elif action == "delete":
person = Person.objects.get(handle=handle) person = Person.objects.get(handle=handle)
person.name_set.get(order=order).delete() name_to_delete = person.name_set.get(order=order)
was_preferred = name_to_delete.preferred
name_to_delete.delete()
names = person.name_set.all().order_by("order")
for count in range(names.count()):
if was_preferred:
names[count].preferred = True
was_preferred = False
names[count].order = count
names[count].save()
form = NameForm()
name = Name()
action = "back" action = "back"
elif action == "add": elif action == "add":
person = Person.objects.get(handle=handle) person = Person.objects.get(handle=handle)
@ -129,31 +140,33 @@ def view_name_detail(request, handle, order, action="view"):
form = NameForm() form = NameForm()
form.model = name form.model = name
action = "edit" action = "edit"
elif action == "new":
person = Person.objects.get(handle=handle)
form = NameForm(request.POST)
if form.is_valid():
name.save()
action = "save"
else:
action = "edit"
elif action == "save": elif action == "save":
person = Person.objects.get(handle=handle) person = Person.objects.get(handle=handle)
name = person.name_set.get(order=order) try:
name = person.name_set.get(order=order)
except:
order = person.name_set.count() + 1
name = Name(calendar=0, modifier=0, quality=0,
year1=0, day1=0, month1=0,
sortval = 0, newyear=0, order=order,
sort_as=0, display_as=0, person_id=person.id)
form = NameForm(request.POST, instance=name) form = NameForm(request.POST, instance=name)
form.model = name form.model = name
if form.is_valid(): if form.is_valid():
# now it is preferred: # now it is preferred:
if form.cleaned_data["preferred"]: print "valid"
# but it wasn't: if name.preferred: # was preferred, stil must be
if not name.preferred: form.cleaned_data["preferred"] = True
# set all of the other names to be elif form.cleaned_data["preferred"]: # now is
# not preferred: # set all of the other names to be
person.name_set.filter(~ Q(id=name.id)) \ # not preferred:
.update(preferred=False) print "set"
# else it always was person.name_set.filter(~ Q(id=name.id)) \
.update(preferred=False)
# else some other name is preferred # else some other name is preferred
form.save() print "save"
n = form.save()
print n.preferred
else: else:
action = "edit" action = "edit"
context = RequestContext(request) context = RequestContext(request)