From 48ef83fbcce5f41ed7ccf6c741051810023ae944 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Thu, 24 May 2012 12:28:34 +0000 Subject: [PATCH] Editing of Person, Name, and surname is nearly complete (need to fix adding second name) svn: r19633 --- src/data/templates/view_name_detail.html | 5 + src/data/templates/view_person_detail.html | 2 +- src/data/templates/view_surname_detail.html | 38 +++-- src/webapp/grampsdb/views.py | 166 +++++++++++++++++--- src/webapp/libdjango.py | 3 + 5 files changed, 175 insertions(+), 39 deletions(-) diff --git a/src/data/templates/view_name_detail.html b/src/data/templates/view_name_detail.html index 5864d39dc..96da284be 100644 --- a/src/data/templates/view_name_detail.html +++ b/src/data/templates/view_name_detail.html @@ -117,6 +117,10 @@ value="Back to Person" onclick="document.location.href='/person/{{person.handle}}'"/> {% if user.is_authenticated %} + {% ifequal action "add" %} + + + {% else %} {% ifequal action "edit" %} @@ -131,6 +135,7 @@ value="Delete Name" onclick="document.location.href='/person/{{person.handle}}/name/{{order}}/delete'"/> {% endifequal %} + {% endifequal %} {% else %} {% endif %} diff --git a/src/data/templates/view_person_detail.html b/src/data/templates/view_person_detail.html index 58489b4b3..e844eb666 100644 --- a/src/data/templates/view_person_detail.html +++ b/src/data/templates/view_person_detail.html @@ -136,7 +136,7 @@ {% ifequal action "add" %} {% make_button "Cancel" "/person/" %} - + {% else %} {% make_button "Back to People" "/person" %} {% make_button "Add Person" "/person/add" %} diff --git a/src/data/templates/view_surname_detail.html b/src/data/templates/view_surname_detail.html index e8719eeea..fe91a9712 100644 --- a/src/data/templates/view_surname_detail.html +++ b/src/data/templates/view_surname_detail.html @@ -21,14 +21,9 @@
{% comment %} 4 cols {% endcomment %} - -{% if user.is_authenticated %} - {% ifequal action "edit" %} - {% for error in form.errors %} - {{error}}
- {% endfor %} - {% endifequal %} -{% endif %} +{% for error in form.errors %} +

{{error}}

+{% endfor %} {% csrf_token %} @@ -67,23 +62,34 @@ {% if user.is_authenticated %} - {% ifequal action "edit" %} - - + {% if action == "edit" %} - {% else %} + + + {% else %} + {% if action == "add" %} + value="Cancel" + onclick="document.location.href='/person/{{person.handle}}/name/{{order}}/surname/{{sorder}}'"/> + + + {% else %} - {% endifequal %} + onclick="document.location.href='/person/{{person.handle}}/name/{{order}}'"/> + + + {% endif %} + {% endif %} {% else %} 1: + neworder = 1 + for surname in surnames: + if surname.order != neworder: + surname.order = neworder + surname.save() + neworder += 1 + elif surname.order == int(sorder): + surname.delete() + else: + neworder += 1 + else: + request.user.message_set.create(message="You can't delete the only surname") + return redirect("/person/%s/name/%s" % (person.handle, name.order)) + elif action in ["add"]: + surname = Surname(name=name, primary=False, + name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0])) + surname.prefix = make_empty(True, surname.prefix, " prefix ") + elif action == "create": + surnames = name.surname_set.all().order_by("order") + sorder = 1 + for surname in surnames: + if surname.order != sorder: + surname.order = sorder + surname.save() + sorder += 1 + surname = Surname(name=name, primary=True, + name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0]), + order=sorder) + sf = SurnameForm(request.POST, instance=surname) + sf.model = surname + if sf.is_valid(): + surname.prefix = ssf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else "" + check_primary(surname, surnames) + sf.save() + return redirect("/person/%s/name/%s/surname/%s" % + (person.handle, name.order, sorder)) + action = "edit" + surname.prefix = make_empty(True, surname.prefix, " prefix ") + elif action == "save": + surname = name.surname_set.get(order=sorder) + sf = SurnameForm(request.POST, instance=surname) + sf.model = surname + if sf.is_valid(): + surname.prefix = ssf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else "" + check_primary(surname, name.surname_set.all().exclude(order=surname.order)) + sf.save() + return redirect("/person/%s/name/%s/surname/%s" % + (person.handle, name.order, sorder)) + action = "edit" + surname.prefix = make_empty(True, surname.prefix, " prefix ") + # else, edit again + else: + raise - # FIXME: working on add/save/create + sf = SurnameForm(instance=surname) + sf.model = surname context = RequestContext(request) context["action"] = action @@ -192,13 +267,14 @@ def process_surname(request, handle, order, sorder, action="view"): context["id"] = id context["person"] = person context["object"] = person - context["surnameform"] = surnameform + context["surnameform"] = sf context["order"] = name.order context["sorder"] = sorder view_template = 'view_surname_detail.html' return render_to_response(view_template, context) def process_name(request, handle, order, action="view"): + ## FIXME: can't add second name if order == "add": order = 0 action = "add" @@ -237,29 +313,37 @@ def process_name(request, handle, order, action="view"): name_type=NameType.objects.get(val=NameType._DEFAULT[0])) nf = NameForm(instance=name) nf.model = name - surname = Surname(name=name, primary=True, order=1) + surname = Surname(name=name, + primary=True, + order=1, + name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0])) sf = SurnameForm(request.POST, instance=surname) - action = "edit" - elif action == "save": - # look up old data, if any: + elif action == "create": + # look up old data: person = Person.objects.get(handle=handle) - oldname = person.name_set.get(preferred=True) - surname = oldname.surname_set.get(primary=True) + name = Name(preferred=False) + name.person = person + next_order = max([name.order for name in person.name_set.all()]) + 1 + surname = Surname(name=name, + primary=True, + order=next_order, + name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0])) # combine with user data: pf = PersonForm(request.POST, instance=person) pf.model = person - nf = NameForm(request.POST, instance=oldname) - nf.model = oldname + nf = NameForm(request.POST, instance=name) + nf.model = name sf = SurnameForm(request.POST, instance=surname) if nf.is_valid() and sf.is_valid(): # name.preferred and surname.primary get set False in the above is_valid() # person = pf.save() # Process data: - oldname.person = person + name.person = person name = nf.save() # Manually set any data: name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else "" - name.preferred = True # FIXME: why is this False? + name.preferred = False # FIXME: why is this False? + name.order = next_order name.save() # Process data: surname.name = name @@ -274,6 +358,40 @@ def process_name(request, handle, order, action="view"): return redirect("/person/%s/name/%s" % (person.handle, name.order)) else: action = "add" + elif action == "save": + # look up old data: + person = Person.objects.get(handle=handle) + oldname = person.name_set.get(preferred=True) + oldsurname = oldname.surname_set.get(primary=True) + # combine with user data: + pf = PersonForm(request.POST, instance=person) + pf.model = person + nf = NameForm(request.POST, instance=oldname) + nf.model = oldname + sf = SurnameForm(request.POST, instance=oldsurname) + if nf.is_valid() and sf.is_valid(): + # name.preferred and surname.primary get set False in the above is_valid() + # person = pf.save() + # Process data: + oldname.person = person + name = nf.save() + # Manually set any data: + name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else "" + name.preferred = True # FIXME: why is this False? + name.save() + # Process data: + oldsurname.name = name + surname = sf.save(commit=False) + # Manually set any data: + surname.prefix = sf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else "" + surname.primary = True # FIXME: why is this False? + surname.save() + # FIXME: last_saved, last_changed, last_changed_by + dji.rebuild_cache(person) + # FIXME: update probably_alive + return redirect("/person/%s/name/%s" % (person.handle, name.order)) + else: + action = "edit" context = RequestContext(request) context["action"] = action context["tview"] = _('Name') @@ -877,6 +995,10 @@ def process_person(request, context, handle, action): # view, edit, save person = Person(handle=create_id()) name = Name(person=person, preferred=True) surname = Surname(name=name, primary=True, order=1) + surname = Surname(name=name, + primary=True, + order=1, + name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0])) # combine with user data: pf = PersonForm(request.POST, instance=person) pf.model = person diff --git a/src/webapp/libdjango.py b/src/webapp/libdjango.py index f546baa28..735625f63 100644 --- a/src/webapp/libdjango.py +++ b/src/webapp/libdjango.py @@ -636,6 +636,7 @@ class DjangoInterface(object): self.add_event_ref(obj, event_ref) def add_surname_list(self, name, surname_list): + order = 1 for data in surname_list: (surname_text, prefix, primary, origin_type, connector) = data @@ -647,7 +648,9 @@ class DjangoInterface(object): origin_type) surname.connector = connector surname.name = name + surname.order = order surname.save() + order += 1 def add_note_list(self, obj, note_list): for handle in note_list:
{{surnameform.name_origin_type.label}}: