From 82bcb4018cf2a3f5cfc0233da6e2875cc46a21e5 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Wed, 16 May 2012 21:50:00 +0000 Subject: [PATCH] Proper editing of person, name, surname; fixed default types svn: r19544 --- src/data/templates/view_person_detail.html | 13 ++-- src/webapp/grampsdb/forms.py | 89 ++++++++++++---------- src/webapp/grampsdb/models.py | 28 +++---- src/webapp/grampsdb/views.py | 45 ++++++----- 4 files changed, 91 insertions(+), 84 deletions(-) diff --git a/src/data/templates/view_person_detail.html b/src/data/templates/view_person_detail.html index ee3f2b77f..8441023d8 100644 --- a/src/data/templates/view_person_detail.html +++ b/src/data/templates/view_person_detail.html @@ -25,6 +25,9 @@ {% for error in nameform.errors %} Error in name: {{error}}
{% endfor %} + {% for error in surnameform.errors %} + Error in surname: {{error}}
+ {% endfor %} {% endifequal %} {% endif %}
{% csrf_token %} @@ -47,11 +50,11 @@ {% render nameform.suffix user action %} - {{nameform.surname.label}}: - {% render nameform.prefix user action %} - {% render nameform.surname user action %} - {{nameform.name_origin_type.label}}: - {% render nameform.name_origin_type user action %} + {{surnameform.surname.label}}: + {% render surnameform.prefix user action %} + {% render surnameform.surname user action %} + {{surnameform.name_origin_type.label}}: + {% render surnameform.name_origin_type user action %} {{personform.gender_type.label}}: diff --git a/src/webapp/grampsdb/forms.py b/src/webapp/grampsdb/forms.py index 40535774e..4a5647ffc 100644 --- a/src/webapp/grampsdb/forms.py +++ b/src/webapp/grampsdb/forms.py @@ -33,7 +33,8 @@ import datetime class PersonForm(forms.ModelForm): class Meta: model = Person - exclude = ["death", "birth", "handle", "birth_ref_index", "death_ref_index"] + exclude = ["death", "birth", "handle", "birth_ref_index", + "death_ref_index"] class NameForm(forms.ModelForm): class Meta: @@ -47,14 +48,56 @@ class NameForm(forms.ModelForm): "sortval", "newyear", "person"] # Add these because they are TextFields, which render as # Textareas: - surname = forms.CharField(label="Surname", - required=False, - widget=TextInput(attrs={'size':'30'})) first_name = forms.CharField(label="Given", required=False, widget=TextInput(attrs={'size':'60'})) title = forms.CharField(required=False, widget=TextInput(attrs={'size':'15'})) + call = forms.CharField(label="Call", + required=False, + widget=TextInput(attrs={'size':'15'})) + nick = forms.CharField(label="Nick", + required=False, + widget=TextInput(attrs={'size':'15'})) + suffix = forms.CharField(required=False, + initial=' suffix ', + widget=TextInput(attrs={'size':'15', + 'style': 'font-style: italic; color: gray; ', + 'onFocus': """if (this.value == ' suffix ') { + this.value = ''; + } + this.style.color = "black"; + this.style.fontStyle = 'normal'; + """, + 'onBlur': """if (this.value == '') { + this.value = ' suffix '; + this.style.color = "gray"; + this.style.fontStyle = 'italic'; + } + """})) + +class NameFormFromPerson(NameForm): + class Meta: + model = Name + # Exclude these, so they don't get checked: + # Excludes sort_as and display_as + exclude = ["order", "calendar", "modifier", + "quality", + #"quality_estimated", "quality_calculated", + #"quality_interpreted", + "year1", "day1", "month1", + "sortval", "newyear", "person", + "sort_as", "display_as"] + +class SurnameForm(forms.ModelForm): + class Meta: + model = Surname + exclude = ['name'] + + surname = forms.CharField(label="Surname", + required=False, + widget=TextInput(attrs={'size':'30'})) + prefix = forms.CharField(label="Prefix", required=False, initial=' prefix ', @@ -72,41 +115,3 @@ class NameForm(forms.ModelForm): this.style.fontStyle = 'italic'; } """})) - suffix = forms.CharField(required=False, - initial=' suffix ', - widget=TextInput(attrs={'size':'15', - 'style': 'font-style: italic; color: gray; ', - 'onFocus': """if (this.value == ' suffix ') { - this.value = ''; - } - this.style.color = "black"; - this.style.fontStyle = 'normal'; - """, - 'onBlur': """if (this.value == '') { - this.value = ' suffix '; - this.style.color = "gray"; - this.style.fontStyle = 'italic'; - } - """})) - call = forms.CharField(label="Call", - required=False, - widget=TextInput(attrs={'size':'15'})) - nick = forms.CharField(label="Nick", - required=False, - widget=TextInput(attrs={'size':'15'})) - - name_origin_type = forms.ChoiceField() - -class NameFormFromPerson(NameForm): - class Meta: - model = Name - # Exclude these, so they don't get checked: - # Excludes sort_as and display_as - exclude = ["order", "calendar", "modifier", - "quality", - #"quality_estimated", "quality_calculated", - #"quality_interpreted", - "year1", "day1", "month1", - "sortval", "newyear", "person", - "sort_as", "display_as"] - diff --git a/src/webapp/grampsdb/models.py b/src/webapp/grampsdb/models.py index b363920d6..e946cf411 100644 --- a/src/webapp/grampsdb/models.py +++ b/src/webapp/grampsdb/models.py @@ -115,35 +115,35 @@ class NameType(mGrampsType): from gen.lib.nametype import NameType _DATAMAP = get_datamap(NameType) _CUSTOM = NameType._CUSTOM - _DEFAULT = _DATAMAP[NameType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == NameType._DEFAULT] val = models.IntegerField('name type', choices=_DATAMAP, blank=False) class NameOriginType(mGrampsType): from gen.lib.nameorigintype import NameOriginType _DATAMAP = get_datamap(NameOriginType) _CUSTOM = NameOriginType._CUSTOM - _DEFAULT = _DATAMAP[NameOriginType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == NameOriginType._DEFAULT] val = models.IntegerField('name origin type', choices=_DATAMAP, blank=False) class AttributeType(mGrampsType): from gen.lib.attrtype import AttributeType _DATAMAP = get_datamap(AttributeType) _CUSTOM = AttributeType._CUSTOM - _DEFAULT = _DATAMAP[AttributeType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == AttributeType._DEFAULT] val = models.IntegerField('attribute type', choices=_DATAMAP, blank=False) class UrlType(mGrampsType): from gen.lib.urltype import UrlType _DATAMAP = get_datamap(UrlType) _CUSTOM = UrlType._CUSTOM - _DEFAULT = _DATAMAP[UrlType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == UrlType._DEFAULT] val = models.IntegerField('url type', choices=_DATAMAP, blank=False) class ChildRefType(mGrampsType): from gen.lib.childreftype import ChildRefType _DATAMAP = get_datamap(ChildRefType) _CUSTOM = ChildRefType._CUSTOM - _DEFAULT = _DATAMAP[ChildRefType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == ChildRefType._DEFAULT] val = models.IntegerField('child reference type', choices=_DATAMAP, blank=False) @@ -151,14 +151,14 @@ class RepositoryType(mGrampsType): from gen.lib.repotype import RepositoryType _DATAMAP = get_datamap(RepositoryType) _CUSTOM = RepositoryType._CUSTOM - _DEFAULT = _DATAMAP[RepositoryType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == RepositoryType._DEFAULT] val = models.IntegerField('repository type', choices=_DATAMAP, blank=False) class EventType(mGrampsType): from gen.lib.eventtype import EventType _DATAMAP = get_datamap(EventType) _CUSTOM = EventType._CUSTOM - _DEFAULT = _DATAMAP[EventType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == EventType._DEFAULT] BIRTH = 12 DEATH = 13 val = models.IntegerField('event type', choices=_DATAMAP, blank=False) @@ -167,7 +167,7 @@ class FamilyRelType(mGrampsType): from gen.lib.familyreltype import FamilyRelType _DATAMAP = get_datamap(FamilyRelType) _CUSTOM = FamilyRelType._CUSTOM - _DEFAULT = _DATAMAP[FamilyRelType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == FamilyRelType._DEFAULT] val = models.IntegerField('family relation type', choices=_DATAMAP, blank=False) @@ -175,7 +175,7 @@ class SourceMediaType(mGrampsType): from gen.lib.srcmediatype import SourceMediaType _DATAMAP = get_datamap(SourceMediaType) _CUSTOM = SourceMediaType._CUSTOM - _DEFAULT = _DATAMAP[SourceMediaType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == SourceMediaType._DEFAULT] val = models.IntegerField('source medium type', choices=_DATAMAP, blank=False) @@ -183,14 +183,14 @@ class EventRoleType(mGrampsType): from gen.lib.eventroletype import EventRoleType _DATAMAP = get_datamap(EventRoleType) _CUSTOM = EventRoleType._CUSTOM - _DEFAULT = _DATAMAP[EventRoleType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == EventRoleType._DEFAULT] val = models.IntegerField('event role type', choices=_DATAMAP, blank=False) class NoteType(mGrampsType): from gen.lib.notetype import NoteType _DATAMAP = get_datamap(NoteType) _CUSTOM = NoteType._CUSTOM - _DEFAULT = _DATAMAP[NoteType._DEFAULT] + _DEFAULT = [x for x in _DATAMAP if x[0] == NoteType._DEFAULT] val = models.IntegerField('note type', choices=_DATAMAP, blank=False) class MarkupType(mGrampsType): @@ -257,7 +257,7 @@ class CalendarType(mGrampsType): (CAL_ISLAMIC, "Islamic"), (CAL_SWEDISH, "Swedish")] - _DEFAULT = _DATAMAP[CAL_GREGORIAN] + _DEFAULT = [x for x in _DATAMAP if x[0] == CAL_GREGORIAN] val = models.IntegerField('Calendar', choices=_DATAMAP, blank=False) class DateModifierType(mGrampsType): @@ -277,7 +277,7 @@ class DateModifierType(mGrampsType): (MOD_SPAN, "Span"), (MOD_TEXTONLY, "Text only")] - _DEFAULT = _DATAMAP[MOD_NONE] + _DEFAULT = [x for x in _DATAMAP if x[0] == MOD_NONE] val = models.IntegerField('Date modifier', choices=_DATAMAP, blank=False) class DateNewYearType(mGrampsType): @@ -291,7 +291,7 @@ class DateNewYearType(mGrampsType): (NEWYEAR_MAR25, "March 25"), (NEWYEAR_SEP1, "September 1")] - _DEFAULT = _DATAMAP[NEWYEAR_JAN1] + _DEFAULT = [x for x in _DATAMAP if x[0] == NEWYEAR_JAN1] val = models.IntegerField('New Year start date', choices=_DATAMAP, blank=False) class ThemeType(mGrampsType): diff --git a/src/webapp/grampsdb/views.py b/src/webapp/grampsdb/views.py index a4137eb4a..561b5d4a5 100644 --- a/src/webapp/grampsdb/views.py +++ b/src/webapp/grampsdb/views.py @@ -495,7 +495,7 @@ def view_person_detail(request, view, handle, action="view"): action = request.POST.get("action") if request.user.is_authenticated(): if action == "edit": - pf, nf, person = get_person_forms(handle, empty=True) + pf, nf, sf, person = get_person_forms(handle, empty=True) elif action == "add": # make new data: person = Person() @@ -530,16 +530,20 @@ def view_person_detail(request, view, handle, action="view"): pf.model = person nf = NameFormFromPerson(request.POST, instance=name) nf.model = name - if nf.is_valid() and pf.is_valid(): + sf = SurnameForm(request.POST, instance=surname) + if nf.is_valid() and pf.is_valid() and sf.is_valid(): person = pf.save() + # Process data: name = nf.save(commit=False) - # Manually set any name data: + # 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() - # Manually set surname data: - surname.surname = nf.cleaned_data["surname"] - surname.prefix = nf.cleaned_data["prefix"] if nf.cleaned_data["prefix"] != " prefix " else "" - surname.name = name + # Process data: + 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 # FIXME: update cache @@ -548,7 +552,7 @@ def view_person_detail(request, view, handle, action="view"): else: # not valid, try again: action = "edit" else: # view - pf, nf, person = get_person_forms(handle) + pf, nf, sf, person = get_person_forms(handle) else: # view person detail # BEGIN NON-AUTHENTICATED ACCESS try: @@ -557,13 +561,14 @@ def view_person_detail(request, view, handle, action="view"): raise Http404(_("Requested %s does not exist.") % view) if person.private: raise Http404(_("Requested %s is not accessible.") % view) - pf, nf, person = get_person_forms(handle, protect=True) + pf, nf, sf, person = get_person_forms(handle, protect=True) # END NON-AUTHENTICATED ACCESS context["action"] = action context["view"] = view context["tview"] = _("Person") context["personform"] = pf context["nameform"] = nf + context["surnameform"] = sf context["person"] = person context["object"] = person context["next"] = "/person/%s" % person.handle @@ -577,26 +582,20 @@ def get_person_forms(handle, protect=False, empty=False): except: name = Name(person=person, preferred=True) try: - primary_surname = name.surname_set.get(primary=True) + surname = name.surname_set.get(primary=True) except: - primary_surname = Surname(name=name, primary=True) + 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, - "name_origin_type": primary_surname.name_origin_type, - "title": name.title, - "nick": name.nick, - "call": name.call, - } pf = PersonForm(instance=person) pf.model = person - nf = NameForm(default_data, instance=name) + name.suffix = make_empty(empty, name.suffix, " suffix ") + nf = NameForm(instance=name) nf.model = name - return pf, nf, person + surname.prefix = make_empty(empty, surname.prefix, " prefix ") + sf = SurnameForm(instance=surname) + sf.model = surname + return pf, nf, sf, person def make_empty(empty, value, empty_value): if value: