Skip to content

Commit

Permalink
note merges in the audit log, more redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Duggan committed Jun 3, 2014
1 parent 9b994b1 commit 822c468
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/Tuba/Book.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(@_);
}

Expand Down
56 changes: 51 additions & 5 deletions lib/Tuba/Controller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -266,20 +266,24 @@ 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;
}
return $c->render_not_found unless $identifier;

my $sth = $c->db->dbh->prepare(<<SQL, { pg_placeholder_dollaronly => 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);
Expand Down Expand Up @@ -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(<<SQL, {}, "identifier=>$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;
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Tuba/Dataset.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(@_);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Tuba/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(@_);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Tuba/Image.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(@_);
Expand Down
2 changes: 1 addition & 1 deletion lib/Tuba/Organization.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(@_);
}
Expand Down
32 changes: 31 additions & 1 deletion lib/Tuba/Person.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(<<SQL, {}, "id=>$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;

2 changes: 1 addition & 1 deletion lib/Tuba/Reference.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(@_);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Tuba/Report.pm
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ update_contributors
update_keywords
update_regions
update_rel
update
remove
update
history
]) {
eval <<DONE;
Expand Down
3 changes: 2 additions & 1 deletion lib/Tuba/Webpage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ sub list {

sub show {
my $c = shift;
$c->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(@_);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Tuba/files/templates/reference/update_form.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
%= form_for obj_uri_for($object, 'update') => method => 'POST', class => 'form-horizontal well', enctype => 'multipart/form-data' => begin
%= include 'messages';
<div class="form-actions">
<div class='input-prepend'>
<button type="submit" name='delete' value='delete' class="btn btn-danger" onclick='{return confirm("Are you sure?")}'>Delete</button>
%= text_field 'replacement_identifier', placeholder => 'replacement', style=>'height:auto;';
</div>
</div>
%= end

5 changes: 5 additions & 0 deletions lib/Tuba/files/templates/update_form.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="reset" class="btn">Reset</button>
<div class='input-prepend'>
<button type="submit" name='delete' value='delete' class="btn btn-danger" onclick='{return confirm("Are you sure?")}'>Delete</button>
% if ($self->can_set_replacement) {
%= text_field 'replacement_identifier', placeholder => 'replacement', style=>'height:auto;';
% }
</div>
</div>

%= end
Expand Down

0 comments on commit 822c468

Please sign in to comment.