diff options
author | Dave Arter <davea@mysociety.org> | 2015-02-05 15:09:49 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2015-10-06 09:09:22 +0100 |
commit | e9961ee495ab87cbe67cf37e53edbae0794541d2 (patch) | |
tree | 79596f3b3cad4325351fb2a92e617c401471d61e | |
parent | a78bb3fc98dd1851e371c78d9743125d02baf04e (diff) |
[Zurich] 'photo required' functionality
- Save 'photo_required' value in Contact->extra from admin edit form
- Enforce per-category photo requirement on new reports
A new step has been added to the photo upload process that
ensures a photo is present if any of the categories chosen for
the report require it.
If the photo is missing an error is displayed to the user in the
same manner as if the photo upload was invalid.
- Add test of mandatory photo categories
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Photo.pm | 39 | ||||
-rw-r--r-- | t/cobrand/zurich.t | 36 | ||||
-rw-r--r-- | templates/web/base/admin/category_edit.html | 3 |
4 files changed, 86 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index d7bca05a7..7ead7db16 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -364,6 +364,14 @@ sub update_contacts : Private { $contact->api_key( $c->get_param('api_key') ); $contact->send_method( $c->get_param('send_method') ); + # Set the photo_required flag in extra to the appropriate value + if ( $c->req->param('photo_required') ) { + $contact->set_extra_metadata_if_undefined( photo_required => 1 ); + } + else { + $contact->unset_extra_metadata( 'photo_required' ); + } + if ( %errors ) { $c->stash->{updated} = _('Please correct the errors below'); $c->stash->{contact} = $contact; diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm index 7cf78cb2f..57a65e8eb 100644 --- a/perllib/FixMyStreet/App/Controller/Photo.pm +++ b/perllib/FixMyStreet/App/Controller/Photo.pm @@ -143,6 +143,7 @@ sub process_photo : Private { return $c->forward('process_photo_upload_or_cache') + || $c->forward('process_photo_required') || 1; # always return true } @@ -167,6 +168,44 @@ sub process_photo_upload_or_cache : Private { return 1; } +=head2 process_photo_required + +Checks that a report has a photo attached if any of its Contacts +require it (by setting extra->photo_required == 1). Puts an error in +photo_error on the stash if it's required and missing, otherwise returns +true. + +(Note that as we have reached this action, we *know* that the photo +is missing, otherwise it would have already been handled.) + +=cut + +sub process_photo_required : Private { + my ( $self, $c ) = @_; + + # load the report + my $report = $c->stash->{report} or return 1; # don't check photo for updates + my $bodies = $c->stash->{bodies}; + + my @contacts = $c-> # + model('DB::Contact') # + ->not_deleted # + ->search( + { + body_id => [ keys %$bodies ], + category => $report->category + } + )->all; + foreach my $contact ( @contacts ) { + if ( $contact->get_extra_metadata('photo_required') ) { + $c->stash->{photo_error} = _("Photo is required."); + return; + } + } + + return 1; +} + =head1 AUTHOR diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t index e1cb55b16..ffee28259 100644 --- a/t/cobrand/zurich.t +++ b/t/cobrand/zurich.t @@ -69,6 +69,7 @@ $division->parent( $zurich->id ); $division->send_method( 'Zurich' ); $division->endpoint( 'division@example.org' ); $division->update; +$division->body_areas->find_or_create({ area_id => 274456 }); my $subdivision = $mech->create_body_ok( 3, 'Subdivision A' ); $subdivision->parent( $division->id ); $subdivision->send_method( 'Zurich' ); @@ -644,6 +645,41 @@ subtest "hidden report email are only sent when requested" => sub { }; }; +subtest "photo must be supplied for categories that require it" => sub { + FixMyStreet::App->model('DB::Contact')->find_or_create({ + body => $division, + category => "Graffiti - photo required", + email => "graffiti\@example.org", + confirmed => 1, + deleted => 0, + editor => "editor", + whenedited => DateTime->now(), + note => "note for graffiti", + extra => { photo_required => 1 } + }); + FixMyStreet::override_config { + MAPIT_TYPES => [ 'O08' ], + MAPIT_URL => 'http://global.mapit.mysociety.org/', + ALLOWED_COBRANDS => [ 'zurich' ], + MAPIT_ID_WHITELIST => [ 274456 ], + MAPIT_GENERATION => 2, + }, sub { + $mech->post_ok( '/report/new', { + detail => 'Problem-Bericht', + lat => 47.381817, + lon => 8.529156, + email => 'user@example.org', + pc => '', + name => '', + category => 'Graffiti - photo required', + photo => '', + submit_problem => 1, + }); + is $mech->res->code, 200, "missing photo shouldn't return anything but 200"; + $mech->content_contains(_("Photo is required."), 'response should contain photo error message'); + }; +}; + subtest "test stats" => sub { FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'zurich' ], diff --git a/templates/web/base/admin/category_edit.html b/templates/web/base/admin/category_edit.html index b827a4b6d..c0bd43ef5 100644 --- a/templates/web/base/admin/category_edit.html +++ b/templates/web/base/admin/category_edit.html @@ -43,6 +43,9 @@ [% IF c.cobrand.moniker != 'zurich' %] <input type="checkbox" name="non_public" value="1" id="non_public"[% ' checked' IF contact.non_public %]> <label class="inline" for="non_public">[% loc('Private') %]</label> + [% ELSE %] + <input type="checkbox" name="photo_required" value="1" id="photo_required"[% ' checked' IF contact.get_extra_metadata('photo_required') %]> + <label class="inline" for="photo_required">[% loc('Photo required') %]</label> [% END %] </p> |