mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-16 23:30:12 +01:00
fix import for csv
This commit is contained in:
parent
76902e95c0
commit
a31c0a7a5d
5 changed files with 41 additions and 21 deletions
|
|
@ -16,16 +16,29 @@ class DataController < ApplicationController
|
||||||
redirect_to :back
|
redirect_to :back
|
||||||
else
|
else
|
||||||
@import_to = params[:import_to]
|
@import_to = params[:import_to]
|
||||||
|
|
||||||
#get column headers and formart as [['name', column_number]...]
|
begin
|
||||||
i = -1
|
#get column headers and format as [['name', column_number]...]
|
||||||
@headers = import_headers(params[:file].path).collect { |v| [v, i+=1] }
|
i = -1
|
||||||
@headers.unshift ['',i]
|
@headers = import_headers(params[:file].path).collect { |v| [v, i+=1] }
|
||||||
|
@headers.unshift ['',i]
|
||||||
|
rescue Exception => e
|
||||||
|
flash[:error] = "Invalid CVS: could not read headers: #{e}"
|
||||||
|
redirect_to :back
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
#save file for later
|
#save file for later
|
||||||
directory = "public/uploads/csv"
|
begin
|
||||||
@path = File.join(directory, params[:file].original_filename)
|
uploaded_file = params[:file]
|
||||||
File.open(@path, "wb") { |f| f.write(params[:file].read) }
|
@filename = Tracks::Utils.sanitize_filename(uploaded_file.original_filename)
|
||||||
|
path_and_file = Rails.root.join('public', 'uploads', 'csv', @filename)
|
||||||
|
File.open(path_and_file, "wb") { |f| f.write(uploaded_file.read) }
|
||||||
|
rescue Exception => e
|
||||||
|
flash[:error] = "Could not save uploaded CSV (#{path_and_file}). Can Tracks write to the upload directory? #{e}"
|
||||||
|
redirect_to :back
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
case @import_to
|
case @import_to
|
||||||
when 'projects'
|
when 'projects'
|
||||||
|
|
@ -44,12 +57,14 @@ class DataController < ApplicationController
|
||||||
|
|
||||||
def csv_import
|
def csv_import
|
||||||
begin
|
begin
|
||||||
|
filename = Tracks::Utils.sanitize_filename(params[:file])
|
||||||
|
path_and_file = Rails.root.join('public', 'uploads', 'csv', filename)
|
||||||
case params[:import_to]
|
case params[:import_to]
|
||||||
when 'projects'
|
when 'projects'
|
||||||
count = Project.import params, current_user
|
count = Project.import path_and_file, params, current_user
|
||||||
flash[:notice] = "#{count} Projects imported"
|
flash[:notice] = "#{count} Projects imported"
|
||||||
when 'todos'
|
when 'todos'
|
||||||
count = Todo.import params, current_user
|
count = Todo.import path_and_file, params, current_user
|
||||||
flash[:notice] = "#{count} Todos imported"
|
flash[:notice] = "#{count} Todos imported"
|
||||||
else
|
else
|
||||||
flash[:error] = t('data.invalid_import_destination')
|
flash[:error] = t('data.invalid_import_destination')
|
||||||
|
|
@ -57,11 +72,11 @@ class DataController < ApplicationController
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
flash[:error] = t('data.invalid_import_destination') + ": #{e}"
|
flash[:error] = t('data.invalid_import_destination') + ": #{e}"
|
||||||
end
|
end
|
||||||
File.delete(params[:file])
|
File.delete(path_and_file)
|
||||||
redirect_to import_data_path
|
redirect_to import_data_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_headers file
|
def import_headers(file)
|
||||||
CSV.foreach(file, headers: false) do |row|
|
CSV.foreach(file, headers: false) do |row|
|
||||||
return row
|
return row
|
||||||
end
|
end
|
||||||
|
|
@ -105,13 +120,14 @@ class DataController < ApplicationController
|
||||||
send_data(result, :filename => "tracks_backup.yml", :type => 'text/plain')
|
send_data(result, :filename => "tracks_backup.yml", :type => 'text/plain')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# export all actions as csv
|
||||||
def csv_actions
|
def csv_actions
|
||||||
content_type = 'text/csv'
|
content_type = 'text/csv'
|
||||||
CSV::Writer.generate(result = "") do |csv|
|
CSV.generate(result = "") do |csv|
|
||||||
csv << ["id", "Context", "Project", "Description", "Notes", "Tags",
|
csv << ["id", "Context", "Project", "Description", "Notes", "Tags",
|
||||||
"Created at", "Due", "Completed at", "User ID", "Show from",
|
"Created at", "Due", "Completed at", "User ID", "Show from",
|
||||||
"state"]
|
"state"]
|
||||||
current_user.todos.include(:context, :project).all.each do |todo|
|
current_user.todos.includes(:context, :project, :taggings, :tags).each do |todo|
|
||||||
csv << [todo.id, todo.context.name,
|
csv << [todo.id, todo.context.name,
|
||||||
todo.project_id.nil? ? "" : todo.project.name,
|
todo.project_id.nil? ? "" : todo.project.name,
|
||||||
todo.description,
|
todo.description,
|
||||||
|
|
@ -127,7 +143,7 @@ class DataController < ApplicationController
|
||||||
send_data(result, :filename => "todos.csv", :type => content_type)
|
send_data(result, :filename => "todos.csv", :type => content_type)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# export all notes as csv
|
||||||
def csv_notes
|
def csv_notes
|
||||||
content_type = 'text/csv'
|
content_type = 'text/csv'
|
||||||
CSV.generate(result = "") do |csv|
|
CSV.generate(result = "") do |csv|
|
||||||
|
|
|
||||||
|
|
@ -138,9 +138,9 @@ class Project < ActiveRecord::Base
|
||||||
@age_in_days ||= ((Time.now.utc - created_at).to_i / 1.day) + 1
|
@age_in_days ||= ((Time.now.utc - created_at).to_i / 1.day) + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.import(params, user)
|
def self.import(filename, params, user)
|
||||||
count = 0
|
count = 0
|
||||||
CSV.foreach(params[:file], headers: true) do |row|
|
CSV.foreach(filename, headers: true) do |row|
|
||||||
unless find_by_name_and_user_id row[params[:name].to_i], user.id
|
unless find_by_name_and_user_id row[params[:name].to_i], user.id
|
||||||
project = new
|
project = new
|
||||||
project.name = row[params[:name].to_i]
|
project.name = row[params[:name].to_i]
|
||||||
|
|
|
||||||
|
|
@ -393,11 +393,11 @@ class Todo < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.import(params, user)
|
def self.import(filename, params, user)
|
||||||
default_context = Context.where(:user_id=>user.id).order('id').first
|
default_context = user.contexts.order('id').first
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
CSV.foreach(params[:file], headers: true) do |row|
|
CSV.foreach(filename, headers: true) do |row|
|
||||||
unless find_by_description_and_user_id row[params[:description].to_i], user.id
|
unless find_by_description_and_user_id row[params[:description].to_i], user.id
|
||||||
todo = new
|
todo = new
|
||||||
todo.user = user
|
todo.user = user
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
<%= select_tag(l, options_for_select(@headers) ) %>
|
<%= select_tag(l, options_for_select(@headers) ) %>
|
||||||
<br>
|
<br>
|
||||||
<% end %>
|
<% end %>
|
||||||
<%= hidden_field_tag :file, @path %>
|
<%= hidden_field_tag :file, @filename %>
|
||||||
<%= hidden_field_tag :import_to, @import_to %>
|
<%= hidden_field_tag :import_to, @import_to %>
|
||||||
<%= submit_tag "Import" %>
|
<%= submit_tag "Import" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
@ -38,6 +38,10 @@ module Tracks
|
||||||
RedCloth.new(text).to_html
|
RedCloth.new(text).to_html
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.sanitize_filename(filename)
|
||||||
|
filename.gsub(/[^0-9A-z.\-]/, '_')
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.helpers
|
def self.helpers
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue