diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-03-05 12:00:58 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-03-06 17:24:12 +0000 |
commit | d1c09d6db343d3ff066d9881db507f963844d0e5 (patch) | |
tree | 49214c9626b09ccd81cc97897ea21a3ee6fb642f | |
parent | c851d416f8774166fc709c49e0a2ad01e55e827b (diff) |
[Zurich] Add fixture script.
Remove old unused setup-contacts code, superceded by fixtures.
-rwxr-xr-x | bin/fixmystreet.com/fixture | 103 | ||||
-rwxr-xr-x | bin/setup-contacts | 204 | ||||
-rwxr-xr-x | bin/zurich/fixture | 116 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 44 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Zurich.pm | 95 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Factories.pm | 133 |
6 files changed, 254 insertions, 441 deletions
diff --git a/bin/fixmystreet.com/fixture b/bin/fixmystreet.com/fixture index aceb75bd3..fc04e531d 100755 --- a/bin/fixmystreet.com/fixture +++ b/bin/fixmystreet.com/fixture @@ -16,12 +16,9 @@ BEGIN { } use List::Util qw(shuffle); -use Path::Tiny; use FixMyStreet; use FixMyStreet::Cobrand; use FixMyStreet::DB::Factories; -use FixMyStreet::App::Model::PhotoSet; -use DateTime::Format::Pg; use Getopt::Long::Descriptive; my ($opt, $usage) = describe_options( @@ -35,32 +32,7 @@ my ($opt, $usage) = describe_options( ); print($usage->text), exit if $opt->help; -my $db = FixMyStreet::DB->schema->storage; -$db->txn_begin; -END { - if ($db) { - $opt->commit ? $db->txn_commit : $db->txn_rollback; - } -} -if (!$opt->commit) { - say "NOT COMMITTING TO DATABASE"; -} - -if ($opt->empty) { - $db->dbh->do(q{ -DO -$func$ -BEGIN - EXECUTE - (SELECT 'TRUNCATE TABLE ' || string_agg(quote_ident(tablename), ', ') || ' RESTART IDENTITY CASCADE ' - FROM pg_tables WHERE schemaname='public'); -END -$func$; -}) or die $!; - $db->dbh->do( scalar FixMyStreet->path_to('db/fixture.sql')->slurp ) or die $!; - $db->dbh->do( scalar FixMyStreet->path_to('db/generate_secret.sql')->slurp ) or die $!; - say "Emptied database"; -} +FixMyStreet::DB::Factories->setup($opt); # Body + categories my $categories = ['Potholes', 'Street lighting', 'Graffiti', 'Other']; @@ -119,50 +91,6 @@ foreach ( # Problems -my %titles = ( - 'Potholes' => ['Deep pothole', 'Small pothole', 'Pothole in cycle lane', 'Pothole on busy pavement', 'Large pothole', 'Sinking manhole'], - 'Street lighting' => ['Faulty light', 'Street light not working', 'Lights out in tunnel', 'Light not coming on', 'Light not going off'], - 'Graffiti' => ['Graffiti', 'Graffiti', 'Offensive graffiti', 'Graffiti on the bridge', 'Remove graffiti'], - 'Other' => ['Loose drain cover', 'Flytipping on country lane', 'Vehicle blocking footpath', 'Hedge encroaching pavement', 'Full litter bins'], -); -my %photos = ( - 'Potholes' => [ '33717571655_46dfc6f65f_z.jpg', '37855543925_9dbbbecf41_z.jpg', '19119222668_a3c866d7c8_z.jpg', '12049724866_404b066875_z.jpg', '3705226606_eac71cf195_z.jpg', '6304445383_bd216ca892_z.jpg' ], - 'Street lighting' => ['38110448864_fd71227247_z.jpg', '27050321819_ac123400eb_z.jpg', '35732107202_b790c61f63_z.jpg', '31889115854_01cdf38b0d_z.jpg', undef ], - 'Graffiti' => ['12205918375_f37f0b27a9_z.jpg', '8895442578_376a9b0be0_z.jpg', '22998854352_17555b7536_z.jpg', '22593395257_3d48f23bfa_z.jpg', '20515339175_f4ed9fc1d9_z.jpg' ], - 'Other' => ['14347396807_20737504f7_z.jpg', '14792525771_167bc20e3d_z.jpg', undef, '36296226976_a83a118ff8_z.jpg', '23222004240_273977b2b2_z.jpg'], -); -my %descriptions = ( - 'Potholes' => [ - '6” deep pothole in the very centre of the Bristol road; cars are swerving to avoid it. Please treat this as a matter of urgency.', - 'It’s small but it’s a trip hazard. Right where people cross over to get into the school or church. About 3” across but will become larger if not attended to.', - 'Just went over my handlebars as I didn’t see this pothole on Banbury road, just before the traffic lights. Dread to think what might have happened if the traffic had been busier.', - 'I work in the cafe at 34 Clarington Avenue and we’ve had four people come in having tripped over in the last seven days. The pothole’s right outside the key-cutting shop, just near the alleyway.', - 'This has been here, next to the side of the road, for a month', - 'A manhole on the junction of Etherington Road is sinking into the road surface. Not only is it an accident waiting to happen but it’s making a terrible noise every time a car passes over it.', - ], - 'Street lighting' => [ - 'I saw a workman attempting to fix this streetlight over a week ago, and ever since then it’s come on in the daytime and gone off as soon as it gets dark. Come and sort it out please!', - 'Every Tuesday night I have to walk across the carpark outside the station at around 9pm. Not a problem in summer but now the nights are drawing in I feel very unsafe. Please get the streetlight by the exit fixed as I’m sure I can’t be the only woman feeling vulnerable.', - 'My toddler is too scared to go in now, as soon as you’re more than a few paces in it’s absolutely pitch black with no hope of seeing any puddles or worse on the floor. I think this needs seeing to as a priority. Thank you.', - 'I think the lights in the multi storey carpark are motion sensitive but I’ve actually never seen them come on. Maybe the bulb needs replacing??', - 'This streetlight is right outside my bedroom window. It is on 24 hours a day, even in blazing sunlight. Apart from the fact that it’s a waste of electricity, it makes my bedroom feel like an interrogation chamber. Please come and fix it.', - ], - 'Graffiti' => [ - 'Someone has scrawled a really offensive piece of graffiti (are they called ‘tags’??) on the side of the town hall. You might want to see about getting it cleaned off. Wouldn’t want my own children to see that, I’m sure others feel the same.', - 'Can’t see the timetable at the bus shelter cos some idiot’s covered it all in red spray paint. Honestly. Kids of today.', - 'Not gonna write down what it depicts cos I suspect that’d get caught in your profanity filter lol. But please do come and paint over this monstrosity before it causes an accident.', - 'That same guy that’s graffitied all over town has gone and done the same on the passenger bridge over the tracks, you can see it as you come into the station. Ugly bit of garbage graffiti. Bit of a poor first impression for the town eh.', - 'What’s the procedure for requesting a bit of graffiti be removed? There’s been a huge scrawl on the wall outside the club for months. Nice sentiment maybe but really brings the tone of the area down.', - ], - 'Other' => [ - 'Surprised me so much when I crossed the road I nearly took a tumble! Glad I didn’t fall in, this really needs securing now.', - 'Some unmentionable has driven down Larker’s Lane and left a huge heap of old rubbish on the verge. Talk about ruining the view! Such a beautiful spot and these lowlifes come and dump their junk. Probably trying to avoid paying the tip.', - 'Well someone on foot can just about squeeze through but good luck if you’ve got a pushchair or god forbid a wheelchair. Think someone’s abandoned this car; it hasn’t moved in weeks.', - 'Awful trying to walk past after a rain shower, well any time really.', - 'I think these need seeing to more frequently, they’re always full to overflowing by midday.', - ], -); - my ($location, $lat, $lon); if ($opt->coords) { $location = $opt->coords; @@ -185,42 +113,21 @@ foreach (FixMyStreet::Cobrand->available_cobrand_classes) { my $user = $users{'user@example.org'}; my $num = 20; say "Created $num problems around '$location' in cobrand '$cobrand'"; -my $inaccurate_km = 0.01; my $confirmed = DateTime->today->subtract(days => 1)->add(hours => 8); my $problems = []; for (1..$num) { $confirmed->add(seconds => rand(7000)); my $category = $categories->[int(rand(@$categories))]; - my $titles = $titles{$category}; - my $descs = $descriptions{$category}; - my $rand = int(rand(@$titles)); - - my $photo; - if (my $file = $photos{$category}->[$rand]) { - my $files = [ $file ]; - if ($category eq 'Graffiti') { - push @$files, $photos{$category}->[int(rand(@$titles))]; - } - $files = [ map { path(FixMyStreet->path_to("t/images/$_"))->slurp_raw } @$files ]; - my $photoset = FixMyStreet::App::Model::PhotoSet->new({ - data_items => $files, - }); - $photo = $photoset->data; - } - - push @$problems, FixMyStreet::DB::Factory::Problem->create({ + push @$problems, FixMyStreet::DB::Factory::Problem->create_problem({ body => $body, areas => ',' . $opt->area_id . ',', user => $user, postcode => $location, - latitude => $lat + rand(2 * $inaccurate_km) - $inaccurate_km, - longitude => $lon + rand(3 * $inaccurate_km) - 1.5 * $inaccurate_km, + latitude => $lat, + longitude => $lon, category => $category, cobrand => $cobrand, - title => $titles->[$rand], - detail => $descs->[$rand], - photo_id => $photo, - confirmed => DateTime::Format::Pg->format_datetime($confirmed), + confirmed => $confirmed, }); } diff --git a/bin/setup-contacts b/bin/setup-contacts deleted file mode 100755 index d562ae71d..000000000 --- a/bin/setup-contacts +++ /dev/null @@ -1,204 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; -require 5.8.0; -use feature 'say'; - -BEGIN { - use File::Basename qw(dirname); - use File::Spec; - my $d = dirname(File::Spec->rel2abs($0)); - require "$d/../setenv.pl"; -} - -use FixMyStreet::App; - -my $moniker = $ARGV[0]; - -my $c = FixMyStreet::App->new(); -my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($moniker)->new({ c => $c }); -$c->stash->{cobrand} = $cobrand; - -die "Not a staging site, bailing out" unless $c->config->{STAGING_SITE}; # TODO, allow override via --force -say "Applying contacts for $cobrand"; - - -ensure_bodies(); -setup_contacts(); - -=head2 setup_contacts, ensure_bodies - -routines to update extra data for contacts. These can be called by -a script: - - bin/setup-contacts zurich - -=cut - -sub ensure_bodies { - my @bodies = $cobrand->body_details_data; - - my $bodies_rs = $c->model('DB::Body'); - - for my $body (@bodies) { - # following should work (having added Unique name/parent constraint, but doesn't) - # $bodies_rs->find_or_create( $body, { $parent ? ( key => 'body_name_parent_key' ) : () } ); - # let's keep it simple and just allow unique names - next if $bodies_rs->search({ name => $body->{name} })->count; - if (my $area_id = delete $body->{area_id}) { - $body->{body_areas} = [ { area_id => $area_id } ]; - } - my $parent = $body->{parent}; - if ($parent and ! ref $parent) { - $body->{parent} = { name => $parent }; - } - $bodies_rs->find_or_create( $body ); - } -} - -sub setup_contacts { - die "Not a staging site, bailing out" unless $c->config->{STAGING_SITE}; # TODO, allow override via --force - - my @contact_details = $cobrand->contact_details_data; - - for my $detail (@contact_details) { - update_contact( $detail, $description ); - } -} - -sub update_contact { - my ($contact_data, $description) = @_; - - my $contact_rs = $c->model('DB::Contact'); - - my $category = $contact_data->{category} or die "No category provided"; - $description ||= "Update contact"; - - my $contact = ensure_contact($contact_data, $description) - or return; # e.g. nothing returned if deleted - - if (my $fields = $contact_data->{fields}) { - - my @fields = map { get_field_extra($_) } @$fields; - my $note = sprintf 'Fields edited by automated script%s', $description ? " ($description)" : ''; - $contact->set_extra_fields(@fields); - $contact->set_inflated_columns({ - confirmed => 1, - deleted => 0, - editor => 'automated script', - whenedited => \'NOW()', - note => "Updated fields $description", - }); - $contact->update; - } -} - -sub ensure_contact { - my ($contact_data, $description) = @_; - - my $category = $contact_data->{category} or die "No category provided"; - $description ||= "Ensure contact exists $category"; - - my $email = temp_email_to_update(); # will only be set if newly created - - my $body = get_body_for_contact($contact_data) or die "No body found for $category"; - - my $contact_rs = $c->model('DB::Contact'); - - my $category_details = $contact_data->{category_details} || {}; - - if (my $old_name = delete $contact_data->{rename_from}) { - if (my $old_category = $contact_rs->find({ - category => $old_name, - , body => $body, - })) { - $old_category->update({ - category => $category, - whenedited => \'NOW()', - note => "Renamed $description", - %{ $category_details || {} }, - }); - return $old_category; - } - } - - if ($contact_data->{delete}) { - my $contact = $contact_rs->search({ - body_id => $body->id, - category => $category, - deleted => 0 - }); - if ($contact->count) { - print sprintf "Deleting: %s\n", $category; - $contact->update({ - deleted => 1, - editor => 'automated script', - whenedited => \'NOW()', - note => "Deleted by script $description", - }); - } - return; - } - - return $contact_rs->find_or_create( - { - body => $body, - category => $category, - - confirmed => 1, - deleted => 0, - email => $email, - editor => 'automated script', - note => 'created by automated script', - send_method => '', - whenedited => \'NOW()', - %{ $category_details || {} }, - }, - { - key => 'contacts_body_id_category_idx' - } - ); -} - -sub get_field_extra { - my ($field) = @_; - - my %default = ( - variable => 'true', - order => '1', - required => 'no', - datatype => 'string', - datatype_description => 'a string', - ); - - if (($field->{datatype} || '') eq 'boolean') { - %default = ( - %default, - datatype => 'singlevaluelist', - datatype_description => 'Yes or No', - values => { value => [ - { key => ['No'], name => ['No'] }, - { key => ['Yes'], name => ['Yes'] }, - ] }, - ); - } - - return { %default, %$field }; -} - -sub temp_email_to_update { 'test@example.com' } - -sub get_body_for_contact { - my ($contact_data) = @_; - if (my $body_name = $contact_data->{body_name}) { - return $c->model('DB::Body')->find({ name => $body_name }); - } - if ($cobrand->can('contact_details_data_body_default')) { - return $cobrand->contact_details_data_body_default; - } - return; - # TODO: for UK Councils use - # $c->model('DB::Body')->find(id => $cobrand->council_id); - # # NB: (or better that's the area in BodyAreas) -} diff --git a/bin/zurich/fixture b/bin/zurich/fixture new file mode 100755 index 000000000..fec9977a5 --- /dev/null +++ b/bin/zurich/fixture @@ -0,0 +1,116 @@ +#!/usr/bin/env perl +# +# This script will create a test body and its categories, covering the area +# provided, and users associated with that body, which should help testing +# of report interactions. + +use strict; +use warnings; +use v5.14; +use utf8; + +BEGIN { + use File::Basename qw(dirname); + use File::Spec; + my $d = dirname(File::Spec->rel2abs($0)); + require "$d/../../setenv.pl"; +} + +use FixMyStreet; +use FixMyStreet::DB::Factories; +use Getopt::Long::Descriptive; + +my ($opt, $usage) = describe_options( + '%c %o', + [ 'empty', "Empty all tables of the database first" ], + [ 'commit', "Actually commit changes to the database" ], + [ 'help', "print usage message and exit", { shortcircuit => 1 } ], +); +print($usage->text), exit if $opt->help; + +FixMyStreet::DB::Factories->setup($opt); + +# Body + categories +my $body = FixMyStreet::DB::Factory::Body->find_or_create({ + name => 'Zürich', + body_areas => [], + contacts => [], +}); +say "Created body " . $body->name; + +my $categories = ['Potholes', 'Street lighting', 'Graffiti', 'Other']; +my $div = FixMyStreet::DB::Factory::Body->find_or_create({ + name => 'Division 1', + parent => $body->id, + endpoint => 'division@example.org', + categories => $categories, + area_id => 423017, +}); +say "Created body " . $div->name; + +my $contact = $div->contacts->first; +$contact->set_extra_fields( + map { $_->{variable} = 'true'; $_->{datatype} = 'string'; $_ } + { code => 'strasse', description => 'Strasse', required => 'yes', }, + { code => 'haus_nr', description => 'Haus-Nr.', }, + { code => 'mast_nr', description => 'Mast-Nr.', } +); +$contact->update; + +my $subdiv = FixMyStreet::DB::Factory::Body->find_or_create({ + name => 'Subdivision A', + parent => $div->id, + endpoint => 'subdivision@example.org', + body_areas => [], + contacts => [], +}); +say "Created body " . $subdiv->name; + +my $ext = FixMyStreet::DB::Factory::Body->find_or_create({ + name => 'External Body', + endpoint => 'external_body@example.org', + body_areas => [], + contacts => [], +}); +say "Created body " . $ext->name; + +# Users +say "Created users, all with password 'password':"; +my %users; +foreach ( + { name => 'Super', email => 'super@example.org', email_verified => 1, body => $body }, + { name => 'DM', email_verified => 1, email => 'dm@example.org', body => $div }, + { name => 'SDM', email_verified => 1, email => 'sdm@example.org', body => $subdiv }, + { name => 'Wizard of Oz', email_verified => 1, email => 'admin@example.org', body => $body, is_superuser => 't' }, + { name => "Norma User", email_verified => 1, email => 'user@example.org' }, +) { + $users{$_->{email}} = FixMyStreet::DB::Factory::User->find_or_create($_); + my $su = $_->{is_superuser} ? " (superuser)" : ""; + say "* $_->{email}$su"; +} + +# Problems + +my $lat = 47.381416; +my $lon = 8.531369; +my $cobrand = 'zurich'; + +my $user = $users{'user@example.org'}; +my $num = 20; +say "Created $num problems around '$lat,$lon' in cobrand '$cobrand'"; +my $confirmed = DateTime->today->subtract(days => 1)->add(hours => 8); +my $problems = []; +for (1..$num) { + $confirmed->add(seconds => rand(7000)); + my $category = $categories->[int(rand(@$categories))]; + push @$problems, FixMyStreet::DB::Factory::Problem->create_problem({ + body => $div, + user => $user, + postcode => "$lat,$lon", + latitude => $lat, + longitude => $lon, + category => $category, + cobrand => $cobrand, + confirmed => $confirmed, + }); +} diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index c6ca5c56b..1883550e5 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -1159,50 +1159,6 @@ sub jurisdiction_id_example { return $self->moniker; } -=item body_details_data - -Returns a list of bodies to create with ensure_body. These -are mostly just passed to ->find_or_create, but there is some -pre-processing so that you can enter: - - area_id => 123, - parent => 'Big Town', - -instead of - - body_areas => [ { area_id => 123 } ], - parent => { name => 'Big Town' }, - -For example: - - return ( - { - name => 'Big Town', - }, - { - name => 'Small town', - parent => 'Big Town', - area_id => 1234, - }, - - -=cut - -sub body_details_data { - return (); -} - -=item contact_details_data - -Returns a list of contact_data to create with setup_contacts. -See Zurich for an example. - -=cut - -sub contact_details_data { - return () -} - =item lookup_by_ref_regex Returns a regex to match postcode form input against to determine if a lookup diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index 457bc1e75..19118774c 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -10,7 +10,6 @@ use DateTime::Format::Pg; use strict; use warnings; -use feature 'say'; use utf8; =head1 NAME @@ -1305,100 +1304,6 @@ sub problem_confirm_email_extras { $self->{c}->stash->{email_confirmed} = $confirmed_reports; } -sub body_details_data { - return ( - { - name => 'Stadt Zurich' - }, - { - name => 'Elektrizitäwerk Stadt Zürich', - parent => 'Stadt Zurich', - area_id => 423017, - }, - { - name => 'ERZ Entsorgung + Recycling Zürich', - parent => 'Stadt Zurich', - area_id => 423017, - }, - { - name => 'Fachstelle Graffiti', - parent => 'Stadt Zurich', - area_id => 423017, - }, - { - name => 'Grün Stadt Zürich', - parent => 'Stadt Zurich', - area_id => 423017, - }, - { - name => 'Tiefbauamt Stadt Zürich', - parent => 'Stadt Zurich', - area_id => 423017, - }, - { - name => 'Dienstabteilung Verkehr', - parent => 'Stadt Zurich', - area_id => 423017, - }, - ); -} - -sub contact_details_data { - return ( - { - category => 'Beleuchtung/Uhren', - body_name => 'Elektrizitätswerk Stadt Zürich', - fields => [ - { - code => 'strasse', - description => 'Strasse', - datatype => 'string', - required => 'yes', - }, - { - code => 'haus_nr', - description => 'Haus-Nr.', - datatype => 'string', - }, - { - code => 'mast_nr', - description => 'Mast-Nr.', - datatype => 'string', - } - ], - }, - { - category => 'Brunnen/Hydranten', - # body_name ??? - fields => [ - { - code => 'hydranten_nr', - description => 'Hydranten-Nr.', - datatype => 'string', - }, - ], - }, - { - category => "Grünflächen/Spielplätze", - body_name => 'Grün Stadt Zürich', - rename_from => "Tiere/Grünflächen", - }, - { - category => 'Spielplatz/Sitzbank', - body_name => 'Grün Stadt Zürich', - delete => 1, - }, - ); -} - -sub contact_details_data_body_default { - my ($self) = @_; - # temporary measure to assign un-bodied contacts to parent - # (this isn't at all how things will be setup in live, but is - # handy during dev.) - return $self->{c}->model('DB::Body')->find({ name => 'Stadt Zurich' }); -} - sub reports_per_page { return 20; } sub singleton_bodies_str { 1 } diff --git a/perllib/FixMyStreet/DB/Factories.pm b/perllib/FixMyStreet/DB/Factories.pm index 0e99608e1..66148ad61 100644 --- a/perllib/FixMyStreet/DB/Factories.pm +++ b/perllib/FixMyStreet/DB/Factories.pm @@ -1,5 +1,46 @@ +use strict; +use warnings; +use v5.14; + use FixMyStreet::DB; +package FixMyStreet::DB::Factories; + +my $db; +my $opt; + +END { + if ($db) { + $opt->commit ? $db->txn_commit : $db->txn_rollback; + } +} +sub setup { + my $cls = shift; + + $opt = shift; + $db = FixMyStreet::DB->schema->storage; + $db->txn_begin; + if (!$opt->commit) { + say "NOT COMMITTING TO DATABASE"; + } + + if ($opt->empty) { + $db->dbh->do(q{ +DO +$func$ +BEGIN + EXECUTE + (SELECT 'TRUNCATE TABLE ' || string_agg(quote_ident(tablename), ', ') || ' RESTART IDENTITY CASCADE ' + FROM pg_tables WHERE schemaname='public'); +END +$func$; +}) or die $!; + $db->dbh->do( scalar FixMyStreet->path_to('db/fixture.sql')->slurp ) or die $!; + $db->dbh->do( scalar FixMyStreet->path_to('db/generate_secret.sql')->slurp ) or die $!; + say "Emptied database"; + } +} + package FixMyStreet::DB::Factory::Base; use parent "DBIx::Class::Factory"; @@ -19,6 +60,10 @@ sub find_or_create { package FixMyStreet::DB::Factory::Problem; use parent "DBIx::Class::Factory"; +use Path::Tiny; +use DateTime::Format::Pg; +use FixMyStreet; +use FixMyStreet::App::Model::PhotoSet; __PACKAGE__->resultset(FixMyStreet::DB->resultset("Problem")); @@ -43,6 +88,94 @@ __PACKAGE__->fields({ category => 'Other', }); +sub data { + my $self = shift; + + my %titles = ( + 'Potholes' => ['Deep pothole', 'Small pothole', 'Pothole in cycle lane', 'Pothole on busy pavement', 'Large pothole', 'Sinking manhole'], + 'Street lighting' => ['Faulty light', 'Street light not working', 'Lights out in tunnel', 'Light not coming on', 'Light not going off'], + 'Graffiti' => ['Graffiti', 'Graffiti', 'Offensive graffiti', 'Graffiti on the bridge', 'Remove graffiti'], + 'Other' => ['Loose drain cover', 'Flytipping on country lane', 'Vehicle blocking footpath', 'Hedge encroaching pavement', 'Full litter bins'], + ); + my %photos = ( + 'Potholes' => [ '33717571655_46dfc6f65f_z.jpg', '37855543925_9dbbbecf41_z.jpg', '19119222668_a3c866d7c8_z.jpg', '12049724866_404b066875_z.jpg', '3705226606_eac71cf195_z.jpg', '6304445383_bd216ca892_z.jpg' ], + 'Street lighting' => ['38110448864_fd71227247_z.jpg', '27050321819_ac123400eb_z.jpg', '35732107202_b790c61f63_z.jpg', '31889115854_01cdf38b0d_z.jpg', undef ], + 'Graffiti' => ['12205918375_f37f0b27a9_z.jpg', '8895442578_376a9b0be0_z.jpg', '22998854352_17555b7536_z.jpg', '22593395257_3d48f23bfa_z.jpg', '20515339175_f4ed9fc1d9_z.jpg' ], + 'Other' => ['14347396807_20737504f7_z.jpg', '14792525771_167bc20e3d_z.jpg', undef, '36296226976_a83a118ff8_z.jpg', '23222004240_273977b2b2_z.jpg'], + ); + my %descriptions = ( + 'Potholes' => [ + '6” deep pothole in the very centre of the Bristol road; cars are swerving to avoid it. Please treat this as a matter of urgency.', + 'It’s small but it’s a trip hazard. Right where people cross over to get into the school or church. About 3” across but will become larger if not attended to.', + 'Just went over my handlebars as I didn’t see this pothole on Banbury road, just before the traffic lights. Dread to think what might have happened if the traffic had been busier.', + 'I work in the cafe at 34 Clarington Avenue and we’ve had four people come in having tripped over in the last seven days. The pothole’s right outside the key-cutting shop, just near the alleyway.', + 'This has been here, next to the side of the road, for a month', + 'A manhole on the junction of Etherington Road is sinking into the road surface. Not only is it an accident waiting to happen but it’s making a terrible noise every time a car passes over it.', + ], + 'Street lighting' => [ + 'I saw a workman attempting to fix this streetlight over a week ago, and ever since then it’s come on in the daytime and gone off as soon as it gets dark. Come and sort it out please!', + 'Every Tuesday night I have to walk across the carpark outside the station at around 9pm. Not a problem in summer but now the nights are drawing in I feel very unsafe. Please get the streetlight by the exit fixed as I’m sure I can’t be the only woman feeling vulnerable.', + 'My toddler is too scared to go in now, as soon as you’re more than a few paces in it’s absolutely pitch black with no hope of seeing any puddles or worse on the floor. I think this needs seeing to as a priority. Thank you.', + 'I think the lights in the multi storey carpark are motion sensitive but I’ve actually never seen them come on. Maybe the bulb needs replacing??', + 'This streetlight is right outside my bedroom window. It is on 24 hours a day, even in blazing sunlight. Apart from the fact that it’s a waste of electricity, it makes my bedroom feel like an interrogation chamber. Please come and fix it.', + ], + 'Graffiti' => [ + 'Someone has scrawled a really offensive piece of graffiti (are they called ‘tags’??) on the side of the town hall. You might want to see about getting it cleaned off. Wouldn’t want my own children to see that, I’m sure others feel the same.', + 'Can’t see the timetable at the bus shelter cos some idiot’s covered it all in red spray paint. Honestly. Kids of today.', + 'Not gonna write down what it depicts cos I suspect that’d get caught in your profanity filter lol. But please do come and paint over this monstrosity before it causes an accident.', + 'That same guy that’s graffitied all over town has gone and done the same on the passenger bridge over the tracks, you can see it as you come into the station. Ugly bit of garbage graffiti. Bit of a poor first impression for the town eh.', + 'What’s the procedure for requesting a bit of graffiti be removed? There’s been a huge scrawl on the wall outside the club for months. Nice sentiment maybe but really brings the tone of the area down.', + ], + 'Other' => [ + 'Surprised me so much when I crossed the road I nearly took a tumble! Glad I didn’t fall in, this really needs securing now.', + 'Some unmentionable has driven down Larker’s Lane and left a huge heap of old rubbish on the verge. Talk about ruining the view! Such a beautiful spot and these lowlifes come and dump their junk. Probably trying to avoid paying the tip.', + 'Well someone on foot can just about squeeze through but good luck if you’ve got a pushchair or god forbid a wheelchair. Think someone’s abandoned this car; it hasn’t moved in weeks.', + 'Awful trying to walk past after a rain shower, well any time really.', + 'I think these need seeing to more frequently, they’re always full to overflowing by midday.', + ], + ); + + return { + titles => \%titles, + descriptions => \%descriptions, + photos => \%photos, + }; +} + +sub create_problem { + my $self = shift; + my $params = shift; + + my $data = $self->data; + my $category = $params->{category}; + my $inaccurate_km = 0.01; + + my $titles = $data->{titles}{$category}; + my $descs = $data->{descriptions}{$category}; + my $rand = int(rand(@$titles)); + + my $photo; + if (my $file = $data->{photos}{$category}->[$rand]) { + my $files = [ $file ]; + if ($category eq 'Graffiti') { + push @$files, $data->{photos}{$category}->[int(rand(@$titles))]; + } + $files = [ map { path(FixMyStreet->path_to("t/images/$_"))->slurp_raw } @$files ]; + my $photoset = FixMyStreet::App::Model::PhotoSet->new({ + data_items => $files, + }); + $photo = $photoset->data; + } + + $params->{latitude} += rand(2 * $inaccurate_km) - $inaccurate_km; + $params->{longitude} += rand(3 * $inaccurate_km) - 1.5 * $inaccurate_km, + $params->{title} = $titles->[$rand]; + $params->{detail} = $descs->[$rand]; + $params->{photo_id} = $photo; + $params->{confirmed} = DateTime::Format::Pg->format_datetime($params->{confirmed}); + return $self->create($params); +} + ####################### package FixMyStreet::DB::Factory::Body; |