From d9e0bd63f3f0522b4eaebe7d525048570fe43ef6 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sun, 24 Aug 2008 15:36:28 +0200 Subject: [PATCH] Added a controller and template for exporting data in plain text --- database/group_roles.sql | 3 ++ lib/NDWeb.pm | 1 + lib/NDWeb/Controller/TextExport.pm | 58 ++++++++++++++++++++++++++++++ root/lib/site/wrapper.tt2 | 3 ++ root/src/alliances/edit.tt2 | 1 + root/src/textexport/index.tt2 | 4 +++ t/controller_TextExport.t | 10 ++++++ 7 files changed, 80 insertions(+) create mode 100644 lib/NDWeb/Controller/TextExport.pm create mode 100644 root/src/textexport/index.tt2 create mode 100644 t/controller_TextExport.t diff --git a/database/group_roles.sql b/database/group_roles.sql index add205f..b18c285 100644 --- a/database/group_roles.sql +++ b/database/group_roles.sql @@ -37,6 +37,7 @@ INSERT INTO roles VALUES('intel'); INSERT INTO roles VALUES('intel_members'); INSERT INTO roles VALUES('intel_member'); INSERT INTO roles VALUES('intel_naps'); +INSERT INTO roles VALUES('textexport_alliance'); INSERT INTO group_roles (gid,role) VALUES(2,'member_menu'); INSERT INTO group_roles (gid,role) VALUES(2,'attack_menu'); @@ -82,6 +83,7 @@ INSERT INTO group_roles (gid,role) VALUES(1,'intel'); INSERT INTO group_roles (gid,role) VALUES(1,'intel_members'); INSERT INTO group_roles (gid,role) VALUES(1,'intel_member'); INSERT INTO group_roles (gid,role) VALUES(1,'intel_naps'); +INSERT INTO group_roles (gid,role) VALUES(1,'textexport_alliance'); INSERT INTO group_roles (gid,role) VALUES(3,'dc_menu'); INSERT INTO group_roles (gid,role) VALUES(3,'bc_menu'); @@ -101,3 +103,4 @@ INSERT INTO group_roles (gid,role) VALUES(3,'intel'); INSERT INTO group_roles (gid,role) VALUES(3,'intel_members'); INSERT INTO group_roles (gid,role) VALUES(3,'intel_member'); INSERT INTO group_roles (gid,role) VALUES(3,'intel_naps'); +INSERT INTO group_roles (gid,role) VALUES(3,'textexport_alliance'); diff --git a/lib/NDWeb.pm b/lib/NDWeb.pm index bce6c38..d7aec96 100644 --- a/lib/NDWeb.pm +++ b/lib/NDWeb.pm @@ -99,6 +99,7 @@ __PACKAGE__->allow_access_if('/jsrpc/end',1); __PACKAGE__->deny_access_unless('/forum/allUnread',[qw//]); __PACKAGE__->deny_access_unless('/forum/privmsg',[qw//]); __PACKAGE__->deny_access_unless('/settings',[qw//]); +__PACKAGE__->deny_access_unless('/textexport/alliance',[qw/textexport_alliance/]); =head1 NAME diff --git a/lib/NDWeb/Controller/TextExport.pm b/lib/NDWeb/Controller/TextExport.pm new file mode 100644 index 0000000..37772ee --- /dev/null +++ b/lib/NDWeb/Controller/TextExport.pm @@ -0,0 +1,58 @@ +package NDWeb::Controller::TextExport; + +use strict; +use warnings; +use parent 'Catalyst::Controller'; + +=head1 NAME + +NDWeb::Controller::TextExport - Catalyst Controller + +=head1 DESCRIPTION + +Catalyst Controller. + +=head1 METHODS + +=cut + + +=head2 index + +=cut + +sub auto : Private { + my ( $self, $c ) = @_; + $c->stash(template => 'textexport/index.tt2'); +} + +sub alliance : Local { + my ( $self, $c, $ally ) = @_; + my $dbh = $c->model; + + my $query = $dbh->prepare(q{SELECT coords(x,y,z), size, score, value, COALESCE(nick,'') AS nick + FROM current_planet_stats + WHERE alliance_id = $1 + ORDER BY x,y,z}); + $query->execute($ally); + + $c->stash(titles => $query->{NAME}); + $c->stash(values => $query->fetchall_arrayref); +} + +sub end : ActionClass('RenderView') { + my ( $self, $c ) = @_; + $c->res->content_type('text/plain') if $c->stash->{template} =~ m{^textexport/}; +} + +=head1 AUTHOR + +Michael Andreen (harv@ruin.nu) + +=head1 LICENSE + +GPL 2.0, or later. + +=cut + +1; diff --git a/root/lib/site/wrapper.tt2 b/root/lib/site/wrapper.tt2 index 6da016b..b6431c5 100644 --- a/root/lib/site/wrapper.tt2 +++ b/root/lib/site/wrapper.tt2 @@ -7,6 +7,9 @@ ELSIF template.name.match('graphs/'); ELSIF template.name.match('jsrpc/'); debug("Passing page through xml: $template.name"); content WRAPPER site/xml.tt2; +ELSIF template.name.match('textexport/'); + debug("Passing page through graph: $template.name"); + content; ELSE; debug("Applying HTML page layout wrappers to $template.name\n"); content WRAPPER site/html.tt2 + site/layout.tt2; diff --git a/root/src/alliances/edit.tt2 b/root/src/alliances/edit.tt2 index 3c766d1..878fcfc 100644 --- a/root/src/alliances/edit.tt2 +++ b/root/src/alliances/edit.tt2 @@ -25,6 +25,7 @@ +

Export information as text.

Alliance size and score vs members Avg alliance size and score vs members diff --git a/root/src/textexport/index.tt2 b/root/src/textexport/index.tt2 new file mode 100644 index 0000000..e91fa9e --- /dev/null +++ b/root/src/textexport/index.tt2 @@ -0,0 +1,4 @@ +[% titles.join("\t") %] +[% FOR v IN values %] + [%- v.join("\t") %] +[% END %] diff --git a/t/controller_TextExport.t b/t/controller_TextExport.t new file mode 100644 index 0000000..ee764f8 --- /dev/null +++ b/t/controller_TextExport.t @@ -0,0 +1,10 @@ +use strict; +use warnings; +use Test::More tests => 3; + +BEGIN { use_ok 'Catalyst::Test', 'NDWeb' } +BEGIN { use_ok 'NDWeb::Controller::TextExport' } + +ok( request('/textexport')->is_success, 'Request should succeed' ); + + -- 2.39.2