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.
This commit is contained in:
Jakub A.Tesinsky 2008-09-05 03:25:56 +02:00
parent a4cb8fb113
commit 229a114441
5 changed files with 138 additions and 20 deletions

View file

@ -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

View file

@ -3,7 +3,7 @@
<h3>Exporting data</h3>
<p>You can choose between the following formats:</p>
<ul>
<li><strong>YAML: </strong>Best for exporting data. <br/><i>Please note that Tracks currently does not support importing YAML files. Do not use this (yet) to backup your Tracks database.</i></li>
<li><strong>YAML: </strong>Best for exporting data. <br/><i>Please note that importing YAML files is currently supported only in experimentally. Do not rely on it for backing up critical data.</i></li>
<li><strong>CSV: </strong>Best for importing into spreadsheet or data analysis software</li>
<li><strong>XML: </strong>Best for importing or repurposing the data</li>
</ul>
@ -33,5 +33,12 @@
</tr>
</table>
</p>
</div><!-- End of feeds -->
<div id="feeds">
<div id="feedlegend">
<h3>Importing data</h3>
<p>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.</p>
<p><%= link_to "Start import", :controller => 'data', :action => 'yaml_form' %>.</p>
</div>
</div><!-- End of feeds -->

View file

@ -1,19 +1,17 @@
<div id="display_box">
<div id="feeds">
<div id="feedlegend">
<p>Paste the contents of the YAML file you exported into the text box below:</p>
<div id="feeds">
<div id="feedlegend">
<p><b>Beware</b>: 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.</p>
<p>Paste the contents of the YAML file you exported into the text box below:</p>
</div>
<p>
<% form_for :import, @import, :url => {:controller => 'data', :action => 'yaml_import'} do |f| %>
<%= f.text_area :yaml %><br />
<input type="submit" value="Import data">
<% end %>
</p>
</div><!-- End of feeds -->
<p>
<% form_for :import, @import, :url => {:controller => 'data', :action => 'yaml_import'} do |f| %>
<%= f.text_area :yaml %><br />
<input type="submit" value="Import data">
<% end %>
</p>
</div><!-- End of feeds -->
</div><!-- End of display_box -->
<div id="input_box">
</div><!-- End of input box -->
</div><!-- End of input box -->

View file

@ -1 +1,7 @@
<p>Import was successful</p>
<% if !(@errmessage == '') %>
<p>There were these errors:
<pre><%= @errmessage %></pre>
</p>
<% else %>
<p>Import was successful.</p>
<% end %>

View file

@ -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