From c0eaa76d68a8eca8f65b344561f73e1c730276dc Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Fri, 25 May 2012 23:41:47 +0000 Subject: [PATCH] Editing of Person, Family, and Event main data complete svn: r19652 --- src/data/templates/view_event_detail.html | 44 +++++-- src/data/templates/view_events.html | 2 +- src/data/templates/view_family_detail.html | 21 ++-- src/data/templates/view_people.html | 4 +- src/data/templates/view_person_detail.html | 19 +-- src/webapp/grampsdb/forms.py | 48 ++++++++ src/webapp/grampsdb/models.py | 2 +- src/webapp/grampsdb/view/event.py | 136 ++++++++------------- src/webapp/grampsdb/view/family.py | 3 +- src/webapp/shell.py | 4 + src/webapp/utils.py | 24 ++-- 11 files changed, 178 insertions(+), 129 deletions(-) diff --git a/src/data/templates/view_event_detail.html b/src/data/templates/view_event_detail.html index 9df1f5404..e61425af0 100644 --- a/src/data/templates/view_event_detail.html +++ b/src/data/templates/view_event_detail.html @@ -17,27 +17,30 @@
+ {% if eventform.errors %} +
+

The following fields have errors. Please correct and try again.

+
{{eventform.errors}}
+
+ {% endif %} +
{% csrf_token %}
- - + + - + -{% if user.is_authenticated %} - -{% else %} - -{% endif %} + - + - +
Event type: {{event.event_type|escape}}{{eventform.event_type.label}}: {% render eventform.event_type user action %} Date:{{event|display_date}}{% render eventform.date user action %}
Description:{{event.description|escape}}[Private]{% render eventform.description user action %}
ID:{{event.gramps_id|escape}}{% render eventform.gramps_id user action %}
Place:{{event.place.title|escape}} [{{event.place.gramps_id}}]{% render eventform.place user action %}
@@ -64,6 +67,25 @@ - +{% if user.is_superuser %} + {% if action == "edit" %} + {% make_button "Back to Event" "/event/%s" event.handle %} + + + {% else %} + {% ifequal action "add" %} + {% make_button "Cancel" "/event/" %} + + + {% else %} + {% make_button "Back to Families" "/event" %} + {% make_button "Add Event" "/event/add" %} + {% make_button "Edit Event" "/event/%s/edit" event.handle %} + {% make_button "Delete Event" "/event/%s/delete" event.handle %} + {% endifequal %} + {% endif %} +{% else %} +{% endif %} + {% endblock %} diff --git a/src/data/templates/view_events.html b/src/data/templates/view_events.html index 514ee0145..16adcc16b 100644 --- a/src/data/templates/view_events.html +++ b/src/data/templates/view_events.html @@ -25,7 +25,7 @@ {{event.event_type|nbsp}} {{event.description|nbsp}} - {{event|render_date:user}} + {{event|date_as_text:user}} {{event.place.title|nbsp}} {% else %} diff --git a/src/data/templates/view_family_detail.html b/src/data/templates/view_family_detail.html index acbe63205..d54f836ec 100644 --- a/src/data/templates/view_family_detail.html +++ b/src/data/templates/view_family_detail.html @@ -19,9 +19,12 @@
{% comment %} 4 cols {% endcomment %} - {% for error in familyform.errors %} -

Error in family: {{error}}

- {% endfor %} + {% if familyform.errors %} +
+

The following fields have errors. Please correct and try again.

+
{{familyform.errors}}
+
+ {% endif %} {% csrf_token %} @@ -36,23 +39,23 @@ {% if user.is_authenticated or not familyform.father.probably_alive %} - + - + {% else %} - + - + {% endif %} - + - +
Father
Birth:{{familyform.father.birth|render_date:user}}{{familyform.father.birth|date_as_text:user}} Birth:{{familyform.mother.birth|render_date:user}}{{familyform.mother.birth|date_as_text:user}}
Death:{{familyform.father.death|render_date:user}}{{familyform.father.death|date_as_text:user}} Death:{{familyform.mother.death|render_date:user}}{{familyform.mother.death|date_as_text:user}}
Death:{{familyform.father.death|render_date:user}}{{familyform.father.death|date_as_text:user}} Death:{{familyform.mother.death|render_date:user}}{{familyform.mother.death|date_as_text:user}}
diff --git a/src/data/templates/view_people.html b/src/data/templates/view_people.html index ff70ae308..c11c89ed6 100644 --- a/src/data/templates/view_people.html +++ b/src/data/templates/view_people.html @@ -24,8 +24,8 @@ [{{name.person.gramps_id}}] {{name.person.gender_type}} - {{name.person.birth|render_date:user}} - {{name.person.death|render_date:user}} + {{name.person.birth|date_as_text:user}} + {{name.person.death|date_as_text:user}} {% endif %} {% endfor %} diff --git a/src/data/templates/view_person_detail.html b/src/data/templates/view_person_detail.html index e844eb666..c3bb570e0 100644 --- a/src/data/templates/view_person_detail.html +++ b/src/data/templates/view_person_detail.html @@ -17,15 +17,16 @@
{% comment %} 5 cols {% endcomment %} - {% for error in personform.errors %} -

Error in person: {{error}}

- {% endfor %} - {% for error in nameform.errors %} -

Error in name: {{error}}

- {% endfor %} - {% for error in surnameform.errors %} -

Error in surname: {{error}}

- {% endfor %} + {% if personform.errors or nameform.errors or surnameform.errors %} +
+

The following fields have errors. Please correct and try again.

+
+ {{personform.errors}} + {{nameform.errors}} + {{surnameform.errors}} +
+
+ {% endif %} {% csrf_token %} diff --git a/src/webapp/grampsdb/forms.py b/src/webapp/grampsdb/forms.py index b7b6843f2..9b9714ca0 100644 --- a/src/webapp/grampsdb/forms.py +++ b/src/webapp/grampsdb/forms.py @@ -126,3 +126,51 @@ class FamilyForm(forms.ModelForm): class Meta: model = Family exclude = ["handle"] + +class EventForm(forms.ModelForm): + class Meta: + model = Event + exclude = ["handle", "sortval", "month1", "year1", "day1", + "newyear", "calendar", "modifier", "quality"] + + def __init__(self, *args, **kwargs): + from webapp.utils import display_date + super(EventForm, self).__init__(*args, **kwargs) + + # Set the form fields based on the model object + if kwargs.has_key('instance'): + instance = kwargs['instance'] + sdate = display_date(instance) + if sdate != "0000-00-00": + self.initial['date'] = sdate + try: + self.data['date'] = self.initial['date'] + except: + pass + + def clean(self): + from webapp.utils import dp + data = super(EventForm, self).clean() + dobj = dp(data.get('date')) + if not dobj.is_valid(): + msg = u"Invalid date format" + self._errors["date"] = self.error_class([msg]) + del data["date"] + else: + data["date"] = str(dobj) + return data + + def save(self, commit=True): + from webapp.utils import dp + from webapp.libdjango import DjangoInterface + dji = DjangoInterface() + model = super(EventForm, self).save(commit=False) + dobj = dp(self.cleaned_data['date']) + dji.add_date(model, dobj.serialize()) + if commit: + model.save() + return model + + date = forms.CharField(label="Date", + required=False, + widget=TextInput(attrs={'size':'30'})) diff --git a/src/webapp/grampsdb/models.py b/src/webapp/grampsdb/models.py index 6bf53241c..60657c110 100644 --- a/src/webapp/grampsdb/models.py +++ b/src/webapp/grampsdb/models.py @@ -517,7 +517,7 @@ class Source(PrimaryObject): class Event(DateObject, PrimaryObject): event_type = models.ForeignKey('EventType') description = models.CharField('description', max_length=50, blank=True) - place = models.ForeignKey('Place', null=True) + place = models.ForeignKey('Place', null=True, blank=True) references = generic.GenericRelation('EventRef', related_name="refs", content_type_field="object_type", object_id_field="object_id") diff --git a/src/webapp/grampsdb/view/event.py b/src/webapp/grampsdb/view/event.py index e744b0e37..bd0c1e101 100644 --- a/src/webapp/grampsdb/view/event.py +++ b/src/webapp/grampsdb/view/event.py @@ -22,10 +22,11 @@ """ Views for Person, Name, and Surname """ ## Gramps Modules -from webapp.utils import _, boolean +from webapp.utils import _, boolean, update_last_changed from webapp.grampsdb.models import Event from webapp.grampsdb.forms import * from webapp.libdjango import DjangoInterface +import DateHandler ## Django Modules from django.shortcuts import get_object_or_404, render_to_response, redirect @@ -33,6 +34,8 @@ from django.template import Context, RequestContext ## Globals dji = DjangoInterface() +dd = DateHandler.displayer.display +dp = DateHandler.parser.parse def process_event(request, context, handle, action): # view, edit, save """ @@ -41,89 +44,54 @@ def process_event(request, context, handle, action): # view, edit, save context["tview"] = _("Event") context["tviews"] = _("Events") context["action"] = "view" - context["event"] = Event() - context["object"] = Event() view_template = "view_event_detail.html" + + if handle == "add": + action = "add" + if request.POST.has_key("action"): + action = request.POST.get("action") + + # Handle: edit, view, add, create, save, delete + if action == "add": + event = Event() + eventform = EventForm(instance=event) + eventform.model = event + elif action in ["view", "edit"]: + event = Event.objects.get(handle=handle) + eventform = EventForm(instance=event) + eventform.model = event + elif action == "save": + event = Event.objects.get(handle=handle) + eventform = EventForm(request.POST, instance=event) + eventform.model = event + if eventform.is_valid(): + update_last_changed(event, request.user.username) + event = eventform.save() + dji.rebuild_cache(event) + action = "view" + else: + action = "edit" + elif action == "create": + event = Event(handle=create_id()) + eventform = EventForm(request.POST, instance=event) + eventform.model = event + if eventform.is_valid(): + update_last_changed(event, request.user.username) + event = eventform.save() + dji.rebuild_cache(event) + action = "view" + else: + action = "add" + elif action == "delete": + event = Event.objects.get(handle=handle) + event.delete() + return redirect("/event/") + else: + raise Exception("Unhandled action: '%s'" % action) + + context["eventform"] = eventform + context["object"] = event + context["event"] = event + context["action"] = action return render_to_response(view_template, context) - if request.user.is_authenticated(): - if action in ["edit", "view"]: - pf, nf, sf, person = get_person_forms(handle, empty=False) - elif action == "add": - pf, nf, sf, person = get_person_forms(handle=None, protect=False, empty=True) - elif action == "delete": - pf, nf, sf, person = get_person_forms(handle, protect=False, empty=True) - person.delete() - return redirect("/person/") - elif action in ["save", "create"]: # could be create a new person - # look up old data, if any: - if handle: - person = Person.objects.get(handle=handle) - name = person.name_set.get(preferred=True) - surname = name.surname_set.get(primary=True) - else: # create new item - 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 - nf = NameFormFromPerson(request.POST, instance=name) - nf.model = name - sf = SurnameForm(request.POST, instance=surname) - # check if valid: - if nf.is_valid() and pf.is_valid() and sf.is_valid(): - # name.preferred and surname.primary get set False in the above is_valid() - person = pf.save() - # Process data: - name.person = person - name = nf.save(commit=False) - # 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? - check_preferred(name, person) - name.save() - # Process data: - surname.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" % person.handle) - else: - # need to edit again - if handle: - action = "edit" - else: - action = "add" - else: # error? - raise Http404(_("Requested %s does not exist.") % "person") - else: # not authenticated - # BEGIN NON-AUTHENTICATED ACCESS - try: - person = Person.objects.get(handle=handle) - except: - raise Http404(_("Requested %s does not exist.") % "person") - if person.private: - raise Http404(_("Requested %s does not exist.") % "person") - pf, nf, sf, person = get_person_forms(handle, protect=True) - # END NON-AUTHENTICATED ACCESS - context["action"] = action - context["view"] = "person" - context["tview"] = _("Person") - context["tviews"] = _("People") - context["personform"] = pf - context["nameform"] = nf - context["surnameform"] = sf - context["person"] = person - context["object"] = person - context["next"] = "/person/%s" % person.handle - diff --git a/src/webapp/grampsdb/view/family.py b/src/webapp/grampsdb/view/family.py index 6cd7c27d0..d43bb899b 100644 --- a/src/webapp/grampsdb/view/family.py +++ b/src/webapp/grampsdb/view/family.py @@ -49,7 +49,8 @@ def process_family(request, context, handle, action): # view, edit, save # Handle: edit, view, add, create, save, delete if action == "add": - family = Family(family_rel_type=FamilyRelType.objects.get( + family = Family( + family_rel_type=FamilyRelType.objects.get( val=FamilyRelType._DEFAULT[0])) familyform = FamilyForm(instance=family) familyform.model = family diff --git a/src/webapp/shell.py b/src/webapp/shell.py index 8542235c6..dbee5f964 100644 --- a/src/webapp/shell.py +++ b/src/webapp/shell.py @@ -12,8 +12,12 @@ from webapp.dbdjango import DbDjango from webapp.reports import import_file from webapp.libdjango import DjangoInterface, totime, todate +import DateHandler + db = DbDjango() dji = DjangoInterface() +dd = DateHandler.displayer.display +dp = DateHandler.parser.parse #def Print(m): # print m diff --git a/src/webapp/utils.py b/src/webapp/utils.py index 2c0d8b9d2..7741a0232 100644 --- a/src/webapp/utils.py +++ b/src/webapp/utils.py @@ -67,7 +67,7 @@ _ = lambda msg: msg util_filters = [ 'nbsp', - 'render_date', + 'date_as_text', 'render_name', ] @@ -97,8 +97,9 @@ util_tags = [ # #------------------------------------------------------------------------ dji = libdjango.DjangoInterface() -_dd = gen.datehandler.displayer.display -_dp = gen.datehandler.parser.parse +dd = DateHandler.displayer.display +dp = DateHandler.parser.parse +db = DbDjango() def register_plugins(): dbstate = DbState.DbState() @@ -122,7 +123,6 @@ def get_person_from_handle(db, handle): def probably_alive(handle): return False - db = DbDjango() person = db.get_person_from_handle(handle) return Utils.probably_alive(person, db) @@ -553,7 +553,7 @@ def children_table(obj, user, action, url=None, *args): child.gender_type, childref.father_rel_type, childref.mother_rel_type, - render_date(child.birth, user), + date_as_text(child.birth, user), ) links.append(('URL', ("/person/%s" % child.handle))) else: @@ -603,7 +603,7 @@ def display_date(obj): if date_tuple: gdate = GDate() gdate.unserialize(date_tuple) - return _dd(gdate) + return dd(gdate) else: return "" @@ -615,13 +615,15 @@ def render(formfield, user, action, test=False, truetext="", id=None): fieldname = formfield.name # 'surname' try: retval = str(getattr(formfield.form.model, fieldname)) + if retval == "True": + retval = "Yes" + elif retval == "False": + retval = "No" except: # name, "prefix" try: - retval = str(formfield.form.data[fieldname]) # formfield._data() + retval = str(formfield.form.data[fieldname]) except: - #import pdb; pdb.set_trace() - #retval = "[ERROR: %s]" % fieldname retval = "[None]" else: retval = truetext @@ -684,7 +686,7 @@ def render_name(name, user): def make_name(name, user): return render_name(name, user) -def render_date(obj, user): +def date_as_text(obj, user): """ Given a Django object, render the date as text and return. This function uses authentication settings. @@ -695,7 +697,7 @@ def render_date(obj, user): date_tuple = dji.get_date(obj) if date_tuple: gdate = GDate().unserialize(date_tuple) - return _dd(gdate) + return dd(gdate) return "" return "[Private]"
{{nameform|render_name:user}}