From 38b50049f2185236858a076e008232638e78a7d3 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Wed, 30 May 2012 00:49:43 +0000 Subject: [PATCH] Working on editing references; fixed an error in ordering of references svn: r19704 --- src/data/templates/reference.html | 35 +++++++++++++++++++++++++++++++ src/webapp/grampsdb/forms.py | 4 ++++ src/webapp/grampsdb/models.py | 7 +++++++ src/webapp/grampsdb/view/event.py | 6 ++++++ src/webapp/grampsdb/views.py | 23 ++++++++++++++++++++ src/webapp/libdjango.py | 25 ++++++++++++++-------- src/webapp/shell.py | 12 +++++------ src/webapp/urls.py | 14 +++++++++---- src/webapp/utils.py | 17 ++++++++------- 9 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 src/data/templates/reference.html diff --git a/src/data/templates/reference.html b/src/data/templates/reference.html new file mode 100644 index 000000000..f505020c0 --- /dev/null +++ b/src/data/templates/reference.html @@ -0,0 +1,35 @@ +{% extends "view_page_detail.html" %} +{% load my_tags %} + +{% block content %} + +
+ +{% include "detail_breadcrumb.html" %} +

Reference Detail

+
+ + + {% if form.errors %} +
+

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

+
{{form.errors}}
+
+ {% endif %} +
{% csrf_token %} + +{% for field in form %} + +
+{{field}} + + +{% endfor %} + +
+
+
+ + +{% endblock %} + diff --git a/src/webapp/grampsdb/forms.py b/src/webapp/grampsdb/forms.py index e36b647c4..c2f18aba0 100644 --- a/src/webapp/grampsdb/forms.py +++ b/src/webapp/grampsdb/forms.py @@ -272,3 +272,7 @@ class TagForm(forms.ModelForm): required=False, widget=TextInput(attrs={'size':'70'})) +class EventRefForm(forms.ModelForm): + class Meta: + model = EventRef + diff --git a/src/webapp/grampsdb/models.py b/src/webapp/grampsdb/models.py index dbbd8eeb8..131f12eda 100644 --- a/src/webapp/grampsdb/models.py +++ b/src/webapp/grampsdb/models.py @@ -818,6 +818,13 @@ class EventRef(BaseRef): def __unicode__(self): return "EventRef to " + str(self.ref_object) + def get_url(self): + # /person/3536453463/reference/event/2 + ref_by = self.object_type.model_class().objects.get(id=self.object_id) + ref_to = self.ref_object.__class__.__name__.lower() + return "/%s/%s/reference/%s/%s" % (ref_by.__class__.__name__.lower(), + ref_by.handle, ref_to, self.order) + class RepositoryRef(BaseRef): ref_object = models.ForeignKey('Repository') source_media_type = models.ForeignKey('SourceMediaType') diff --git a/src/webapp/grampsdb/view/event.py b/src/webapp/grampsdb/view/event.py index 32d9f9e4c..7583662e6 100644 --- a/src/webapp/grampsdb/view/event.py +++ b/src/webapp/grampsdb/view/event.py @@ -26,6 +26,7 @@ from webapp.utils import _, boolean, update_last_changed from webapp.grampsdb.models import Event from webapp.grampsdb.forms import * from webapp.libdjango import DjangoInterface +from webapp.dbdjango import DbDjango from gen.datehandler import displayer, parser ## Django Modules @@ -34,6 +35,7 @@ from django.template import Context, RequestContext ## Globals dji = DjangoInterface() +db = DbDjango() dd = displayer.display dp = parser.parse @@ -58,6 +60,10 @@ def process_event(request, context, handle, action, add_to=None): # view, edit, eventform.model = event elif action in ["view", "edit"]: event = Event.objects.get(handle=handle) + genlibevent = db.get_event_from_handle(handle) + if genlibevent: + date = genlibevent.get_date_object() + event.text = dd(date) eventform = EventForm(instance=event) eventform.model = event elif action == "save": diff --git a/src/webapp/grampsdb/views.py b/src/webapp/grampsdb/views.py index 00c90cc8c..baeed0288 100644 --- a/src/webapp/grampsdb/views.py +++ b/src/webapp/grampsdb/views.py @@ -46,6 +46,7 @@ from django.http import Http404, HttpResponseRedirect, HttpResponse from django.shortcuts import get_object_or_404, render_to_response, redirect from django.template import Context, RequestContext from django.db.models import Q +from django.forms.models import modelformset_factory #------------------------------------------------------------------------ # @@ -698,3 +699,25 @@ def build_person_query(search, protect): if protect: query &= (Q(private=False) & Q(person__private=False)) return query + + + +def process_reference(request, ref_by, handle, ref_to, order): + context = RequestContext(request) + ref_by_class = dji.get_model(ref_by) + referenced_by = ref_by_class.objects.get(handle=handle) + object_type = ContentType.objects.get_for_model(referenced_by) + ref_to_class = dji.get_model("%sRef" % ref_to.title()) + referenced_to = ref_to_class.objects.filter(object_id=referenced_by.id, + object_type=object_type, + order=order) + form = modelformset_factory(ref_to_class, extra=0)(queryset=referenced_to) + form.model = referenced_to[0] + context["form"] = form + context["view"] = 'Reference' + context["tview"] = _('Reference') + context["tviews"] = _('References') + context["object"] = referenced_by + context["handle"] = referenced_by.handle + context["action"] = "view" + return render_to_response("reference.html", context) diff --git a/src/webapp/libdjango.py b/src/webapp/libdjango.py index 55568b2df..e184a4190 100644 --- a/src/webapp/libdjango.py +++ b/src/webapp/libdjango.py @@ -161,10 +161,12 @@ class DjangoInterface(object): return "%s%04d" % (prefix, count) def get_model(self, name): - if hasattr(models, name.title()): + if hasattr(models, name): + return getattr(models, name) + elif hasattr(models, name.title()): return getattr(models, name.title()) else: - raise AttributeError("no such model: '%s'" % name.title()) + raise AttributeError("no such model: '%s'" % name) # ----------------------------------------------- # Get methods to retrieve list data from the tables @@ -827,7 +829,8 @@ class DjangoInterface(object): handle) return - count = models.CitationRef.objects.filter(object_id=obj.id,object_type=obj).count() + object_type = ContentType.objects.get_for_model(obj) + count = models.CitationRef.objects.filter(object_id=obj.id,object_type=object_type).count() citation_ref = models.CitationRef(private=False, referenced_by=obj, citation=citation, @@ -876,7 +879,8 @@ class DjangoInterface(object): print >> sys.stderr, ("ERROR: Person does not exist: '%s'" % ref) return - count = models.ChildRef.objects.filter(object_id=obj.id,object_type=obj).count() + object_type = ContentType.objects.get_for_model(obj) + count = models.ChildRef.objects.filter(object_id=obj.id,object_type=object_type).count() child_ref = models.ChildRef(private=private, referenced_by=obj, ref_object=child, @@ -888,7 +892,8 @@ class DjangoInterface(object): self.add_note_list(child_ref, note_list) def add_event_ref_default(self, obj, event, private=False, role=models.EventRoleType._DEFAULT): - count = models.EventRef.objects.filter(object_id=obj.id,object_type=obj).count() + object_type = ContentType.objects.get_for_model(obj) + count = models.EventRef.objects.filter(object_id=obj.id,object_type=object_type).count() event_ref = models.EventRef(private=private, referenced_by=obj, ref_object=event, @@ -904,7 +909,8 @@ class DjangoInterface(object): print >> sys.stderr, ("ERROR: Event does not exist: '%s'" % ref) return - count = models.EventRef.objects.filter(object_id=obj.id,object_type=obj).count() + object_type = ContentType.objects.get_for_model(obj) + count = models.EventRef.objects.filter(object_id=obj.id,object_type=object_type).count() event_ref = models.EventRef(private=private, referenced_by=obj, ref_object=event, @@ -926,7 +932,8 @@ class DjangoInterface(object): print >> sys.stderr, ("ERROR: Repository does not exist: '%s'" % ref) return - count = models.RepositoryRef.objects.filter(object_id=obj.id,object_type=obj).count() + object_type = ContentType.objects.get_for_model(obj) + count = models.RepositoryRef.objects.filter(object_id=obj.id,object_type=object_type).count() repos_ref = models.RepositoryRef(private=private, referenced_by=obj, call_number=call_number, @@ -1227,7 +1234,7 @@ class DjangoInterface(object): events = models.EventRef.objects.filter( object_id=person.id, object_type=obj_type, - ref_object__event_type__val=models.EventType.BIRTH) + ref_object__event_type__val=models.EventType.BIRTH).order_by("order") all_events = self.get_event_ref_list(person) if events: @@ -1237,7 +1244,7 @@ class DjangoInterface(object): events = models.EventRef.objects.filter( object_id=person.id, object_type=obj_type, - ref_object__event_type__val=models.EventType.DEATH) + ref_object__event_type__val=models.EventType.DEATH).order_by("order") if events: person.death = events[0].ref_object person.death_ref_index = lookup_role_index(models.EventType.DEATH, all_events) diff --git a/src/webapp/shell.py b/src/webapp/shell.py index ad9e2354d..588974ac7 100644 --- a/src/webapp/shell.py +++ b/src/webapp/shell.py @@ -18,6 +18,7 @@ from webapp.dbdjango import DbDjango from webapp.reports import import_file from webapp.libdjango import DjangoInterface, totime, todate from gen.datehandler import displayer, parser +from webapp.utils import StyledNoteFormatter, parse_styled_text import gen.lib import cli.user @@ -30,16 +31,15 @@ dp = parser.parse # "/home/dblank/gramps/trunk/example/gramps/data.gramps", # cli.user.User()) -from webapp.utils import StyledNoteFormatter, parse_styled_text -snf = StyledNoteFormatter(db) +#snf = StyledNoteFormatter(db) #for n in Note.objects.all(): # note = db.get_note_from_handle(n.handle) # print snf.format(note) -note = Note.objects.get(handle="aef30789d3d2090abe2") -genlibnote = db.get_note_from_handle(note.handle) -html_text = snf.format(genlibnote) -# FIXME: this looks wrong: +#note = Note.objects.get(handle="aef30789d3d2090abe2") +#genlibnote = db.get_note_from_handle(note.handle) +#html_text = snf.format(genlibnote) +## FIXME: this looks wrong: #print html_text #print parse_styled_text(html_text) diff --git a/src/webapp/urls.py b/src/webapp/urls.py index f382bd7f6..dd740d0ac 100644 --- a/src/webapp/urls.py +++ b/src/webapp/urls.py @@ -77,10 +77,16 @@ urlpatterns += patterns('', {"action": "view"}), # /view/handle/ (r'^(?P(\w+))/(?P(\w+))/(?P(\w+))$', action), # /view/handle/action - (r'^person/(?P(\w+))/name/(?P(\w+))$', process_name), - (r'^person/(?P(\w+))/name/(?P(\w+))/(?P(\w+))$', process_name), - (r'^person/(?P(\w+))/name/(?P(\w+))/surname/(?P(\w+))$', process_surname), - (r'^person/(?P(\w+))/name/(?P(\w+))/surname/(?P(\w+))/(?P(\w+))$', process_surname), + (r'^(?P(\w+))/(?P(\w+))/reference/(?P(\w+))/(?P(\w+))$', + process_reference), # /view/handle/reference/item/order + (r'^person/(?P(\w+))/name/(?P(\w+))$', process_name), + (r'^person/(?P(\w+))/name/(?P(\w+))/(?P(\w+))$', + process_name), + + (r'^person/(?P(\w+))/name/(?P(\w+))/surname/(?P(\w+))$', + process_surname), + (r'^person/(?P(\w+))/name/(?P(\w+))/surname/(?P(\w+))/(?P(\w+))$', + process_surname), ) # In urls: diff --git a/src/webapp/utils.py b/src/webapp/utils.py index 0d2fd4817..224eda695 100644 --- a/src/webapp/utils.py +++ b/src/webapp/utils.py @@ -213,8 +213,6 @@ class Table(object): # We have a couple of HTML bits that we want to unescape: return str(self.doc.doc.htmllist[0]).replace(" ", " ") -_ = lambda text: text - def make_button(text, url, *args): url = url % args #return """[ %s ] """ % (url, text) @@ -223,12 +221,14 @@ def make_button(text, url, *args): def event_table(obj, user, action, url=None, *args): retval = "" table = Table() - table.columns(_("Description"), - _("Type"), - _("ID"), - _("Date"), - _("Place"), - _("Role")) + table.columns( + _("Event Reference"), + _("Description"), + _("Type"), + _("ID"), + _("Date"), + _("Place"), + _("Role")) if user.is_authenticated(): obj_type = ContentType.objects.get_for_model(obj) event_ref_list = models.EventRef.objects.filter( @@ -237,6 +237,7 @@ def event_table(obj, user, action, url=None, *args): event_list = [(obj.ref_object, obj) for obj in event_ref_list] for (djevent, event_ref) in event_list: table.row( + event_ref, djevent.description or str(djevent), table.db.get_event_from_handle(djevent.handle), djevent.gramps_id,