Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline CSV generation #3146

Merged
merged 2 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- Better sort admin user table.
- Centralise update creation to include fields.
- Add full text index to speed up admin search.
- Offline process for CSV generation.
- Development improvements:
- `#geolocate_link` is now easier to re-style. #3006
- Links inside `#front-main` can be customised using `$primary_link_*` Sass variables. #3007
Expand Down
77 changes: 77 additions & 0 deletions bin/csv-export
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env perl

# csv-export
# Offline creation of CSV export, first take

use v5.14;
use warnings;

BEGIN {
use File::Basename qw(dirname);
use File::Spec;
my $d = dirname(File::Spec->rel2abs($0));
require "$d/../setenv.pl";
}

use open ':std', ':encoding(UTF-8)';
use Getopt::Long::Descriptive;
use Path::Tiny;
use CronFns;
use FixMyStreet::Cobrand;
use FixMyStreet::DB;
use FixMyStreet::Reporting;

my $site = CronFns::site(FixMyStreet->config('BASE_URL'));
CronFns::language($site);

my ($opts, $usage) = describe_options(
'%c %o',
['cobrand=s', 'which cobrand is asking for the data', { required => 1 }],
['type=s', 'whether to export problems or updates', { required => 1 }],
['out=s', 'where to output CSV data'],

['body=i', 'Body ID to restrict export to'],
['wards=s', 'Ward area IDs to restrict export to'],
['category=s', 'Category to restrict export to'],
['state=s', 'State to restrict export to'],
['start_date=s', 'Start date for export (default 30 days ago)'],
['end_date=s', 'End date for export'],

['user=i', 'user ID which requested this export'],
['verbose|v', 'more verbose output'],
['help|h', "print usage message and exit" ],
);
$usage->die if $opts->help;

my $use_stdout = !$opts->out || $opts->out eq '-';
my ($file, $fh);
if ($use_stdout) {
$fh = *STDOUT;
} else {
$file = path($opts->out . '-part');
$fh = $file->openw_utf8;
}

my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($opts->cobrand);
FixMyStreet::DB->schema->cobrand($cobrand);

my $user = FixMyStreet::DB->resultset("User")->find($opts->user) if $opts->user;
my $body = FixMyStreet::DB->resultset("Body")->find($opts->body) if $opts->body;
my $wards = $opts->wards ? [split',', $opts->wards] : [];

my $reporting = FixMyStreet::Reporting->new(
type => $opts->type,
user => $user,
category => $opts->category,
state => $opts->state,
wards => $wards,
body => $body,
$opts->start_date ? (start_date => $opts->start_date) : (),
end_date => $opts->end_date,
);
$reporting->construct_rs_filter;
$reporting->csv_parameters;
$reporting->generate_csv($fh);
unless ($use_stdout) {
$file->move($opts->out);
}
12 changes: 9 additions & 3 deletions perllib/Catalyst/Authentication/Credential/AccessToken.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ sub new {
return $self;
}

sub authenticate {
my ( $self, $c, $realm, $authinfo_ignored ) = @_;

sub get_token {
my ($self, $c) = @_;
my $auth_header = $c->req->header('Authorization') || '';
my ($token) = $auth_header =~ /^Bearer (.*)/i;
$token ||= $c->get_param('access_token');
return $token;
}

sub authenticate {
my ( $self, $c, $realm, $authinfo_ignored ) = @_;

my $token = $self->get_token($c);
return unless $token;

my $id;
Expand Down