1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
#!/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;
BEGIN {
use File::Basename qw(dirname);
use File::Spec;
my $d = dirname(File::Spec->rel2abs($0));
require "$d/../../setenv.pl";
}
use FixMyStreet;
use FixMyStreet::Cobrand;
use FixMyStreet::DB::Factories;
use DateTime::Format::Pg;
use Getopt::Long::Descriptive;
my ($opt, $usage) = describe_options(
'%c %o',
[ 'area-id=i', "MapIt area ID to create body for", { required => 1 } ],
[ 'name:s', "Name of body to use (defaults to MapIt area name)" ],
[ 'empty', "Empty all tables of the database first" ],
[ 'commit', "Actually commit changes to the database" ],
[ 'coords=s', "Co-ordinates to use instead of example postcode" ],
[ 'help', "print usage message and exit", { shortcircuit => 1 } ],
);
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/alert_types.sql')->slurp ) or die $!;
$db->dbh->do( scalar FixMyStreet->path_to('db/generate_secret.sql')->slurp ) or die $!;
say "Emptied database";
}
# Body + categories
my $categories = ['Potholes', 'Street lighting', 'Graffiti', 'Other'];
my $body = FixMyStreet::DB::Factory::Body->find_or_create({
area_id => $opt->area_id,
categories => $categories,
$opt->name ? (name => $opt->name) : (),
});
say "Created body " . $body->name . " for MapIt area ID " . $opt->area_id . ', categories ' . join(', ', @$categories);
FixMyStreet::DB::Factory::ResponseTemplate->create({ body => $body, title => 'Generic' });
FixMyStreet::DB::Factory::ResponseTemplate->create({ body => $body, title => 'Fixed', state => 'fixed - council' });
FixMyStreet::DB::Factory::ResponseTemplate->create({ body => $body, title => 'Monitoring', state => 'unable to fix' });
my $template = FixMyStreet::DB::Factory::ResponseTemplate->create({ body => $body, title => 'Not us', state => 'not responsible' });
$template->add_to_contacts($body->contacts->first);
my $priority = FixMyStreet::DB::Factory::ResponsePriority->create_batch(3, { body => $body });
$priority->[0]->add_to_contacts($body->contacts->first);
# Users
say "Created users, all with password 'password':";
my $user; # Will store the final (normal) user of the loop for later user
my $perms_inspector = ['report_inspect', 'planned_reports'];
my $perms_cs = [
'contribute_as_body', 'contribute_as_another_user',
'moderate', 'view_body_contribute_details',
];
foreach (
{ name => 'Inspector Gadget', email => 'inspector@example.org', body => $body, permissions => $perms_inspector },
{ name => 'Harriet Helpful', email => 'cs@example.org', body => $body, permissions => $perms_cs },
{ name => 'Super User', email => 'super@example.org', body => $body, permissions => [
@$perms_cs, @$perms_inspector, 'report_edit',
'category_edit', 'template_edit', 'responsepriority_edit',
'user_assign_body', 'user_manage_permissions', 'user_edit',
] },
{ name => 'Wizard of Oz', email => 'admin@example.org', is_superuser => 't' },
{ name => "Norma User", email => 'user@example.org' },
) {
$user = FixMyStreet::DB::Factory::User->find_or_create($_);
my $perms = $_->{permissions} ? " (" . join(', ', @{$_->{permissions} || []}) . ")" : "";
my $su = $_->{is_superuser} ? " (superuser)" : "";
say "* $_->{email}$perms$su";
}
# 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 ($location, $lat, $lon);
if ($opt->coords) {
$location = $opt->coords;
($lat, $lon) = split ',', $location;
} else {
my $postcode = mySociety::MaPit::call('area/example_postcode', $opt->area_id);
$postcode = mySociety::MaPit::call('postcode', $postcode);
($location, $lat, $lon) = map { $postcode->{$_} } qw/postcode wgs84_lat wgs84_lon/;
}
my $cobrand = 'default';
foreach (FixMyStreet::Cobrand->available_cobrand_classes) {
my $sub = $_->{class} && $_->{class}->can('council_area_id');
if ($sub && &$sub == $opt->area_id) {
$cobrand = $_->{class}->moniker;
last;
}
}
my $num = 10;
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};
push @$problems, FixMyStreet::DB::Factory::Problem->create({
body => $body,
areas => ',' . $opt->area_id . ',',
user => $user,
postcode => $location,
latitude => $lat + rand($inaccurate_km) - $inaccurate_km / 2,
longitude => $lon + rand($inaccurate_km) - $inaccurate_km / 2,
category => $category,
cobrand => $cobrand,
title => $titles->[int(rand(@$titles))],
detail => 'The details of the report would be here, provided by the user, including exact location and how long it has been here.',
confirmed => DateTime::Format::Pg->format_datetime($confirmed),
});
}
# Comment
my $updates = [];
for (1..$num) {
$confirmed->add(seconds => rand(1000));
push @$updates, FixMyStreet::DB::Factory::Comment->create({
problem => $problems->[int(rand(@$problems))],
user => $user,
text => 'This is an update on this problem, perhaps providing further relevant information or letting everyone know it is being worked on or fixed.',
confirmed => DateTime::Format::Pg->format_datetime($confirmed),
});
}
say "Created $num updates on problems " . join(', ', map { $_->problem_id } @$updates);
|