Class: ForestAdminAgent::Routes::Resources::Csv

Inherits:
AbstractAuthenticatedRoute show all
Includes:
QueryHandler, Utils, ForestAdminDatasourceToolkit::Components::Query::ConditionTree
Defined in:
lib/forest_admin_agent/routes/resources/csv.rb

Instance Method Summary collapse

Methods included from QueryHandler

#execute_query, #inject_context_variables, #parse_query_segment

Methods inherited from AbstractAuthenticatedRoute

#build, #format_attributes, #redacted_full_projection, #redacted_projection_with_pks

Methods inherited from AbstractRoute

#add_route, #build, #initialize, #routes

Constructor Details

This class inherits a constructor from ForestAdminAgent::Routes::AbstractRoute

Instance Method Details

#handle_request(args = {}) ⇒ Object



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
# File 'lib/forest_admin_agent/routes/resources/csv.rb', line 21

def handle_request(args = {})
  context = build(args)
  context.permissions.can?(:browse, context.collection)
  context.permissions.can?(:export, context.collection)

  condition_tree = QueryStringParser.parse_condition_tree(context.collection, args)
  search = QueryStringParser.parse_search(context.collection, args)
  search_extended = QueryStringParser.parse_search_extended(args)
  sort = QueryStringParser.parse_sort(context.collection, args)
  context.permissions.assert_can_read_query_fields(
    context.collection,
    condition_tree: condition_tree, sort: sort, search: search, search_extended: search_extended
  )

  filter = ForestAdminDatasourceToolkit::Components::Query::Filter.new(
    condition_tree: ConditionTreeFactory.intersect(
      [
        context.permissions.get_scope(context.collection),
        parse_query_segment(context.collection, args, context.permissions, context.caller),
        condition_tree
      ]
    ),
    search: search,
    search_extended: search_extended,
    sort: sort,
    segment: QueryStringParser.parse_segment(context.collection, args)
  )
  requested = QueryStringParser.parse_requested_projection(context.collection, args)
  projection = context.permissions.redact_projection(
    context.collection,
    requested[:projection],
    named_by_caller: requested[:named_by_caller]
  )
  filename = args[:params][:filename] || args[:params]['collection_name']
  filename += '.csv' unless /\.csv$/i.match?(filename)
  header = Utils::CsvGenerator.filter_header(args[:params][:header], requested[:projection], projection)

  # Generate timestamp for filename
  now = Time.now.strftime('%Y%m%d_%H%M%S')
  filename_with_timestamp = filename.gsub('.csv', "_export_#{now}.csv")

  # Return streaming enumerator instead of full CSV string
  list_records = ->(batch_filter) { context.collection.list(context.caller, batch_filter, projection) }

  {
    content: {
      type: 'Stream',
      enumerator: Utils::CsvGeneratorStream.stream(
        header,
        filter,
        projection,
        list_records,
        Facades::Container.config_from_cache[:limit_export_size]
      ),
      headers: {
        'Content-Type' => 'text/csv; charset=utf-8',
        'Content-Disposition' => "attachment; filename=\"#{filename_with_timestamp}\"",
        'Cache-Control' => 'no-cache',
        'X-Accel-Buffering' => 'no'
      }
    },
    status: 200
  }
end

#setup_routesObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/forest_admin_agent/routes/resources/csv.rb', line 9

def setup_routes
  add_route(
    'forest_list_csv',
    'get',
    '/:collection_name.:format',
    ->(args) { handle_request(args) },
    'csv'
  )

  self
end