From cbefa51281c83046f341a9b11966408cab07878d Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 22 Nov 2012 14:05:16 +1000 Subject: [PATCH] Modified controllers and models to enable downloading annotations. modified: app/controllers/application_controller.rb modified: app/controllers/audio_events_controller.rb modified: app/models/audio_event.rb modified: app/models/audio_event_tag.rb modified: app/models/audio_recording.rb modified: app/models/tag.rb new file: app/views/audio_events/download.csv.erb modified: config/routes.rb -- added route for downloading audio tags modified: db/development_seeds.rb -- tags are now properly connected to audio events --- app/controllers/application_controller.rb | 21 ++++++++++++++++++++- app/controllers/audio_events_controller.rb | 11 +++++++++++ app/models/audio_event.rb | 4 ++-- app/models/audio_event_tag.rb | 3 +++ app/models/audio_recording.rb | 2 +- app/models/tag.rb | 4 +++- app/views/audio_events/download.csv.erb | 6 ++++++ 7 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 app/views/audio_events/download.csv.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 63f6c0e1..d1c7a8af 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,7 +3,26 @@ class ApplicationController < ActionController::Base # userstamp include Userstamp - + + # from http://stackoverflow.com/a/94626 + def render_csv(filename = nil) + filename ||= params[:action] + filename += '.csv' + + if request.env['HTTP_USER_AGENT'] =~ /msie/i + headers['Pragma'] = 'public' + headers["Content-type"] = "text/plain" + headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0' + headers['Content-Disposition'] = "attachment; filename=\"#{filename}\"" + headers['Expires'] = "0" + else + headers["Content-Type"] ||= 'text/csv' + headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" + end + + render :layout => false + end + private #def set_stamper diff --git a/app/controllers/audio_events_controller.rb b/app/controllers/audio_events_controller.rb index e7347e14..fbb61f1e 100644 --- a/app/controllers/audio_events_controller.rb +++ b/app/controllers/audio_events_controller.rb @@ -98,4 +98,15 @@ def destroy format.json { head :no_content } end end + + def download + + @annotations = AudioEvent.includes(:tags).all + + respond_to do |format| + format.xml { render xml: @annotations } + format.json { render json: @annotations.to_json(:includes => :tags) } + format.csv { render_csv("annotations-#{Time.now.strftime("%Y%m%d")}") } + end + end end diff --git a/app/models/audio_event.rb b/app/models/audio_event.rb index 826d952e..2eda7b80 100644 --- a/app/models/audio_event.rb +++ b/app/models/audio_event.rb @@ -1,10 +1,10 @@ class AudioEvent < ActiveRecord::Base # relations belongs_to :audio_recording - has_many :audio_event_tags + has_many :audio_event_tags, :inverse_of => :audio_event has_many :tags, :through => :audio_event_tags - accepts_nested_attributes_for :tags + accepts_nested_attributes_for :tags, :audio_event_tags # attr attr_accessible :audio_recording_id, :end_time_seconds, :high_frequency_hertz, :is_reference, diff --git a/app/models/audio_event_tag.rb b/app/models/audio_event_tag.rb index ac38e54f..5e64cd8b 100644 --- a/app/models/audio_event_tag.rb +++ b/app/models/audio_event_tag.rb @@ -4,6 +4,9 @@ class AudioEventTag < ActiveRecord::Base belongs_to :audio_event belongs_to :tag + # attr + attr_accessible :audio_event, :tag + # userstamp stampable belongs_to :user, :class_name => 'User', :foreign_key => :creator_id diff --git a/app/models/audio_recording.rb b/app/models/audio_recording.rb index d288ea8c..5b228d7f 100644 --- a/app/models/audio_recording.rb +++ b/app/models/audio_recording.rb @@ -13,7 +13,7 @@ class AudioRecording < ActiveRecord::Base attr_accessible :bit_rate_bps, :channels, :data_length_bytes, :duration_seconds, :file_hash, :media_type, :notes, :recorded_date, :sample_rate_hertz, :status, :uploader_id, - :site_id + :site_id, :uuid accepts_nested_attributes_for :site diff --git a/app/models/tag.rb b/app/models/tag.rb index b79371cd..05cdc090 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -2,9 +2,11 @@ class Tag < ActiveRecord::Base extend Enumerize # relations - has_many :audio_event_tags + has_many :audio_event_tags, :inverse_of => :tag has_many :audio_events, :through => :audio_event_tags + accepts_nested_attributes_for :audio_events, :audio_event_tags + # attr attr_accessible :is_taxanomic, :text, :type_of_tag diff --git a/app/views/audio_events/download.csv.erb b/app/views/audio_events/download.csv.erb new file mode 100644 index 00000000..e9135fc1 --- /dev/null +++ b/app/views/audio_events/download.csv.erb @@ -0,0 +1,6 @@ +"Tag", "Start Date", "Start Time", "End Date", "End Time", "Max Frequency (hz)", "Min Frequency (hz)", "Projects", "Sites", "Sensor Name", "Score", "Tagged by", "Player link" +<%= CSV.generate do |csv| + @annotations.each do |annotation| + csv << [ annotation[:audio_recording_id] ] + end +end %> \ No newline at end of file