From 822c468361ec9f3473ee298541fd4cf4acf01a76 Mon Sep 17 00:00:00 2001 From: Brian Duggan Date: Tue, 3 Jun 2014 09:50:05 -0600 Subject: [PATCH] note merges in the audit log, more redirects --- lib/Tuba/Book.pm | 3 +- lib/Tuba/Controller.pm | 56 +++++++++++++++++-- lib/Tuba/Dataset.pm | 2 +- lib/Tuba/File.pm | 2 +- lib/Tuba/Image.pm | 2 +- lib/Tuba/Organization.pm | 2 +- lib/Tuba/Person.pm | 32 ++++++++++- lib/Tuba/Reference.pm | 2 +- lib/Tuba/Report.pm | 2 +- lib/Tuba/Webpage.pm | 3 +- .../templates/reference/update_form.html.ep | 3 + lib/Tuba/files/templates/update_form.html.ep | 5 ++ 12 files changed, 100 insertions(+), 14 deletions(-) diff --git a/lib/Tuba/Book.pm b/lib/Tuba/Book.pm index 740ac0cf..4df0e0ab 100644 --- a/lib/Tuba/Book.pm +++ b/lib/Tuba/Book.pm @@ -27,7 +27,8 @@ sub list { sub show { my $c = shift; - $c->stash('object', $c->_this_object); + my $book = $c->_this_object or return $c->render_not_found_or_redirect; + $c->stash('object', $book); $c->SUPER::show(@_); } diff --git a/lib/Tuba/Controller.pm b/lib/Tuba/Controller.pm index f0e8427b..c7fd7f3a 100644 --- a/lib/Tuba/Controller.pm +++ b/lib/Tuba/Controller.pm @@ -254,7 +254,7 @@ multiple changes have been made (/report/oldreport/figure/oldgigure), there will be two redirects. The first one will go to /report/newreport/figure/oldfigure, and the second one will go to /report/newreport/figure/newfigure. -Also adding 'noredirect' as an audit note will prevent redirects. +Also adding 'no redirect' to the audit note will prevent redirects. =cut @@ -266,9 +266,13 @@ sub render_not_found_or_redirect { my @bind; my $table_name = $meta->table; my $identifier; + my $identifier_column; for my $name ($meta->primary_key_column_names) { ; # e.g. identifier, report_identifier my $val = $c->_pk_to_stashval($meta,$name) or next; - $identifier = $val if $name eq 'identifier'; + if ($name =~ /^id(entifier)?$/) { + $identifier = $val; + $identifier_column = $name; + } push @bind, $val; $sql .= " and " if $sql; $sql .= " row_data->'$name' = \$".@bind; @@ -276,10 +280,10 @@ sub render_not_found_or_redirect { return $c->render_not_found unless $identifier; my $sth = $c->db->dbh->prepare(< 1 }); -select changed_fields->'identifier' - from audit.logged_actions where table_name='$table_name' and changed_fields?'identifier' +select changed_fields->'$identifier_column' + from audit.logged_actions where table_name='$table_name' and changed_fields?'$identifier_column' and $sql - and audit_note not like '%noredirect%' + and audit_note not like '%no redirect%' order by transaction_id limit 1 SQL my $got = $sth->execute(@bind); @@ -1062,6 +1066,42 @@ sub normalize_form_parameter { return $value; } +=head2 set_replacement + +After deleting an object, indicate that another object takes precedence. + +(Not implemented for composite primary keys.) + +=cut + +sub set_replacement { + my $c = shift; + my $table_name = shift; + my $old_identifier = shift; + my $new_identifier = shift; + my $dbh = $c->dbs->dbh; + $dbh->do(<$new_identifier", $old_identifier) and return 1; + update audit.logged_actions set changed_fields = ?::hstore + where action='D' and table_name='$table_name' and row_data->'identifier' = ? +SQL + $c->stash(error => $dbh->errstr); + return 0; +} + +=head2 can_set_replacement + +See above. + +=cut + +sub can_set_replacement { + my $c = shift; + my $meta = $c->_guess_object_class->meta; + my @cols = $meta->primary_key_column_names; + return 0 if @cols > 1; + return 1; +} + sub update { my $c = shift; my $object = $c->_this_object or return $c->render_not_found; @@ -1083,7 +1123,13 @@ sub update { } if ($c->param('delete')) { + my $table_name = $object->meta->table; if ($object->delete) { + my $identifier = $object->pk_values; + my $new = $c->param('replacement_identifier'); + if ($identifier && $new) { + $c->set_replacement($table_name, $identifier => $new); + } $c->flash(message => "Deleted $table"); return $c->redirect_to('list_'.$table); } diff --git a/lib/Tuba/Dataset.pm b/lib/Tuba/Dataset.pm index d837de28..62b8f45d 100644 --- a/lib/Tuba/Dataset.pm +++ b/lib/Tuba/Dataset.pm @@ -16,7 +16,7 @@ sub show { $identifier =~ s/\s+$//; my $object = Dataset->new( identifier => $identifier )->load(speculative => 1) - or return $c->render_not_found; + or return $c->render_not_found_or_redirect; $c->stash(object => $object); $c->SUPER::show(@_); } diff --git a/lib/Tuba/File.pm b/lib/Tuba/File.pm index 366bb203..2b0bb55e 100644 --- a/lib/Tuba/File.pm +++ b/lib/Tuba/File.pm @@ -21,7 +21,7 @@ sub show { my $c = shift; my $identifier = $c->stash('file_identifier'); my $meta = File->meta; - my $object = File->new(identifier => $identifier)->load(speculative => 1 ) or return $c->render_not_found; + my $object = File->new(identifier => $identifier)->load(speculative => 1 ) or return $c->render_not_found_or_redirect; $c->stash(object => $object); $c->SUPER::show(@_); } diff --git a/lib/Tuba/Image.pm b/lib/Tuba/Image.pm index 7611cd7e..9cb2f1ad 100644 --- a/lib/Tuba/Image.pm +++ b/lib/Tuba/Image.pm @@ -28,7 +28,7 @@ sub show { my $meta = Image->meta; my $object = Image->new( identifier => $identifier ) ->load( speculative => 1, with => [qw/figures/] ) - or return $c->render_not_found; + or return $c->render_not_found_or_redirect; $c->stash(object => $object); $c->stash(meta => $meta); $c->SUPER::show(@_); diff --git a/lib/Tuba/Organization.pm b/lib/Tuba/Organization.pm index 4676b442..8f85ea6f 100644 --- a/lib/Tuba/Organization.pm +++ b/lib/Tuba/Organization.pm @@ -14,7 +14,7 @@ sub show { my $meta = Organization->meta; my $identifier = $c->stash('organization_identifier'); my $object = Organization->new( identifier => $identifier )->load( speculative => 1 ) - or return $c->render_not_found; + or return $c->render_not_found_or_redirect; $c->stash(object => $object); return $c->SUPER::show(@_); } diff --git a/lib/Tuba/Person.pm b/lib/Tuba/Person.pm index ae0b3fb6..4c8cead7 100644 --- a/lib/Tuba/Person.pm +++ b/lib/Tuba/Person.pm @@ -26,7 +26,7 @@ sub show { my $person = Person->new( id => $identifier ) ->load( speculative => 1, with => [qw/contributors/] ) - or return $c->render_not_found; + or return $c->render_not_found_or_redirect; $c->stash(object => $person); $c->stash(meta => Person->meta); @@ -143,5 +143,35 @@ sub update_rel { $c->redirect_to($next); } +=head2 set_replacement + +Override to use id instead of identifier. + +=cut + +sub set_replacement { + my $c = shift; + my $table_name = shift; + my $old_identifier = shift; + my $new_identifier = shift; + my $dbh = $c->dbs->dbh; + $dbh->do(<$new_identifier", $old_identifier) and return 1; + update audit.logged_actions set changed_fields = ?::hstore + where action='D' and table_name='$table_name' and row_data->'id' = ? +SQL + $c->stash(error => $dbh->errstr); + return 0; +} + +sub _pk_to_stashval { + # Map a primary key column name to a value in the stash + my $c = shift; + my $meta = shift; + my $name = shift; + my $stash_name = $name; + $stash_name = "person_identifier" if $name eq 'id'; + return $c->stash($stash_name); +} + 1; diff --git a/lib/Tuba/Reference.pm b/lib/Tuba/Reference.pm index d526c314..9f84578e 100644 --- a/lib/Tuba/Reference.pm +++ b/lib/Tuba/Reference.pm @@ -48,7 +48,7 @@ sub show { my $c = shift; my $identifier = $c->stash('reference_identifier'); my $reference = Reference->new(identifier => $identifier); - $reference->load(speculative => 1) or return $c->render_not_found; + $reference->load(speculative => 1) or return $c->render_not_found_or_redirect; $c->stash( object => $reference); $c->SUPER::show(@_); } diff --git a/lib/Tuba/Report.pm b/lib/Tuba/Report.pm index 5a64d93c..a25d9358 100644 --- a/lib/Tuba/Report.pm +++ b/lib/Tuba/Report.pm @@ -251,8 +251,8 @@ update_contributors update_keywords update_regions update_rel -update remove +update history ]) { eval <stash('object', $c->_this_object); + my $webpage = $c->_this_object or return $c->render_not_found_or_redirect; + $c->stash(object => $webpage); $c->SUPER::show(@_); } diff --git a/lib/Tuba/files/templates/reference/update_form.html.ep b/lib/Tuba/files/templates/reference/update_form.html.ep index 351e4c4e..7614f693 100644 --- a/lib/Tuba/files/templates/reference/update_form.html.ep +++ b/lib/Tuba/files/templates/reference/update_form.html.ep @@ -28,7 +28,10 @@ %= form_for obj_uri_for($object, 'update') => method => 'POST', class => 'form-horizontal well', enctype => 'multipart/form-data' => begin %= include 'messages';
+
+ %= text_field 'replacement_identifier', placeholder => 'replacement', style=>'height:auto;'; +
%= end diff --git a/lib/Tuba/files/templates/update_form.html.ep b/lib/Tuba/files/templates/update_form.html.ep index 8d4696fe..475a60c3 100644 --- a/lib/Tuba/files/templates/update_form.html.ep +++ b/lib/Tuba/files/templates/update_form.html.ep @@ -57,7 +57,12 @@
+
+ % if ($self->can_set_replacement) { + %= text_field 'replacement_identifier', placeholder => 'replacement', style=>'height:auto;'; + % } +
%= end