Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #508 from rdsubhas/master
Browse files Browse the repository at this point in the history
1718 | Subhas | CPIMS Export Functionality
  • Loading branch information
rdsubhas committed May 14, 2013
2 parents 1e3ae5d + 52486b8 commit 5cf4d25
Show file tree
Hide file tree
Showing 44 changed files with 560 additions and 382 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -34,3 +34,4 @@ lib/tasks/windows/*.msi
vendor/bundle
Gemfile.lock
*.deb
*.xls
7 changes: 7 additions & 0 deletions Gemfile
@@ -1,5 +1,8 @@
source 'https://rubygems.org'

gem 'rapidftr_addon', :git => 'git://github.com/farismosman/rapidftr-addon.git'
gem 'rapidftr_addon_cpims', :git => 'git://github.com/farismosman/rapidftr-addon-cpims.git'

gem 'couchrest', '0.34'
gem 'fastercsv', '1.5.3'
gem 'json', '1.4.6'
Expand Down Expand Up @@ -49,6 +52,10 @@ end
gem 'rufus-scheduler', '~> 2.0.18', :require => false
gem 'daemons', '~> 1.1.9', :require => false

group :development do
gem 'active_reload'
end

group :development, :assets do
gem 'rubyzip', '~> 0.9.9'
gem 'sass', '~> 3.2.7'
Expand Down
22 changes: 22 additions & 0 deletions Gemfile.lock
@@ -1,3 +1,20 @@
GIT
remote: git://github.com/farismosman/rapidftr-addon-cpims.git
revision: 7d4cf4dd9ee13c401b945cdad00314ed0bd5b725
specs:
rapidftr_addon_cpims (0.0.2)
activesupport
i18n
writeexcel

GIT
remote: git://github.com/farismosman/rapidftr-addon.git
revision: 30b6442b9ce69902c1b9b9b631061d54764f78bb
specs:
rapidftr_addon (0.0.2)
activesupport
i18n

GEM
remote: https://rubygems.org/
specs:
Expand All @@ -16,6 +33,7 @@ GEM
rack-mount (~> 0.6.14)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
active_reload (0.6.1)
activemodel (3.0.19)
activesupport (= 3.0.19)
builder (~> 2.1.2)
Expand Down Expand Up @@ -222,6 +240,7 @@ GEM
validatable (1.6.7)
websocket (1.0.7)
will_paginate (3.0.4)
writeexcel (1.0.0)
xpath (0.1.4)
nokogiri (~> 1.3)
zipruby (0.3.6)
Expand All @@ -230,6 +249,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_reload
cancan (~> 1.6.9)
capistrano (~> 2.14.2)
capybara (~> 1.0.1)
Expand Down Expand Up @@ -260,6 +280,8 @@ DEPENDENCIES
pry
rails (= 3.0.19)
rake (= 0.8.7)
rapidftr_addon!
rapidftr_addon_cpims!
rest-client (= 1.3.0)
rspec (~> 2.11.0)
rspec-instafail (~> 0.2.4)
Expand Down
50 changes: 13 additions & 37 deletions app/controllers/advanced_search_controller.rb
Expand Up @@ -25,48 +25,20 @@ def index

def export_data
authorize! :export, Child
selected_records = Hash[params["selections"].to_a.sort_by { |k,v| k}].values.reverse || {} if params["all"] != "Select all records"
selected_records = params["full_results"].split(/,/) if params["all"] == "Select all records"
if selected_records.empty?
record_ids = Hash[params["selections"].to_a.sort_by { |k,v| k}].values.reverse || {} if params["all"] != "Select all records"
record_ids = params["full_results"].split(/,/) if params["all"] == "Select all records"
if record_ids.empty?
raise ErrorResponse.bad_request('You must select at least one record to be exported')
end

children = []
selected_records.each do |child_id| children.push(Child.get(child_id)) end
if params[:commit] == t("child.actions.export_to_photo_wall")
export_photos_to_pdf(children, "#{file_basename}.pdf")
elsif params[:commit] == t("child.actions.export_to_pdf")
pdf_data = ExportGenerator.new(children).to_full_pdf
send_pdf(pdf_data, "#{file_basename}.pdf")
elsif params[:commit] == t("child.actions.export_to_csv")
render_as_csv(children, "#{file_basename}.csv")
end
end

def export_photos_to_pdf children, filename
authorize! :export, Child

pdf_data = ExportGenerator.new(children).to_photowall_pdf
send_pdf(pdf_data, filename)
end
children = record_ids.map { |child_id| Child.get child_id }

def file_basename(child = nil)
prefix = child.nil? ? current_user_name : child.short_id
user = User.find_by_user_name(current_user_name)
"#{prefix}-#{Clock.now.in_time_zone(user.time_zone).strftime('%Y%m%d-%H%M')}"
end

def render_as_csv results, filename
results = results || [] # previous version handled nils - needed?

results.each do |child|
child['photo_url'] = child_photo_url(child, child.primary_photo_id) unless (child.primary_photo_id.nil? || child.primary_photo_id == "")
child['audio_url'] = child_audio_url(child)
RapidftrAddon::ExportTask.active.each do |addon|
if params[:commit] == t("addons.export_task.#{addon.id}.selected")
results = addon.new.export(children)
encrypt_exported_files results, export_filename(children, addon)
end
end

export_generator = ExportGenerator.new results
csv_data = export_generator.to_csv
send_csv(csv_data.data, csv_data.options)
end

def child_fields_selected? criteria_list
Expand Down Expand Up @@ -140,4 +112,8 @@ def prepare_params_for_limited_access_user user
params[:disable_create] = "true"
end

def export_filename(children, export_task)
(children.length == 1 ? children.first.short_id : current_user_name) + '_' + export_task.id.to_s + '.zip'
end

end
24 changes: 6 additions & 18 deletions app/controllers/application_controller.rb
Expand Up @@ -55,14 +55,6 @@ def render_error_response(ex)
end
end

def send_pdf(data, filename)
send_encrypted_file data, :filename => filename, :type => "application/pdf"
end

def send_csv(csv, opts = {})
send_encrypted_file csv, opts
end

def name
self.class.to_s.gsub("Controller", "")
end
Expand All @@ -78,27 +70,23 @@ def clean_params(param)
param.reject{|value| value.blank?}
end

def send_encrypted_file(data, opts = {})
def encrypt_exported_files(results, zip_filename)
if params[:password].present?
zip_filename = File.basename(opts[:filename], ".*") + ".zip"
enc_filename = "#{generate_encrypted_filename}.zip"
enc_filename = CleansingTmpDir.temp_file_name

Zip::Archive.open(enc_filename, Zip::CREATE) do |ar|
ar.add_or_replace_buffer opts[:filename], data
results.each do |result|
ar.add_or_replace_buffer File.basename(result.filename), result.data
end
ar.encrypt params[:password]
end

send_file enc_filename, :filename => zip_filename, :disposition => "inline", :type => 'application/zip'
end
end

def generate_encrypted_filename
dir = CleanupEncryptedFiles.dir_name
FileUtils.mkdir_p dir
File.join dir, UUIDTools::UUID.random_create.to_s
end

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
%(<span class="field-error">) + html_tag + %(</span>)
end

end
86 changes: 20 additions & 66 deletions app/controllers/children_controller.rb
Expand Up @@ -2,7 +2,7 @@ class ChildrenController < ApplicationController
skip_before_filter :verify_authenticity_token
skip_before_filter :check_authentication, :only => [:reindex]

before_filter :load_child_or_redirect, :only => [:show, :edit, :destroy, :edit_photo, :update_photo, :export_photo_to_pdf]
before_filter :load_child_or_redirect, :only => [ :show, :edit, :destroy, :edit_photo, :update_photo ]
before_filter :current_user, :except => [:reindex]
before_filter :sanitize_params, :only => [:update, :sync_unverified]

Expand All @@ -27,15 +27,8 @@ def index
respond_to do |format|
format.html
format.xml { render :xml => @children }
format.csv do
authorize! :export, Child
render_as_csv @children
end
format.pdf do
authorize! :export, Child
pdf_data = ExportGenerator.new(@children).to_full_pdf
send_pdf(pdf_data, "#{file_basename}.pdf")
end

respond_to_export format, @children
end
end

Expand All @@ -51,19 +44,9 @@ def show
respond_to do |format|
format.html
format.xml { render :xml => @child }
format.json { render :json => @child.compact.to_json }

format.json {
render :json => @child.compact.to_json
}
format.csv do
authorize! :export, Child
render_as_csv([@child])
end
format.pdf do
authorize! :export, Child
pdf_data = ExportGenerator.new(@child).to_full_pdf
send_pdf(pdf_data, "#{file_basename(@child)}.pdf")
end
respond_to_export format, [ @child ]
end
end

Expand Down Expand Up @@ -233,19 +216,6 @@ def search
default_search_respond_to
end

def export_photos_to_pdf children, filename
authorize! :export, Child

pdf_data = ExportGenerator.new(children).to_photowall_pdf
send_pdf(pdf_data, filename)
end

def export_photo_to_pdf
authorize! :export, Child
pdf_data = ExportGenerator.new(@child).to_photowall_pdf
send_pdf(pdf_data, "#{file_basename(@child)}.pdf")
end

private

def child_short_id child_params
Expand All @@ -261,27 +231,11 @@ def create_or_update_child(child_params)
end
end

def file_basename(child = nil)
prefix = child.nil? ? current_user_name : child.short_id
user = User.find_by_user_name(current_user_name)
"#{prefix}-#{Clock.now.in_time_zone(user.time_zone).strftime('%Y%m%d-%H%M')}"
end

def sanitize_params
child_params = params['child']
child_params['histories'] = JSON.parse(child_params['histories']) if child_params and child_params['histories'].is_a?(String) #histories might come as string from the mobile client.
end

def file_name_datetime_string
user = User.find_by_user_name(current_user_name)
Clock.now.in_time_zone(user.time_zone).strftime('%Y%m%d-%H%M')
end

def file_name_date_string
user = User.find_by_user_name(current_user_name)
Clock.now.in_time_zone(user.time_zone).strftime("%Y%m%d")
end

def get_form_sections
FormSection.enabled_by_order
end
Expand All @@ -293,23 +247,9 @@ def default_search_respond_to
redirect_to child_path(@results.first)
end
end
format.csv do
render_as_csv(@results) if @results
end
end
end

def render_as_csv results
results = results || [] # previous version handled nils - needed?

results.each do |child|
child['photo_url'] = child_photo_url(child, child.primary_photo_id) unless (child.primary_photo_id.nil? || child.primary_photo_id == "")
child['audio_url'] = child_audio_url(child)
respond_to_export format, @results
end

export_generator = ExportGenerator.new results
csv_data = export_generator.to_csv
send_csv(csv_data.data, csv_data.options)
end

def load_child_or_redirect
Expand Down Expand Up @@ -376,4 +316,18 @@ def update_child_with_attachments(child, params)
child
end

def respond_to_export(format, children)
RapidftrAddon::ExportTask.active.each do |export_task|
format.any(export_task.id) do
authorize! :export, Child
results = export_task.new.export(children)
encrypt_exported_files results, export_filename(children, export_task)
end
end
end

def export_filename(children, export_task)
(children.length == 1 ? children.first.short_id : current_user_name) + '_' + export_task.id.to_s + '.zip'
end

end
1 change: 1 addition & 0 deletions app/models/child.rb
Expand Up @@ -17,6 +17,7 @@ class Child < CouchRestRails::Document
property :nickname
property :unique_identifier
property :short_id
property :created_by
property :created_organisation
property :flag, :cast_as => :boolean
property :reunited, :cast_as => :boolean
Expand Down
2 changes: 1 addition & 1 deletion app/models/migration.rb
Expand Up @@ -19,7 +19,7 @@ def self.migrate
end

def self.all_migrations
Dir[migration_dir.join "*.rb"].sort
Dir[migration_dir.join "*.rb"].map{ |path| File.basename path }.sort
end

def self.applied_migrations
Expand Down
6 changes: 3 additions & 3 deletions app/views/children/_header.html.erb
Expand Up @@ -8,9 +8,9 @@
<span class="export"><%= t("children.export") %></span>
<div class="dropdown hide">
<% if can?(:export, Child) %>
<%= link_to t('children.export_all_child_records_to_csv'), children_path(:format => :csv, :per_page => :all), :class => "password-prompt" %>
<%= link_to t('children.export_all_child_records_to_pdf'), children_path(:format => :pdf, :per_page => :all), :class => "password-prompt" %>
<%= link_to t("children.export_some_records_to_csv"), new_advanced_search_path %>
<% RapidftrAddon::ExportTask.active.each do |addon| %>
<%= link_to t("addons.export_task.#{addon.id}.all"), children_path(:format => addon.id, :per_page => :all), :class => "password-prompt" %>
<% end %>
<%end%>
</div>
</div>
Expand Down
18 changes: 9 additions & 9 deletions app/views/children/_search_results.html.erb
Expand Up @@ -43,15 +43,15 @@
</div>
<div class="profiles-button-wrapper">
<% if !@results.nil? && @results.size > 0 %>
<%= hidden_field_tag 'password', nil, :id => 'hidden-password-field' %>
<%= check_box_tag 'allbottom', 'Select all records' %>
<%= label_tag 'allbottom', t("select_all") %>
<% end %>
<% if can? :export, Child %>
<%= submit_tag t("child.actions.export_to_photo_wall"), :class => "password-prompt" if has_results %>
<%= submit_tag t("child.actions.export_to_pdf"), :class => "password-prompt" if has_results %>
<%= submit_tag t("child.actions.export_to_csv"), :class => "password-prompt" if has_results %>
<%= hidden_field_tag 'password', nil, :id => 'hidden-password-field' %>
<%= check_box_tag 'allbottom', 'Select all records' %>
<%= label_tag 'allbottom', t("select_all") %>
<% if can? :export, Child %>
<% RapidftrAddon::ExportTask.active.each do |addon| %>
<%= submit_tag t("addons.export_task.#{addon.id}.selected"), :class => "password-prompt", :name => "commit" %>
<% end %>
<% end %>
<% end %>
</div>
<%end%>

0 comments on commit 5cf4d25

Please sign in to comment.