Browse Source

Add CSV downloads

main
Adrian Short 5 years ago
parent
commit
891a910e6f
3 changed files with 29 additions and 0 deletions
  1. +17
    -0
      bin/build
  2. +7
    -0
      public/style.css
  3. +5
    -0
      views/authority.haml

+ 17
- 0
bin/build View File

@@ -4,6 +4,7 @@ require 'scraperwiki'
require 'haml' require 'haml'
require 'pp' require 'pp'
require 'logger' require 'logger'
require 'csv'
require_relative '../lib/helpers' require_relative '../lib/helpers'


OUTPUT_DIR = '_site' OUTPUT_DIR = '_site'
@@ -28,6 +29,15 @@ def write_page(path_items, template, locals = {})
# https://support.google.com/webmasters/answer/183668?hl=en&ref_topic=4581190 # https://support.google.com/webmasters/answer/183668?hl=en&ref_topic=4581190
end end


def write_csv(path_items, filename, data)
dir = File.join(OUTPUT_DIR, path_items)
FileUtils.mkdir_p(dir)
@log.debug dir
fn = File.join(dir, filename + '.csv')
File.write(fn, data)
@log.info fn
end

def create_output_dir def create_output_dir
# Recursively delete working directory to ensure no redundant files are left behind from previous builds. # Recursively delete working directory to ensure no redundant files are left behind from previous builds.
# FileUtils.rm_rf(@working_dir) # FileUtils.rm_rf(@working_dir)
@@ -56,6 +66,13 @@ def gen_authorities
") ")
apps = ScraperWiki.select("* from applications where authority_name='#{auth['authority_name']}' order by date_received") apps = ScraperWiki.select("* from applications where authority_name='#{auth['authority_name']}' order by date_received")
write_page(['authorities', slug(auth['authority_name'])], 'authority', { apps: apps, auth: auth, summary: summary }) write_page(['authorities', slug(auth['authority_name'])], 'authority', { apps: apps, auth: auth, summary: summary })

csv_string = CSV.generate do |csv|
csv << apps.first.keys # header row
apps.each { |app| csv << app.values }
end

write_csv(['authorities', slug(auth['authority_name'])], slug(auth['authority_name']), csv_string)
end end
end end




+ 7
- 0
public/style.css View File

@@ -162,6 +162,13 @@ nav
padding: 10px 20px; padding: 10px 20px;
} }


.button, .button a, .button a:visited {
background-color: green;
color: white;
padding: 5px 10px;
margin: 10px 0;
}

.menu { .menu {
margin: 0; margin: 0;
padding: 0; padding: 0;


+ 5
- 0
views/authority.haml View File

@@ -21,6 +21,11 @@


%h2 Applications %h2 Applications


%p
- csv_fn = slug(auth['authority_name']) + '.csv'
%a.button{ :href => csv_fn, :download => csv_fn }
Download CSV data

%table %table
%thead %thead
%tr %tr


Loading…
Cancel
Save