From 229a1144413fcf5633730d75cbe61c9314138ab6 Mon Sep 17 00:00:00 2001 From: "Jakub A.Tesinsky" Date: Fri, 5 Sep 2008 03:25:56 +0200 Subject: [PATCH] mport feature added. Everything gets imported except updated_at fields. IDs of objects are not copied, but created as new so there could be no conflict with existing objects. --- app/controllers/data_controller.rb | 109 +++++++++++++++++++++++++++- app/views/data/index.html.erb | 13 +++- app/views/data/yaml_form.html.erb | 26 +++---- app/views/data/yaml_import.html.erb | 8 +- public/.htaccess | 2 +- 5 files changed, 138 insertions(+), 20 deletions(-) diff --git a/app/controllers/data_controller.rb b/app/controllers/data_controller.rb index a0514945..47a85fd6 100644 --- a/app/controllers/data_controller.rb +++ b/app/controllers/data_controller.rb @@ -88,8 +88,115 @@ class DataController < ApplicationController # Draw the form to input the YAML text data end + # adjusts time to utc + def adjust_time(timestring) + if (timestring=='') or ( timestring == nil) + return nil + else + return Time.parse(timestring + 'UTC') + end + end + def yaml_import - # Logic to load the YAML text file and create new records from data + @errmessage = '' + @inarray = YAML::load(params['import']['yaml']) + # arrays to handle id translations + + + # contexts + translate_context = Hash.new + translate_context[nil] = nil + current_user.contexts.each { |context| context.destroy } + @inarray['contexts'].each { | item | + newitem = Context.new(item.ivars['attributes']) + newitem.user_id = current_user.id + newitem.created_at = adjust_time(item.ivars['attributes']['created_at']) + newitem.save(false) + translate_context[item.ivars['attributes']['id'].to_i] = newitem.id + } + + # projects + translate_project = Hash.new + translate_project[nil] = nil + current_user.projects.each { |item| item.destroy } + @inarray['projects'].each { |item| + newitem = Project.new(item.ivars['attributes']) + # ids + newitem.user_id = current_user.id + newitem.default_context_id = translate_context[newitem.default_context_id] + newitem.save(false) + translate_project[item.ivars['attributes']['id'].to_i] = newitem.id + + # state + dates + newitem.transition_to(item.ivars['attributes']['state']) + newitem.completed_at = adjust_time(item.ivars['attributes']['completed_at']) + newitem.created_at = adjust_time(item.ivars['attributes']['created_at']) + newitem.position = item.ivars['attributes']['position'] + newitem.save(false) + } + + # todos + translate_todo = Hash.new + translate_todo[nil] = nil + current_user.todos.each { |item| item.destroy } + @inarray['todos'].each { |item| + newitem = Todo.new(item.ivars['attributes']) + # ids + newitem.user_id = current_user.id + newitem.context_id = translate_context[newitem.context_id] + newitem.project_id = translate_project[newitem.project_id] + # TODO: vyresit recurring_todo_id + newitem.save(false) + translate_todo[item.ivars['attributes']['id'].to_i] = newitem.id + + # state + dates + case item.ivars['attributes']['state'] + when 'active' : newitem.activate! + when 'project_hidden' : newitem.hide! + when 'completed' + newitem.complete! + newitem.completed_at = adjust_time(item.ivars['attributes']['completed_at']) + when 'deferred' : newitem.defer! + end + newitem.created_at = adjust_time(item.ivars['attributes']['created_at']) + newitem.save(false) + } + + #tags + translate_tag = Hash.new + translate_tag[nil] = nil + current_user.tags.each { |item| item.destroy } + @inarray['tags'].each { |item| + newitem = Tag.new(item.ivars['attributes']) + newitem.created_at = adjust_time(item.ivars['attributes']['created_at']) + newitem.save + translate_tag[item.ivars['attributes']['id'].to_i] = newitem.id + } + + # taggings + current_user.taggings.each { |item| item.destroy } + @inarray['taggings'].each { |item| + newitem = Tagging.new(item.ivars['attributes']) + newitem.user_id = current_user.id + newitem.tag_id = translate_tag[newitem.tag_id] + case newitem.taggable_type + when 'Todo' : newitem.taggable_id = translate_todo[newitem.taggable_id] + else newitem.taggable_id = 0 + end + newitem.save + } + + # notes + current_user.notes.each { |item| item.destroy } + @inarray['notes'].each { |item| + newitem = Note.new(item.ivars['attributes']) + newitem.id = item.ivars['attributes']['id'] + newitem.user_id = current_user.id + newitem.project_id = translate_project[newitem.project_id] + newitem.created_at = adjust_time(item.ivars['attributes']['created_at']) + newitem.save + } + end end diff --git a/app/views/data/index.html.erb b/app/views/data/index.html.erb index 5fb93294..8f7aec2c 100644 --- a/app/views/data/index.html.erb +++ b/app/views/data/index.html.erb @@ -3,7 +3,7 @@

Exporting data

You can choose between the following formats:

@@ -33,5 +33,12 @@

- - \ No newline at end of file + +
+
+

Importing data

+

Curently there is a experimental support for importing YAML files. Beware: all your current data will be destroyed before importing the YAML file, so if you have access to the database, we strongly reccoment backing up the database right now in case that anything goes wrong.

+

<%= link_to "Start import", :controller => 'data', :action => 'yaml_form' %>.

+
+ +
diff --git a/app/views/data/yaml_form.html.erb b/app/views/data/yaml_form.html.erb index 444613d7..d3be8c1e 100644 --- a/app/views/data/yaml_form.html.erb +++ b/app/views/data/yaml_form.html.erb @@ -1,19 +1,17 @@
-
-
-

Paste the contents of the YAML file you exported into the text box below:

+
+
+

Beware: all your current data will be destroyed before importing the YAML file, so if you have access to the database, we strongly reccoment backing up the database right now in case that anything goes wrong.

+

Paste the contents of the YAML file you exported into the text box below:

- -

-<% form_for :import, @import, :url => {:controller => 'data', :action => 'yaml_import'} do |f| %> - <%= f.text_area :yaml %>
- -<% end %> -

- -
+

+ <% form_for :import, @import, :url => {:controller => 'data', :action => 'yaml_import'} do |f| %> + <%= f.text_area :yaml %>
+ + <% end %> +

+
- -
\ No newline at end of file +
diff --git a/app/views/data/yaml_import.html.erb b/app/views/data/yaml_import.html.erb index cf79230b..2e9fcd5f 100644 --- a/app/views/data/yaml_import.html.erb +++ b/app/views/data/yaml_import.html.erb @@ -1 +1,7 @@ -

Import was successful

\ No newline at end of file +<% if !(@errmessage == '') %> +

There were these errors: +

<%= @errmessage  %>
+

+<% else %> +

Import was successful.

+<% end %> diff --git a/public/.htaccess b/public/.htaccess index d579b5cc..3b66fccd 100644 --- a/public/.htaccess +++ b/public/.htaccess @@ -31,7 +31,7 @@ RewriteEngine On RewriteRule ^$ index.html [QSA] RewriteRule ^([^.]+)$ $1.html [QSA] RewriteCond %{REQUEST_FILENAME} !-f -RewriteRule ^(.*)$ dispatch.cgi [QSA,L] +RewriteRule ^(.*)$ dispatch.fcgi [QSA,L] # In case Rails experiences terminal errors # Instead of displaying this message you can supply a file here which will be rendered instead