From ae13dd3e671c789aed7037f6fd0d4a774308a02e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 3 Oct 2013 17:23:41 +0200 Subject: [PATCH] Use the REST API in the web interface for editing projects --- src/lib/Hydra/Controller/Project.pm | 67 +++++++++++------------------ src/root/edit-project.tt | 28 +++++++----- src/root/project.tt | 18 +++++++- src/root/static/js/common.js | 26 +++++++++++ 4 files changed, 84 insertions(+), 55 deletions(-) diff --git a/src/lib/Hydra/Controller/Project.pm b/src/lib/Hydra/Controller/Project.pm index 1d0c7753..78c42db0 100644 --- a/src/lib/Hydra/Controller/Project.pm +++ b/src/lib/Hydra/Controller/Project.pm @@ -1,5 +1,6 @@ package Hydra::Controller::Project; +use utf8; use strict; use warnings; use base 'Hydra::Base::Controller::ListBuilds'; @@ -51,15 +52,13 @@ sub project_PUT { if (defined $c->stash->{project}) { requireProjectOwner($c, $c->stash->{project}); + txn_do($c->model('DB')->schema, sub { updateProject($c, $c->stash->{project}); }); - if ($c->req->looks_like_browser) { - $c->res->redirect($c->uri_for($self->action_for("project"), [$c->stash->{project}->name]) . "#tabs-configuration"); - } else { - $self->status_no_content($c); - } + my $uri = $c->uri_for($self->action_for("project"), [$c->stash->{project}->name]) . "#tabs-configuration"; + $self->status_ok($c, entity => { redirect => "$uri" }); } else { @@ -72,23 +71,32 @@ sub project_PUT { # valid. Idem for the owner. my $owner = $c->user->username; $project = $c->model('DB::Projects')->create( - { name => ".tmp.$$." . int(rand(100000)), displayname => "", owner => $owner }); + { name => ".tmp", displayname => "", owner => $owner }); updateProject($c, $project); }); my $uri = $c->uri_for($self->action_for("project"), [$project->name]); - 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" } - ); - } + $self->status_created($c, + location => "$uri", + entity => { name => $project->name, uri => "$uri", redirect => "$uri", type => "project" }); } } +sub project_DELETE { + my ($self, $c) = @_; + + requireProjectOwner($c, $c->stash->{project}); + + txn_do($c->model('DB')->schema, sub { + $c->stash->{project}->jobsetevals->delete_all; + $c->stash->{project}->builds->delete_all; + $c->stash->{project}->delete; + }); + + my $uri = $c->res->redirect($c->uri_for("/")); + $self->status_ok($c, entity => { redirect => "$uri" }); +} + sub edit : Chained('projectChain') PathPart Args(0) { my ($self, $c) = @_; @@ -100,25 +108,6 @@ sub edit : Chained('projectChain') PathPart Args(0) { } -sub submit : Chained('projectChain') PathPart Args(0) { - my ($self, $c) = @_; - - requirePost($c); - requireProjectOwner($c, $c->stash->{project}); - - if (($c->request->params->{submit} // "") eq "delete") { - txn_do($c->model('DB')->schema, sub { - $c->stash->{project}->jobsetevals->delete_all; - $c->stash->{project}->builds->delete_all; - $c->stash->{project}->delete; - }); - return $c->res->redirect($c->uri_for("/")); - } - - project_PUT($self, $c); -} - - sub requireMayCreateProjects { my ($c) = @_; @@ -140,12 +129,6 @@ sub create : Path('/create-project') { } -sub create_submit : Path('/create-project/submit') { - my ($self, $c) = @_; - project_PUT($self, $c); -} - - sub create_jobset : Chained('projectChain') PathPart('create-jobset') Args(0) { my ($self, $c) = @_; @@ -178,9 +161,9 @@ sub updateProject { } my $projectName = $c->stash->{params}->{name}; - error($c, "Invalid project name ‘$projectName’.") if $projectName !~ /^$projectNameRE$/; + error($c, "Invalid project identifier ‘$projectName’.") if $projectName !~ /^$projectNameRE$/; - error($c, "Cannot rename project to ‘$projectName’ since that name is already taken.") + error($c, "Cannot rename project to ‘$projectName’ since that identifier is already taken.") if $projectName ne $project->name && defined $c->model('DB::Projects')->find($projectName); my $displayName = trim $c->stash->{params}->{displayname}; diff --git a/src/root/edit-project.tt b/src/root/edit-project.tt index 44565a22..b5da303e 100644 --- a/src/root/edit-project.tt +++ b/src/root/edit-project.tt @@ -1,7 +1,7 @@ [% WRAPPER layout.tt title=(create ? "New project" : "Editing project $project.name") %] [% PROCESS common.tt %] -
+
@@ -54,21 +54,27 @@
- - [% IF !create %] - - [% END %] +
diff --git a/src/root/project.tt b/src/root/project.tt index db3d0256..9b5721e1 100644 --- a/src/root/project.tt +++ b/src/root/project.tt @@ -9,7 +9,8 @@ @@ -94,7 +95,6 @@
- Edit @@ -186,4 +186,18 @@ + + [% END %] diff --git a/src/root/static/js/common.js b/src/root/static/js/common.js index 69984746..78269b76 100644 --- a/src/root/static/js/common.js +++ b/src/root/static/js/common.js @@ -88,3 +88,29 @@ var makeLazyTab = function(tabName, uri) { } }); } + +var requestJSON = function(args) { + args.dataType = 'json'; + args.error = function(data) { + json = {}; + try { + if (data.responseText) + json = $.parseJSON(data.responseText); + } catch (err) { + } + if (json.error) + bootbox.alert(json.error); + else if (data.responseText) + bootbox.alert("Server error: " + data.responseText); + else + bootbox.alert("Unknown server error!"); + }; + return $.ajax(args); +} + +var redirectJSON = function(args) { + args.success = function(data) { + window.location = data.redirect; + }; + return requestJSON(args); +}
Display name: