aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdmund von der Burg <evdb@mysociety.org>2011-02-23 22:10:37 +0000
committerEdmund von der Burg <evdb@mysociety.org>2011-02-23 22:10:37 +0000
commit3ef36c3c9b5393c78c0af59b9f4e3f4528472357 (patch)
tree74e1118ae16bdd0e7cbb9f206248883c59de51e0
parentb6ef8d10c3ec7164c65d81b75ecf2662952aeb6d (diff)
More work on the Cobrand and setting for request
test to see welsh about us page
-rw-r--r--Makefile.PL1
-rw-r--r--perllib/FixMyStreet.pm2
-rw-r--r--perllib/FixMyStreet/App.pm78
-rw-r--r--perllib/FixMyStreet/App/Controller/Root.pm18
-rw-r--r--perllib/FixMyStreet/App/View/Web.pm7
-rw-r--r--perllib/FixMyStreet/Cobrand.pm53
-rw-r--r--perllib/FixMyStreet/Cobrand/Default.pm67
-rw-r--r--perllib/FixMyStreet/Cobrand/EmptyHomes.pm28
-rw-r--r--perllib/FixMyStreet/Cobrand/FiksGataMi.pm4
-rw-r--r--perllib/Problems.pm10
-rw-r--r--t/app/controller/about.t10
-rw-r--r--t/cobrand/loading.t67
-rw-r--r--templates/web/default/footer.html9
-rw-r--r--templates/web/emptyhomes/about/index.html (renamed from templates/web/eha/about/index.html)0
14 files changed, 272 insertions, 82 deletions
diff --git a/Makefile.PL b/Makefile.PL
index 44e35d4a6..2965c04db 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -21,6 +21,7 @@ requires 'Readonly';
test_requires 'Test::More' => '0.88';
test_requires 'Test::WWW::Mechanize::Catalyst';
+test_requires 'Sub::Override';
catalyst;
diff --git a/perllib/FixMyStreet.pm b/perllib/FixMyStreet.pm
index 8d50c86e0..daa9de334 100644
--- a/perllib/FixMyStreet.pm
+++ b/perllib/FixMyStreet.pm
@@ -4,7 +4,7 @@ use strict;
use warnings;
use Path::Class;
-my $ROOT_DIR = file(__FILE__)->parent->parent->absolute;
+my $ROOT_DIR = file(__FILE__)->parent->parent->absolute->resolve;
use Readonly;
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm
index 870453b09..bc9bd7672 100644
--- a/perllib/FixMyStreet/App.pm
+++ b/perllib/FixMyStreet/App.pm
@@ -3,6 +3,10 @@ use Moose;
use namespace::autoclean;
use Catalyst::Runtime 5.80;
+use FixMyStreet;
+use FixMyStreet::Cobrand;
+use Memcached;
+use Problems;
use Catalyst qw/
ConfigLoader
@@ -13,11 +17,6 @@ extends 'Catalyst';
our $VERSION = '0.01';
-BEGIN {
- use mySociety::Config;
- mySociety::Config::set_file( __PACKAGE__->path_to("conf/general") );
-}
-
# Configure the application.
#
# Note that settings in fixmystreet_app.conf (or other external
@@ -28,7 +27,9 @@ BEGIN {
# local deployment.
__PACKAGE__->config(
- %{ mySociety::Config::get_list() },
+
+ # get the config from the core object
+ %{ FixMyStreet->config() },
name => 'FixMyStreet::App',
@@ -37,7 +38,7 @@ __PACKAGE__->config(
# Serve anything in web dir that is not a .cgi script
static => { #
- include_path => [ __PACKAGE__->path_to("web") . "" ],
+ include_path => [ FixMyStreet->path_to("web") . "" ],
ignore_extensions => ['cgi'],
}
);
@@ -55,20 +56,67 @@ FixMyStreet::App - Catalyst based application
=head1 DESCRIPTION
-[enter your description here]
+FixMyStreet.com codebase
-=head1 SEE ALSO
+=head1 METHODS
-L<FixMyStreet::App::Controller::Root>, L<Catalyst>
+=head2 cobrand
+
+ $cobrand = $c->cobrand();
+
+Returns the cobrand object. If not already determined this request finds it and
+caches it to the stash.
+
+=cut
+
+sub cobrand {
+ my $c = shift;
+ return $c->stash->{cobrand} ||= $c->_get_cobrand();
+}
+
+sub _get_cobrand {
+ my $c = shift;
+ my $host = $c->req->uri->host;
+ my $cobrand_class = FixMyStreet::Cobrand->get_class_for_host($host);
+ return $cobrand_class->new( { request => $c->req } );
+}
-=head1 AUTHOR
+=head2 setup_cobrand
-Edmund von der Burg,,,
+ $cobrand = $c->setup_cobrand();
-=head1 LICENSE
+Work out which cobrand we should be using. Set the environment correctly - eg
+template paths
-This library is free software. You can redistribute it and/or modify
-it under the same terms as Perl itself.
+=cut
+
+sub setup_cobrand {
+ my $c = shift;
+ my $cobrand = $c->cobrand;
+
+ # append the cobrand templates to the include path
+ $c->stash->{additional_template_paths} =
+ [ $cobrand->path_to_web_templates . '' ];
+
+ my $host = $c->req->uri->host;
+ my $lang =
+ $host =~ /^en\./ ? 'en-gb'
+ : $host =~ /cy/ ? 'cy'
+ : undef;
+
+ # set the language and the translation file to use
+ $cobrand->set_lang_and_domain( $lang, 1 );
+
+ Problems::set_site_restriction_with_cobrand_object($cobrand);
+
+ Memcached::set_namespace( FixMyStreet->config('BCI_DB_NAME') . ":" );
+
+ return $cobrand;
+}
+
+=head1 SEE ALSO
+
+L<FixMyStreet::App::Controller::Root>, L<Catalyst>
=cut
diff --git a/perllib/FixMyStreet/App/Controller/Root.pm b/perllib/FixMyStreet/App/Controller/Root.pm
index b61650f79..debddef38 100644
--- a/perllib/FixMyStreet/App/Controller/Root.pm
+++ b/perllib/FixMyStreet/App/Controller/Root.pm
@@ -22,23 +22,13 @@ Set up general things for this instance
=cut
-sub auto {
+sub auto : Private {
my ( $self, $c ) = @_;
- return;
-}
-
-=head2 index
-
-The root page (/)
-
-=cut
-
-sub index : Path : Args(0) {
- my ( $self, $c ) = @_;
+ # decide which cobrand this request should use
+ $c->setup_cobrand();
- # Hello World
- $c->response->body( $c->welcome_message );
+ return 1;
}
=head2 default
diff --git a/perllib/FixMyStreet/App/View/Web.pm b/perllib/FixMyStreet/App/View/Web.pm
index dcda54db9..306e4c5a7 100644
--- a/perllib/FixMyStreet/App/View/Web.pm
+++ b/perllib/FixMyStreet/App/View/Web.pm
@@ -4,12 +4,13 @@ use base 'Catalyst::View::TT';
use strict;
use warnings;
-use FixMyStreet::App;
+use mySociety::Locale;
+use FixMyStreet;
__PACKAGE__->config(
TEMPLATE_EXTENSION => '.html',
INCLUDE_PATH => [ #
- FixMyStreet::App->path_to( 'templates', 'web', 'default' ),
+ FixMyStreet->path_to( 'templates', 'web', 'default' ),
],
render_die => 1,
expose_methods => ['loc'],
@@ -37,7 +38,7 @@ FIXME - currently just passes through.
sub loc {
my ( $self, $c, @args ) = @_;
- return join ' ', @args;
+ return _(@args);
}
1;
diff --git a/perllib/FixMyStreet/Cobrand.pm b/perllib/FixMyStreet/Cobrand.pm
index 74414e270..91155db6e 100644
--- a/perllib/FixMyStreet/Cobrand.pm
+++ b/perllib/FixMyStreet/Cobrand.pm
@@ -6,6 +6,7 @@ package FixMyStreet::Cobrand;
use strict;
use warnings;
+use FixMyStreet;
use Carp;
use Module::Pluggable
@@ -13,42 +14,58 @@ use Module::Pluggable
search_path => ['FixMyStreet::Cobrand'],
require => 1;
-=item get_allowed_cobrands
+my @ALL_COBRAND_CLASSES = __PACKAGE__->_cobrands;
+
+=head2 get_allowed_cobrands
Return an array reference of allowed cobrand subdomains
=cut
sub get_allowed_cobrands {
-
-
- my $allowed_cobrand_string = mySociety::Config::get('ALLOWED_COBRANDS');
+ my $allowed_cobrand_string = FixMyStreet->config('ALLOWED_COBRANDS');
my @allowed_cobrands = split( /\|/, $allowed_cobrand_string );
return \@allowed_cobrands;
}
-=item cobrand_handle Q
+=head2 available_cobrand_classes
+
+ @available_cobrand_classes =
+ FixMyStreet::Cobrand->available_cobrand_classes();
-Given a query that has the name of a site set, return a handle to the Util module for that
-site, if one exists, or zero if not.
+Return an array of all the classes that were found and that have monikers that
+match the values from get_allowed_cobrands.
=cut
-sub cobrand_handle {
- my $cobrand = shift;
+sub available_cobrand_classes {
+ my $class = shift;
+
+ my %allowed = map { $_ => 1 } @{ $class->get_allowed_cobrands };
+ my @avail = grep { $allowed{ $_->moniker } } @ALL_COBRAND_CLASSES;
+
+ return @avail;
+}
+
+=head2 get_class_for_host
+
+ $cobrand_class = FixMyStreet::Cobrand->get_class_for_host( $host );
- our %handles;
+Given a host determine which cobrand we should be using.
+
+=cut
- # Once we have a handle defined, return it.
- return $handles{$cobrand} if defined $handles{$cobrand};
+sub get_class_for_host {
+ my $class = shift;
+ my $host = shift;
- my $cobrand_class = ucfirst($cobrand);
- my $class = "Cobrands::" . $cobrand_class . "::Util";
- eval "use $class";
+ foreach my $avail ( $class->available_cobrand_classes ) {
+ my $moniker = $avail->moniker;
+ return $avail if $host =~ m{$moniker};
+ }
- eval { $handles{$cobrand} = $class->new };
- $handles{$cobrand} = 0 if $@;
- return $handles{$cobrand};
+ # if none match then use the default
+ return 'FixMyStreet::Cobrand::Default';
}
1;
diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm
index 8b6f7f5f0..608a754f7 100644
--- a/perllib/FixMyStreet/Cobrand/Default.pm
+++ b/perllib/FixMyStreet/Cobrand/Default.pm
@@ -24,6 +24,22 @@ sub new {
return bless $self, $class;
}
+=head2 moniker
+
+ $moniker = $cobrand_class->moniker();
+
+Returns a moniker that can be used to identify this cobrand. By default this is
+the last part of the class name lowercased - eg 'F::C::SomeCobrand' becomes
+'somecobrand'.
+
+=cut
+
+sub moniker {
+ my $class = ref( $_[0] ) || $_[0]; # deal with object or class
+ my ($last_part) = $class =~ m{::(\w+)$};
+ return lc($last_part);
+}
+
=head2 q
$request = $cobrand->q;
@@ -36,11 +52,26 @@ use a request-related method out of a request-context.
=cut
sub q {
+ my $self = shift;
return $self->{request}
|| croak "No request has been set"
. " - should you be calling this method outside of a web request?";
}
+=head2 path_to_web_templates
+
+ $path = $cobrand->path_to_web_templates( );
+
+Returns the path to the templates for this cobrand - by default
+"templates/web/$moniker"
+
+=cut
+
+sub path_to_web_templates {
+ my $self = shift;
+ return FixMyStreet->path_to( 'templates/web', $self->moniker );
+}
+
=head1 site_restriction
Return a site restriction clause and a site key if the cobrand uses a subset of
@@ -100,7 +131,8 @@ sub base_url { mySociety::Config::get('BASE_URL') }
=head2 enter_postcode_text
-Return the text that prompts the user to enter their postcode/place name. Parameter is QUERY
+Return the text that prompts the user to enter their postcode/place name.
+Parameter is QUERY
=cut
@@ -124,7 +156,8 @@ sub set_lang_and_domain {
=head2 alert_list_options
-Return HTML for a list of alert options for the cobrand, given QUERY and OPTIONS.
+Return HTML for a list of alert options for the cobrand, given QUERY and
+OPTIONS.
=cut
@@ -184,7 +217,8 @@ sub prettify_epoch { 0 }
=head2 form_elements
-Parameters are FORM_NAME, QUERY. Return HTML for any extra needed elements for FORM_NAME
+Parameters are FORM_NAME, QUERY. Return HTML for any extra needed elements for
+FORM_NAME
=cut
@@ -192,7 +226,8 @@ sub form_elements { '' }
=head2 cobrand_data_for_generic_update
-Parameter is UPDATE_DATA, a reference to a hash of non-cobranded update data. Return cobrand extra data for the update
+Parameter is UPDATE_DATA, a reference to a hash of non-cobranded update data.
+Return cobrand extra data for the update
=cut
@@ -200,7 +235,8 @@ sub cobrand_data_for_generic_update { '' }
=head2 cobrand_data_for_generic_update
-Parameter is PROBLEM_DATA, a reference to a hash of non-cobranded problem data. Return cobrand extra data for the problem
+Parameter is PROBLEM_DATA, a reference to a hash of non-cobranded problem data.
+Return cobrand extra data for the problem
=cut
@@ -265,7 +301,8 @@ sub extra_update_meta_text { '' }
=head2 url
-Given a URL ($_[1]), QUERY, EXTRA_DATA, return a URL with any extra params needed appended to it.
+Given a URL ($_[1]), QUERY, EXTRA_DATA, return a URL with any extra params
+needed appended to it.
=cut
@@ -345,7 +382,7 @@ string LOCATION passes the cobrands checks.
=cut
-geocoded_string_check { return 1; }
+sub geocoded_string_check { return 1; }
=head2 council_check
@@ -354,7 +391,7 @@ COUNCILS pass any extra checks. CONTEXT is where we are on the site.
=cut
-sub council_check { return ( 1, '' ) }
+sub council_check { return ( 1, '' ); }
=head2 feed_xsl
@@ -424,11 +461,11 @@ Get the value for KEY from the config file for COBRAND
sub get_cobrand_conf {
my ( $self, $key ) = @_;
- my $value = undef;
- my $cobrand_code = $self->code;
+ my $value = undef;
+ my $cobrand_moniker = $self->moniker;
my $cobrand_config_file =
- FixMyStreet->path_to("conf/cobrands/$cobrand_code/general");
+ FixMyStreet->path_to("conf/cobrands/$cobrand_moniker/general");
my $normal_config_file = FixMyStreet->path_to('conf/general');
if ( -e $cobrand_config_file ) {
@@ -437,7 +474,7 @@ sub get_cobrand_conf {
# change mySociety::Config so that it can return values from a
# particular config file instead
mySociety::Config::set_file("$cobrand_config_file");
- my $config_key = $key . "_" . uc($cobrand_code);
+ my $config_key = $key . "_" . uc($cobrand_moniker);
$value = mySociety::Config::get( $config_key, undef );
mySociety::Config::set_file("$normal_config_file");
}
@@ -457,11 +494,11 @@ Return if we are the virtual host that sends email for this cobrand
=cut
sub email_host {
- my $self = shift;
- my $cobrand_code_uc = uc( $self->code );
+ my $self = shift;
+ my $cobrand_moniker_uc = uc( $self->moniker );
my $email_vhost =
- mySociety::Config::get("EMAIL_VHOST_$cobrand_code_uc")
+ mySociety::Config::get("EMAIL_VHOST_$cobrand_moniker_uc")
|| mySociety::Config::get("EMAIL_VHOST")
|| '';
diff --git a/perllib/FixMyStreet/Cobrand/EmptyHomes.pm b/perllib/FixMyStreet/Cobrand/EmptyHomes.pm
index 2d0798989..6b907cbd0 100644
--- a/perllib/FixMyStreet/Cobrand/EmptyHomes.pm
+++ b/perllib/FixMyStreet/Cobrand/EmptyHomes.pm
@@ -1,9 +1,11 @@
-package FixMyStreet::Cobrands::EmptyHomes;
-use base 'FixMyStreet::Cobrands::Default';
+package FixMyStreet::Cobrand::EmptyHomes;
+use base 'FixMyStreet::Cobrand::Default';
use strict;
use warnings;
+use FixMyStreet;
+use mySociety::Locale;
use Carp;
=item
@@ -13,11 +15,11 @@ Return the base url for this cobranded site
=cut
sub base_url {
- my $base_url = mySociety::Config::get('BASE_URL');
- if ($base_url !~ /emptyhomes/) {
- $base_url =~ s/http:\/\//http:\/\/emptyhomes\./g;
- }
- return $base_url;
+ my $base_url = FixMyStreet->config('BASE_URL');
+ if ( $base_url !~ /emptyhomes/ ) {
+ $base_url =~ s/http:\/\//http:\/\/emptyhomes\./g;
+ }
+ return $base_url;
}
sub admin_base_url {
@@ -25,7 +27,7 @@ sub admin_base_url {
}
sub area_types {
- return qw(DIS LBO MTD UTA LGD COI); # No CTY
+ return qw(DIS LBO MTD UTA LGD COI); # No CTY
}
=item set_lang_and_domain LANG UNICODE
@@ -35,9 +37,10 @@ Set the language and text domain for the site based on the query and host.
=cut
sub set_lang_and_domain {
- my ($self, $lang, $unicode) = @_;
- mySociety::Locale::negotiate_language('en-gb,English,en_GB|cy,Cymraeg,cy_GB', $lang);
- mySociety::Locale::gettext_domain('FixMyStreet-EmptyHomes', $unicode);
+ my ( $self, $lang, $unicode ) = @_;
+ mySociety::Locale::negotiate_language(
+ 'en-gb,English,en_GB|cy,Cymraeg,cy_GB', $lang );
+ mySociety::Locale::gettext_domain( 'FixMyStreet-EmptyHomes', $unicode );
mySociety::Locale::change();
}
@@ -47,7 +50,7 @@ Return the title to be used in page heads
=cut
-sub site_title {
+sub site_title {
my ($self) = @_;
return _('Report Empty Homes');
}
@@ -57,6 +60,7 @@ sub site_title {
Return the XSL file path to be used for feeds'
=cut
+
sub feed_xsl {
my ($self) = @_;
return '/xsl.eha.xsl';
diff --git a/perllib/FixMyStreet/Cobrand/FiksGataMi.pm b/perllib/FixMyStreet/Cobrand/FiksGataMi.pm
index feec6b0f0..25a4ad83c 100644
--- a/perllib/FixMyStreet/Cobrand/FiksGataMi.pm
+++ b/perllib/FixMyStreet/Cobrand/FiksGataMi.pm
@@ -1,5 +1,5 @@
-package FixMyStreet::Cobrands::FiksGataMi;
-use base 'FixMyStreet::Cobrands::Default';
+package FixMyStreet::Cobrand::FiksGataMi;
+use base 'FixMyStreet::Cobrand::Default';
use strict;
use warnings;
diff --git a/perllib/Problems.pm b/perllib/Problems.pm
index 127d88a30..74ac34f75 100644
--- a/perllib/Problems.pm
+++ b/perllib/Problems.pm
@@ -35,6 +35,16 @@ sub set_site_restriction {
}
}
+# Set the site restrictions using the new cobrand style - no need to special
+# case 'fixmystreet' as default cobrand takes care of that.
+sub set_site_restriction_with_cobrand_object {
+ my $cobrand = shift;
+
+ my $cobrand_data = $cobrand->extra_data;
+ ( $site_restriction, $site_key ) =
+ $cobrand->site_restriction($cobrand_data);
+}
+
sub current_timestamp {
my $current_timestamp = dbh()->selectrow_array('select ms_current_timestamp()');
return "'$current_timestamp'::timestamp";
diff --git a/t/app/controller/about.t b/t/app/controller/about.t
index 2ac628367..adbce8f25 100644
--- a/t/app/controller/about.t
+++ b/t/app/controller/about.t
@@ -11,8 +11,14 @@ $mech->get_ok('/about');
$mech->content_contains('FixMyStreet.com');
# check that geting the page as EHA produces a different page
-ok $mech->host("www.reportemptyhomes.co.uk"), 'change host to reportemptyhomes';
+ok $mech->host("reportemptyhomes.co.uk"), 'change host to reportemptyhomes';
$mech->get_ok('/about');
-$mech->content_lacks('FixMyStreet.com');
+$mech->content_contains('The Empty Homes Agency');
+
+# check that geting the page as EHA in welsh produces a different page
+ok $mech->host("cy.reportemptyhomes.co.uk"),
+ 'change host to cy.reportemptyhomes';
+$mech->get_ok('/about');
+$mech->content_contains('Yr Asiantaeth Tai Gwag');
done_testing();
diff --git a/t/cobrand/loading.t b/t/cobrand/loading.t
new file mode 100644
index 000000000..aa74bbb91
--- /dev/null
+++ b/t/cobrand/loading.t
@@ -0,0 +1,67 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Sub::Override;
+
+use FixMyStreet;
+
+use_ok 'FixMyStreet::Cobrand';
+
+# check that the allowed cobrands is correctly loaded from config
+{
+ my $allowed = FixMyStreet::Cobrand->get_allowed_cobrands;
+ ok $allowed, "got the allowed_cobrands";
+ isa_ok $allowed, "ARRAY";
+ cmp_ok scalar @$allowed, '>', 1, "got more than one";
+ is join( '|', @$allowed ), FixMyStreet->config('ALLOWED_COBRANDS'),
+ "matches config value";
+}
+
+# fake the allowed cobrands for testing
+my $override = Sub::Override->new( #
+ 'FixMyStreet::Cobrand::get_allowed_cobrands' =>
+ sub { return ['emptyhomes'] }
+);
+is_deeply FixMyStreet::Cobrand->get_allowed_cobrands, ['emptyhomes'],
+ 'overidden get_allowed_cobrands';
+
+sub run_host_tests {
+ my %host_tests = @_;
+ for my $host ( sort keys %host_tests ) {
+ is FixMyStreet::Cobrand->get_class_for_host($host),
+ "FixMyStreet::Cobrand::$host_tests{$host}",
+ "does $host -> F::C::$host_tests{$host}";
+ }
+}
+
+# get the cobrand class by host
+run_host_tests(
+ 'www.fixmystreet.com' => 'Default',
+ 'reportemptyhomes.com' => 'EmptyHomes',
+ 'barnet.fixmystreet.com' => 'Default', # not in the allowed_cobrands list
+ 'some.odd.site.com' => 'Default',
+);
+
+# now enable barnet too and check that it works
+$override->replace( #
+ 'FixMyStreet::Cobrand::get_allowed_cobrands' =>
+ sub { return [ 'emptyhomes', 'barnet' ] }
+);
+
+# get the cobrand class by host
+run_host_tests(
+ 'www.fixmystreet.com' => 'Default',
+ 'reportemptyhomes.com' => 'EmptyHomes',
+ 'barnet.fixmystreet.com' => 'Barnet', # found now it is in allowed_cobrands
+ 'some.odd.site.com' => 'Default',
+);
+
+# check that the moniker works as expected both on class and object.
+is FixMyStreet::Cobrand::EmptyHomes->moniker, 'emptyhomes',
+ 'class->moniker works';
+is FixMyStreet::Cobrand::EmptyHomes->new->moniker, 'emptyhomes',
+ 'object->moniker works';
+
+# all done
+done_testing();
diff --git a/templates/web/default/footer.html b/templates/web/default/footer.html
index 57ef9ac28..f5175af8c 100644
--- a/templates/web/default/footer.html
+++ b/templates/web/default/footer.html
@@ -21,5 +21,14 @@
[% INCLUDE 'tracking_code.html' %]
+[% IF c.config.STAGING_SITE %]
+<hr>
+
+<ul>
+ <li>Cobrand: [% c.cobrand.moniker %]</li>
+ <li>extra template path: [% additional_template_paths.join(',') %]</li>
+</ul>
+
+[% END %]
</body>
</html>
diff --git a/templates/web/eha/about/index.html b/templates/web/emptyhomes/about/index.html
index f34d2c9ff..f34d2c9ff 100644
--- a/templates/web/eha/about/index.html
+++ b/templates/web/emptyhomes/about/index.html