Making progress on person edit

svn: r19535
This commit is contained in:
Doug Blank 2012-05-16 01:25:58 +00:00
parent 400ce7de33
commit c4b84b1e03
2 changed files with 64 additions and 70 deletions

View File

@ -486,7 +486,8 @@ def view_person_detail(request, view, handle, action="view"):
context["tview"] = _("Person")
context["tviews"] = _("People")
if handle == "add":
if request.POST.has_key("action"):
# FIXME: what should handle be then?
if request.POST.has_key("action"): # save
action = request.POST.get("action")
else:
action = "add"
@ -494,29 +495,7 @@ def view_person_detail(request, view, handle, action="view"):
action = request.POST.get("action")
if request.user.is_authenticated():
if action == "edit":
# get all of the data:
person = Person.objects.get(handle=handle)
try:
name = person.name_set.get(preferred=True)
except:
name = Name(person=person, preferred=True)
try:
primary_surname = name.surname_set.get(primary=True)
except:
primary_surname = Surname(name=name, primary=True)
default_data = {"surname": primary_surname.surname,
"prefix": primary_surname.prefix or " prefix ",
"suffix": name.suffix or " suffix ",
"first_name": name.first_name,
"name_type": name.name_type,
"title": name.title,
"nick": name.nick,
"call": name.call,
}
pf = PersonForm(instance=person)
pf.model = person
nf = NameForm(default_data, instance=name)
nf.model = name
pf, nf, person = get_person_forms(handle, empty=True)
elif action == "add":
# make new data:
person = Person()
@ -554,58 +533,38 @@ def view_person_detail(request, view, handle, action="view"):
nf = NameFormFromPerson(request.POST, instance=name)
nf.model = name
if nf.is_valid() and pf.is_valid():
name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else ""
name.title = nf.cleaned_data["title"]
name.nick = nf.cleaned_data["nick"]
name.name_type = nf.cleaned_data["name_type"]
name.call = nf.cleaned_data["call"]
name.first_name = nf.cleaned_data["first_name"]
surname.surname = nf.cleaned_data["surname"]
surname.prefix = nf.cleaned_data["prefix"] if nf.cleaned_data["prefix"] != " prefix " else ""
name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else ""
person = pf.save()
name = nf.save(commit=False)
name.person = person
pf.save()
nf.save(commit=False)
surname.save()
name.save()
else:
import pdb; pdb.set_trace()
#name.save() # FIXME: why this get rid of name.person?
# FIXME: update cache
# FIXME: update probably_alive
return redirect("/person/%s" % person.handle)
else: # not valid, try again:
action = "edit"
else: # view
# get all of the data:
person = Person.objects.get(handle=handle)
try:
name = person.name_set.get(preferred=True)
except:
name = Name(person=person, preferred=True)
try:
primary_surname = name.surname_set.get(primary=True)
except:
primary_surname = Surname(name=name, primary=True)
default_data = {"surname": primary_surname.surname,
"prefix": primary_surname.prefix or " prefix ",
"suffix": name.suffix or " suffix ",
"first_name": name.first_name,
"name_type": name.name_type,
"title": name.title,
"nick": name.nick,
"call": name.call,
}
pf = PersonForm(instance=person)
pf.model = person
nf = NameForm(default_data, instance=name)
nf.model = name
pf, nf, person = get_person_forms(handle)
else: # view person detail
# BEGIN NON-AUTHENTICATED ACCESS
try:
person = Person.objects.get(handle=handle)
if person:
except:
raise Http404(_("Requested %s does not exist.") % view)
if person.private:
raise Http404(_("Requested %s is not accessible.") % view)
name = person.name_set.get(preferred=True)
if person.probably_alive:
name.sanitize()
else:
raise Http404(_("Requested %s does not exist.") % view)
pf = PersonForm(instance=person)
pf.model = person
nf = NameForm(instance=name)
nf.model = name
pf, nf, person = get_person_forms(handle, protect=True)
# END NON-AUTHENTICATED ACCESS
if action == "save":
return redirect("/person/%s" % person.handle)
context["action"] = action
context["view"] = view
context["tview"] = _("Person")
@ -617,6 +576,41 @@ def view_person_detail(request, view, handle, action="view"):
view_template = 'view_person_detail.html'
return render_to_response(view_template, context)
def get_person_forms(handle, protect=False, empty=False):
person = Person.objects.get(handle=handle)
try:
name = person.name_set.get(preferred=True)
except:
name = Name(person=person, preferred=True)
try:
primary_surname = name.surname_set.get(primary=True)
except:
primary_surname = Surname(name=name, primary=True)
if protect and person.probably_alive:
name.sanitize()
default_data = {"surname": primary_surname.surname,
"prefix": make_empty(empty, primary_surname.prefix, " prefix "),
"suffix": make_empty(empty, name.suffix, " suffix "),
"first_name": name.first_name,
"name_type": name.name_type,
"title": name.title,
"nick": name.nick,
"call": name.call,
}
pf = PersonForm(instance=person)
pf.model = person
nf = NameForm(default_data, instance=name)
nf.model = name
return pf, nf, person
def make_empty(empty, value, empty_value):
if value:
return value
elif empty:
return empty_value
else:
return value
def view(request, view):
context = RequestContext(request)
search = ""

View File

@ -607,13 +607,13 @@ def render(formfield, user, action, test=False, truetext="", id=None):
if (not user.is_authenticated() and not test) or user.is_authenticated():
fieldname = formfield.name # 'surname'
try:
retval = str(getattr(formfield.form.model, fieldname))
retval = "1:" + str(getattr(formfield.form.model, fieldname))
except:
# name, "prefix"
try:
retval = str(formfield.form.data[fieldname]) # formfield._data()
retval = "2:" + str(formfield.form.data[fieldname]) # formfield._data()
except:
retval = "XXX"
retval = "[ERROR: %s]" % fieldname
else:
retval = truetext
else: