Fix and clean up editing project settings

This commit is contained in:
Eelco Dolstra 2013-10-03 14:05:10 +02:00
parent b1f7096935
commit 2166c478ef

View file

@ -1,5 +1,6 @@
package Hydra::Controller::Project; package Hydra::Controller::Project;
use utf8;
use strict; use strict;
use warnings; use warnings;
use base 'Hydra::Base::Controller::ListBuilds'; use base 'Hydra::Base::Controller::ListBuilds';
@ -9,8 +10,9 @@ use Hydra::Helper::CatalystUtils;
sub projectChain :Chained('/') :PathPart('project') :CaptureArgs(1) { sub projectChain :Chained('/') :PathPart('project') :CaptureArgs(1) {
my ($self, $c, $projectName) = @_; my ($self, $c, $projectName) = @_;
$c->stash->{params}->{name} //= $projectName;
my $project = $c->model('DB::Projects')->find($projectName, { columns => [ $c->stash->{project} = $c->model('DB::Projects')->find($projectName, { columns => [
"me.name", "me.name",
"me.displayname", "me.displayname",
"me.description", "me.description",
@ -19,25 +21,15 @@ sub projectChain :Chained('/') :PathPart('project') :CaptureArgs(1) {
"me.homepage", "me.homepage",
"owner.username", "owner.username",
"owner.fullname", "owner.fullname",
"views.name",
"releases.name", "releases.name",
"releases.timestamp", "releases.timestamp",
"jobsets.name", "jobsets.name",
"jobsets.enabled", "jobsets.enabled",
], join => [ 'owner', 'views', 'releases', 'jobsets' ], order_by => { -desc => "releases.timestamp" }, collapse => 1 }); ], join => [ 'owner', 'releases', 'jobsets' ], order_by => { -desc => "releases.timestamp" }, collapse => 1 });
if ($project) { if (!$c->stash->{project} && !($c->action->name eq "project" and $c->request->method eq "PUT")) {
$c->stash->{project} = $project; $self->status_not_found($c, message => "Project $projectName doesn't exist.");
} else { $c->detach;
if ($c->action->name eq "project" and $c->request->method eq "PUT") {
$c->stash->{projectName} = $projectName;
} else {
$self->status_not_found(
$c,
message => "Project $projectName doesn't exist."
);
$c->detach;
}
} }
} }
@ -54,17 +46,13 @@ sub project_GET {
$c->stash->{releases} = [$c->stash->{project}->releases->search({}, $c->stash->{releases} = [$c->stash->{project}->releases->search({},
{order_by => ["timestamp DESC"]})]; {order_by => ["timestamp DESC"]})];
$self->status_ok( $self->status_ok($c, entity => $c->stash->{project});
$c,
entity => $c->stash->{project}
);
} }
sub project_PUT { sub project_PUT {
my ($self, $c) = @_; my ($self, $c) = @_;
if (defined $c->stash->{project}) { if (defined $c->stash->{project}) {
error($c, "Cannot rename project `$c->stash->{params}->{oldName}' over existing project `$c->stash->{project}->name") if defined $c->stash->{params}->{oldName};
requireProjectOwner($c, $c->stash->{project}); requireProjectOwner($c, $c->stash->{project});
txn_do($c->model('DB')->schema, sub { txn_do($c->model('DB')->schema, sub {
updateProject($c, $c->stash->{project}); updateProject($c, $c->stash->{project});
@ -75,34 +63,10 @@ sub project_PUT {
} else { } else {
$self->status_no_content($c); $self->status_no_content($c);
} }
} elsif (defined $c->stash->{params}->{oldName}) { }
my $project = $c->model('DB::Projects')->find($c->stash->{params}->{oldName});
if (defined $project) {
requireProjectOwner($c, $project);
txn_do($c->model('DB')->schema, sub {
updateProject($c, $project);
});
my $uri = $c->uri_for($self->action_for("project"), [$project->name]); else {
if ($c->req->looks_like_browser) {
$c->res->redirect($uri . "#tabs-configuration");
} else {
$self->status_created(
$c,
location => "$uri",
entity => { name => $project->name, uri => "$uri", type => "project" }
);
}
} else {
$self->status_not_found(
$c,
message => "Project $c->stash->{params}->{oldName} doesn't exist."
);
}
} else {
requireMayCreateProjects($c); requireMayCreateProjects($c);
error($c, "Invalid project name: $c->stash->{projectName}") if $c->stash->{projectName} !~ /^$projectNameRE$/;
my $project; my $project;
txn_do($c->model('DB')->schema, sub { txn_do($c->model('DB')->schema, sub {
@ -111,7 +75,7 @@ sub project_PUT {
# valid. Idem for the owner. # valid. Idem for the owner.
my $owner = $c->user->username; my $owner = $c->user->username;
$project = $c->model('DB::Projects')->create( $project = $c->model('DB::Projects')->create(
{name => $c->stash->{projectName}, displayname => "", owner => $owner}); { name => ".tmp.$$." . int(rand(100000)), displayname => "", owner => $owner });
updateProject($c, $project); updateProject($c, $project);
}); });
@ -154,13 +118,6 @@ sub submit : Chained('projectChain') PathPart Args(0) {
return $c->res->redirect($c->uri_for("/")); return $c->res->redirect($c->uri_for("/"));
} }
my $newName = trim $c->stash->{params}->{name};
my $oldName = trim $c->stash->{project}->name;
unless ($oldName eq $newName) {
$c->stash->{params}->{oldName} = $oldName;
$c->stash->{projectName} = $newName;
undef $c->stash->{project};
}
project_PUT($self, $c); project_PUT($self, $c);
} }
@ -188,9 +145,6 @@ sub create : Path('/create-project') {
sub create_submit : Path('/create-project/submit') { sub create_submit : Path('/create-project/submit') {
my ($self, $c) = @_; my ($self, $c) = @_;
$c->stash->{projectName} = trim $c->stash->{params}->{name};
project_PUT($self, $c); project_PUT($self, $c);
} }
@ -222,15 +176,18 @@ sub updateProject {
my $owner = $project->owner; my $owner = $project->owner;
if ($c->check_user_roles('admin') and defined $c->stash->{params}->{owner}) { if ($c->check_user_roles('admin') and defined $c->stash->{params}->{owner}) {
$owner = trim $c->stash->{params}->{owner}; $owner = trim $c->stash->{params}->{owner};
error($c, "Invalid owner: $owner") error($c, "The user name $owner does not exist.")
unless defined $c->model('DB::Users')->find({username => $owner}); unless defined $c->model('DB::Users')->find($owner);
} }
my $projectName = $c->stash->{projectName} or $project->name; my $projectName = $c->stash->{params}->{name};
error($c, "Invalid project name: $projectName") if $projectName !~ /^$projectNameRE$/; error($c, "Invalid project name $projectName.") if $projectName !~ /^$projectNameRE$/;
error($c, "Cannot rename project to $projectName since that name is already taken.")
if $projectName ne $project->name && defined $c->model('DB::Projects')->find($projectName);
my $displayName = trim $c->stash->{params}->{displayname}; my $displayName = trim $c->stash->{params}->{displayname};
error($c, "Invalid display name: $displayName") if $displayName eq ""; error($c, "You must specify a display name.") if $displayName eq "";
$project->update( $project->update(
{ name => $projectName { name => $projectName