I believe that all Person, Name, and Surname editing is done

svn: r19637
This commit is contained in:
Doug Blank 2012-05-24 17:22:14 +00:00
parent eadc089835
commit fe01467480
4 changed files with 141 additions and 69 deletions

View File

@ -17,7 +17,7 @@
{% endfilter %} {% endfilter %}
<h3>{{person|make_name:user}} [{{person.gramps_id}}]</h3> <h3>{{nameform.model|make_name:user}} [{{person.gramps_id}}]</h3>
<div id="summaryarea"> <div id="summaryarea">
<table class="infolist" style="width:90%;"> {% comment %} 4 cols {% endcomment %} <table class="infolist" style="width:90%;"> {% comment %} 4 cols {% endcomment %}
<tbody> <tbody>
@ -54,11 +54,7 @@
</tr> </tr>
<tr> <tr>
<td class="ColumnAttribute">{{nameform.preferred.label}}:</td> <td class="ColumnAttribute">{{nameform.preferred.label}}:</td>
{% if nameform.model.preferred %}
<td class="ColumnValue" id="data">True</td>
{% else %}
<td class="ColumnValue" id="data">{% render nameform.preferred user action %}</td> <td class="ColumnValue" id="data">{% render nameform.preferred user action %}</td>
{% endif %}
<td class="ColumnAttribute">{{nameform.private.label}}:</td> <td class="ColumnAttribute">{{nameform.private.label}}:</td>
<td class="ColumnValue" id="data">{% render nameform.private user action %}</td> <td class="ColumnValue" id="data">{% render nameform.private user action %}</td>
</tr> </tr>

View File

@ -625,14 +625,12 @@ class Name(DateObject, SecondaryObject):
person = models.ForeignKey("Person") person = models.ForeignKey("Person")
_sanitized = False _sanitized = False
def get_primary_surname(self):
try:
return self.surname_set.get(primary=True).surname
except:
return ""
def __unicode__(self): def __unicode__(self):
return "%s, %s" % (self.get_primary_surname(), try:
surname = self.surname_set.get(primary=True)
except:
surname = "[No primary surname]"
return "%s, %s" % (surname,
self.first_name) self.first_name)
@staticmethod @staticmethod
def get_dummy(): def get_dummy():

View File

@ -169,19 +169,78 @@ def set_date(obj):
obj.day1, obj.month1, obj.year1, obj.slash1 = 0, 0, 0, 0 obj.day1, obj.month1, obj.year1, obj.slash1 = 0, 0, 0, 0
obj.day2, obj.month2, obj.year2, obj.slash2 = 0, 0, 0, 0 obj.day2, obj.month2, obj.year2, obj.slash2 = 0, 0, 0, 0
def check_order(person):
"""
Check for proper ordering 1..., and for a preferred name.
"""
order = 1
preferred = False
for name in person.name_set.all().order_by("order"):
if name.preferred:
preferred = True
if name.order != order:
name.order = order
name.save()
order += 1
if not preferred:
name = person.name_set.get(order=1)
name.preferred = True
name.save()
def check_primary(surname, surnames): def check_primary(surname, surnames):
"""
Check for a proper primary surname.
"""
if surname.primary: if surname.primary:
# then all reast should not be: # then all rest should not be:
for s in surnames: for s in surnames:
if s.primary: if s.primary:
s.primary = False s.primary = False
s.save() s.save()
else: else:
# then one of them should be # then one of them should be
if not any([s.primary for s in surnames]): ok = False
surnames[0].primary = True for s in surnames:
surnames[0].save() if s.id != surname.id:
if s.primary:
ok = True
break
else:
s.primary = False
s.save()
ok = True
break
if not ok:
name.primary = True
def check_preferred(name, person):
"""
Check for a proper preferred name.
"""
names = []
if person:
names = person.name_set.all()
if name.preferred:
# then all reast should not be:
for s in names:
if s.preferred and s.id != name.id:
s.preferred = False
s.save()
else:
# then one of them should be
ok = False
for s in names:
if s.id != name.id:
if s.preferred:
ok = True
break
else:
s.preferred = False
s.save()
ok = True
break
if not ok:
name.preferred = True
def process_surname(request, handle, order, sorder, action="view"): def process_surname(request, handle, order, sorder, action="view"):
# /sdjhgsdjhdhgsd/name/1/surname/1 (view) # /sdjhgsdjhdhgsd/name/1/surname/1 (view)
@ -235,8 +294,8 @@ def process_surname(request, handle, order, sorder, action="view"):
sf.model = surname sf.model = surname
if sf.is_valid(): if sf.is_valid():
surname.prefix = ssf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else "" surname.prefix = ssf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else ""
surname = sf.save(commit=False)
check_primary(surname, surnames) check_primary(surname, surnames)
sf.save()
return redirect("/person/%s/name/%s/surname/%s" % return redirect("/person/%s/name/%s/surname/%s" %
(person.handle, name.order, sorder)) (person.handle, name.order, sorder))
action = "edit" action = "edit"
@ -247,8 +306,9 @@ def process_surname(request, handle, order, sorder, action="view"):
sf.model = surname sf.model = surname
if sf.is_valid(): if sf.is_valid():
surname.prefix = ssf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else "" surname.prefix = ssf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else ""
surname = sf.save(commit=False)
check_primary(surname, name.surname_set.all().exclude(order=surname.order)) check_primary(surname, name.surname_set.all().exclude(order=surname.order))
sf.save() surname.save()
return redirect("/person/%s/name/%s/surname/%s" % return redirect("/person/%s/name/%s/surname/%s" %
(person.handle, name.order, sorder)) (person.handle, name.order, sorder))
action = "edit" action = "edit"
@ -274,40 +334,31 @@ def process_surname(request, handle, order, sorder, action="view"):
return render_to_response(view_template, context) return render_to_response(view_template, context)
def process_name(request, handle, order, action="view"): def process_name(request, handle, order, action="view"):
## FIXME: can't add second name
if order == "add": if order == "add":
order = 0
action = "add" action = "add"
if request.POST.has_key("action"): if request.POST.has_key("action"):
action = request.POST.get("action") action = request.POST.get("action")
### Process action: ### Process action:
if action == "view": if action == "view":
pf, nf, sf, person = get_person_forms(handle) pf, nf, sf, person = get_person_forms(handle, order=order)
name = nf.model name = nf.model
elif action == "edit": elif action == "edit":
pf, nf, sf, person = get_person_forms(handle) pf, nf, sf, person = get_person_forms(handle, order=order)
name = nf.model name = nf.model
elif action == "delete": elif action == "delete":
person = Person.objects.get(handle=handle) person = Person.objects.get(handle=handle)
names = person.name_set.all().order_by("order") name = person.name_set.filter(order=order)
if names.count() > 1: names = person.name_set.all()
name_to_delete = names[0] if len(names) > 1:
was_preferred = name_to_delete.preferred name.delete()
name_to_delete.delete() check_order(person)
names = person.name_set.all().order_by("order") else:
for count in range(names[1:].count()): request.user.message_set.create(message = "Can't delete only name.")
if was_preferred: return redirect("/person/%s" % person.handle)
names[count].preferred = True
was_preferred = False
names[count].order = count
names[count].save()
nf = NameForm()
name = Name()
nf.model = name
action = "back"
elif action == "add": # add name elif action == "add": # add name
person = Person.objects.get(handle=handle) person = Person.objects.get(handle=handle)
name = Name(person=person, name = Name(person=person,
preferred=False,
display_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]), display_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]),
sort_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]), sort_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]),
name_type=NameType.objects.get(val=NameType._DEFAULT[0])) name_type=NameType.objects.get(val=NameType._DEFAULT[0]))
@ -319,35 +370,35 @@ def process_name(request, handle, order, action="view"):
name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0])) name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0]))
sf = SurnameForm(request.POST, instance=surname) sf = SurnameForm(request.POST, instance=surname)
elif action == "create": elif action == "create":
# look up old data: # make new data
person = Person.objects.get(handle=handle) person = Person.objects.get(handle=handle)
name = Name(preferred=False) name = Name(preferred=False)
name.person = person
next_order = max([name.order for name in person.name_set.all()]) + 1 next_order = max([name.order for name in person.name_set.all()]) + 1
surname = Surname(name=name, surname = Surname(name=name,
primary=True, primary=True,
order=next_order, order=next_order,
name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0])) name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0]))
# combine with user data: # combine with user data:
pf = PersonForm(request.POST, instance=person)
pf.model = person
nf = NameForm(request.POST, instance=name) nf = NameForm(request.POST, instance=name)
name.id = None # FIXME: why did this get set to an existing name? Should be new.
name.preferred = False
nf.model = name nf.model = name
sf = SurnameForm(request.POST, instance=surname) sf = SurnameForm(request.POST, instance=surname)
sf.model = surname
if nf.is_valid() and sf.is_valid(): if nf.is_valid() and sf.is_valid():
# name.preferred and surname.primary get set False in the above is_valid() # name.preferred and surname.primary get set False in the above is_valid()
# person = pf.save() # person = pf.save()
# Process data: # Process data:
name = nf.save(commit=False)
name.person = person name.person = person
name = nf.save()
# Manually set any data: # Manually set any data:
name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else "" name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else ""
name.preferred = False # FIXME: why is this False? name.preferred = False # FIXME: why is this False?
name.order = next_order name.order = next_order
name.save() name.save()
# Process data: # Process data:
surname.name = name
surname = sf.save(commit=False) surname = sf.save(commit=False)
surname.name = name
# Manually set any data: # Manually set any data:
surname.prefix = sf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else "" surname.prefix = sf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else ""
surname.primary = True # FIXME: why is this False? surname.primary = True # FIXME: why is this False?
@ -361,7 +412,7 @@ def process_name(request, handle, order, action="view"):
elif action == "save": elif action == "save":
# look up old data: # look up old data:
person = Person.objects.get(handle=handle) person = Person.objects.get(handle=handle)
oldname = person.name_set.get(preferred=True) oldname = person.name_set.get(order=order)
oldsurname = oldname.surname_set.get(primary=True) oldsurname = oldname.surname_set.get(primary=True)
# combine with user data: # combine with user data:
pf = PersonForm(request.POST, instance=person) pf = PersonForm(request.POST, instance=person)
@ -378,6 +429,7 @@ def process_name(request, handle, order, action="view"):
# Manually set any data: # Manually set any data:
name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else "" name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else ""
name.preferred = True # FIXME: why is this False? name.preferred = True # FIXME: why is this False?
check_preferred(name, person)
name.save() name.save()
# Process data: # Process data:
oldsurname.name = name oldsurname.name = name
@ -403,7 +455,7 @@ def process_name(request, handle, order, action="view"):
context["object"] = person context["object"] = person
context["nameform"] = nf context["nameform"] = nf
context["surnameform"] = sf context["surnameform"] = sf
context["order"] = name.order context["order"] = order
context["next"] = "/person/%s/name/%d" % (person.handle, name.order) context["next"] = "/person/%s/name/%d" % (person.handle, name.order)
view_template = "view_name_detail.html" view_template = "view_name_detail.html"
return render_to_response(view_template, context) return render_to_response(view_template, context)
@ -1015,6 +1067,7 @@ def process_person(request, context, handle, action): # view, edit, save
# Manually set any data: # Manually set any data:
name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else "" name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else ""
name.preferred = True # FIXME: why is this False? name.preferred = True # FIXME: why is this False?
check_preferred(name, person)
name.save() name.save()
# Process data: # Process data:
surname.name = name surname.name = name
@ -1056,20 +1109,27 @@ def process_person(request, context, handle, action): # view, edit, save
context["object"] = person context["object"] = person
context["next"] = "/person/%s" % person.handle context["next"] = "/person/%s" % person.handle
def get_person_forms(handle, protect=False, empty=False): def get_person_forms(handle, protect=False, empty=False, order=None):
if handle: if handle:
person = Person.objects.get(handle=handle) person = Person.objects.get(handle=handle)
else: else:
person = Person() person = Person()
#person.gramps_id = "I0000" # FIXME: get next ID #person.gramps_id = "I0000" # FIXME: get next ID
## get a name ## get a name
try: name = None
name = person.name_set.get(preferred=True) if order is not None:
except: try:
name = Name(person=person, preferred=True, name = person.name_set.get(order=order)
display_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]), except:
sort_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]), pass
name_type=NameType.objects.get(val=NameType._DEFAULT[0])) if name is None:
try:
name = person.name_set.get(preferred=True)
except:
name = Name(person=person, preferred=True,
display_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]),
sort_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]),
name_type=NameType.objects.get(val=NameType._DEFAULT[0]))
## get a surname ## get a surname
try: try:
surname = name.surname_set.get(primary=True) surname = name.surname_set.get(primary=True)

View File

@ -290,19 +290,25 @@ def surname_table(obj, user, action, url=None, *args):
order = args[1] order = args[1]
retval = "" retval = ""
table = Table() table = Table()
table.columns(_("Surname"),) table.columns(_("Order"), _("Surname"),)
if user.is_authenticated(): if user.is_authenticated():
links = [] try:
count = 1 name = obj.name_set.filter(order=order)[0]
name = obj.name_set.filter(order=order)[0] except:
for surname in name.surname_set.all(): name = None
table.row(surname.surname) if name:
links.append(('URL', links = []
# url is "/person/%s/name/%s/surname" count = 1
(url % args) + ("/%s" % count))) for surname in name.surname_set.all():
count += 1 table.row(str(count), surname.surname)
table.links(links) links.append(('URL',
retval += table.get_html() # url is "/person/%s/name/%s/surname"
(url % args) + ("/%s" % count)))
count += 1
table.links(links)
retval += table.get_html()
else:
retval += "<p id='error'>No such name order = %s</p>" % order
if user.is_superuser and url and action == "view": if user.is_superuser and url and action == "view":
retval += make_button(_("Add surname"), (url + "/add") % args) retval += make_button(_("Add surname"), (url + "/add") % args)
else: else:
@ -631,11 +637,19 @@ def render_name(name, user):
if isinstance(name, models.Name): if isinstance(name, models.Name):
if not user.is_authenticated(): if not user.is_authenticated():
name.sanitize() name.sanitize()
return "%s, %s" % (name.get_primary_surname(), name.first_name) try:
surname = name.surname_set.get(primary=True)
except:
surname = "[No primary surname]"
return "%s, %s" % (surname, name.first_name)
elif isinstance(name, forms.NameForm): elif isinstance(name, forms.NameForm):
if not user.is_authenticated(): if not user.is_authenticated():
name.model.sanitize() name.model.sanitize()
return "%s, %s" % (name.model.get_primary_surname(), try:
surname = name.model.surname_set.get(primary=True)
except:
surname = "[No primary surname]"
return "%s, %s" % (surname,
name.model.first_name) name.model.first_name)
elif isinstance(name, gen.lib.Person): # name is a gen.lib.Person elif isinstance(name, gen.lib.Person): # name is a gen.lib.Person
person = name person = name
@ -647,7 +661,11 @@ def render_name(name, user):
return "[No preferred name]" return "[No preferred name]"
if not user.is_authenticated(): if not user.is_authenticated():
name.sanitize() name.sanitize()
return "%s, %s" % (name.get_primary_surname(), name.first_name) try:
surname = name.surname_set.get(primary=True)
except:
surname = "[No primary surname]"
return "%s, %s" % (surname, name.first_name)
elif isinstance(name, models.Person): # django person elif isinstance(name, models.Person): # django person
person = name person = name
try: try: