public
Description: Tracks is a GTD(TM) web application, built with Ruby on Rails
Homepage: http://www.rousette.org.uk/projects/
Clone URL: git://github.com/bsag/tracks.git
Click here to lend your support to: tracks and make a donation at www.pledgie.com !
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.
Jakub A.Tesinsky (author)
Thu Sep 04 18:25:56 -0700 2008
commit  229a1144413fcf5633730d75cbe61c9314138ab6
tree    76eeff3b072ea25557f83039a26b5703ca545355
parent  a4cb8fb113b11ae9cb934ed6b9228c254e1d5285
...
88
89
90
 
 
 
 
 
 
 
 
 
91
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
94
95
...
88
89
90
91
92
93
94
95
96
97
98
99
100
 
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
0
@@ -88,8 +88,115 @@ class DataController < ApplicationController
0
     # Draw the form to input the YAML text data
0
   end
0
 
0
+  # adjusts time to utc
0
+  def adjust_time(timestring)
0
+    if (timestring=='') or ( timestring == nil)
0
+      return nil
0
+    else 
0
+      return Time.parse(timestring + 'UTC')
0
+    end
0
+  end
0
+
0
   def yaml_import
0
-    # Logic to load the YAML text file and create new records from data
0
+    @errmessage = ''
0
+    @inarray = YAML::load(params['import']['yaml'])
0
+    # arrays to handle id translations
0
+
0
+
0
+    # contexts
0
+    translate_context = Hash.new
0
+    translate_context[nil] = nil
0
+    current_user.contexts.each { |context| context.destroy }
0
+    @inarray['contexts'].each { | item |
0
+      newitem = Context.new(item.ivars['attributes'])
0
+      newitem.user_id = current_user.id
0
+      newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
0
+      newitem.save(false)
0
+      translate_context[item.ivars['attributes']['id'].to_i] = newitem.id
0
+    }
0
+
0
+    # projects
0
+    translate_project = Hash.new
0
+    translate_project[nil] = nil
0
+    current_user.projects.each { |item| item.destroy }
0
+    @inarray['projects'].each { |item|
0
+      newitem = Project.new(item.ivars['attributes'])
0
+      # ids
0
+      newitem.user_id = current_user.id
0
+      newitem.default_context_id = translate_context[newitem.default_context_id]
0
+      newitem.save(false)
0
+      translate_project[item.ivars['attributes']['id'].to_i] = newitem.id
0
+
0
+      # state + dates
0
+      newitem.transition_to(item.ivars['attributes']['state'])
0
+      newitem.completed_at = adjust_time(item.ivars['attributes']['completed_at'])
0
+      newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
0
+      newitem.position = item.ivars['attributes']['position']
0
+      newitem.save(false)
0
+    }
0
+
0
+    # todos
0
+    translate_todo = Hash.new
0
+    translate_todo[nil] = nil
0
+    current_user.todos.each { |item| item.destroy }
0
+    @inarray['todos'].each { |item|
0
+      newitem = Todo.new(item.ivars['attributes'])
0
+      # ids
0
+      newitem.user_id = current_user.id
0
+      newitem.context_id = translate_context[newitem.context_id]
0
+      newitem.project_id = translate_project[newitem.project_id]
0
+      # TODO: vyresit recurring_todo_id
0
+      newitem.save(false)
0
+      translate_todo[item.ivars['attributes']['id'].to_i] = newitem.id
0
+
0
+      # state + dates
0
+      case item.ivars['attributes']['state']
0
+        when 'active' : newitem.activate!
0
+        when 'project_hidden' : newitem.hide!
0
+        when 'completed' 
0
+          newitem.complete!
0
+          newitem.completed_at = adjust_time(item.ivars['attributes']['completed_at'])
0
+        when 'deferred' : newitem.defer!
0
+      end
0
+      newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
0
+      newitem.save(false)
0
+    }
0
+
0
+    #tags
0
+    translate_tag = Hash.new
0
+    translate_tag[nil] = nil
0
+    current_user.tags.each { |item| item.destroy }
0
+    @inarray['tags'].each { |item|
0
+      newitem = Tag.new(item.ivars['attributes'])
0
+      newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
0
+      newitem.save
0
+      translate_tag[item.ivars['attributes']['id'].to_i] = newitem.id
0
+    }
0
+
0
+    # taggings
0
+    current_user.taggings.each { |item| item.destroy }
0
+    @inarray['taggings'].each { |item|
0
+      newitem = Tagging.new(item.ivars['attributes'])
0
+      newitem.user_id = current_user.id
0
+      newitem.tag_id = translate_tag[newitem.tag_id]
0
+      case newitem.taggable_type
0
+        when 'Todo' : newitem.taggable_id = translate_todo[newitem.taggable_id]
0
+        else newitem.taggable_id = 0
0
+      end
0
+      newitem.save
0
+    }
0
+
0
+    # notes
0
+    current_user.notes.each { |item| item.destroy }
0
+    @inarray['notes'].each { |item|
0
+      newitem = Note.new(item.ivars['attributes'])
0
+      newitem.id = item.ivars['attributes']['id']
0
+      newitem.user_id = current_user.id
0
+      newitem.project_id = translate_project[newitem.project_id]
0
+      newitem.created_at = adjust_time(item.ivars['attributes']['created_at'])
0
+      newitem.save
0
+    }
0
+
0
   end
0
  
0
 end
...
3
4
5
6
 
7
8
9
...
33
34
35
36
37
38
 
 
 
 
 
 
 
 
 
...
3
4
5
 
6
7
8
9
...
33
34
35
 
 
36
37
38
39
40
41
42
43
44
45
0
@@ -3,7 +3,7 @@
0
     <h3>Exporting data</h3>
0
     <p>You can choose between the following formats:</p>
0
     <ul>
0
-            <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>
0
+            <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>
0
       <li><strong>CSV: </strong>Best for importing into spreadsheet or data analysis software</li>
0
       <li><strong>XML: </strong>Best for importing or repurposing the data</li>
0
           </ul>
0
@@ -33,5 +33,12 @@
0
 </tr>
0
 </table>
0
 </p>
0
-  
0
-</div><!-- End of feeds -->
0
\ No newline at end of file
0
+
0
+<div id="feeds">
0
+  <div id="feedlegend">
0
+   <h3>Importing data</h3>
0
+<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>
0
+<p><%= link_to "Start import", :controller => 'data', :action => 'yaml_form' %>.</p>
0
+</div>
0
+
0
+</div><!-- End of feeds -->
...
1
2
3
4
 
 
 
 
5
6
7
8
9
10
11
12
13
14
 
 
 
 
 
 
 
15
16
17
18
19
20
 
...
1
 
 
 
2
3
4
5
6
 
 
 
 
 
 
 
 
 
7
8
9
10
11
12
13
14
15
16
 
 
17
18
0
@@ -1,19 +1,17 @@
0
 <div id="display_box">
0
-  <div id="feeds">
0
-  <div id="feedlegend">
0
-    <p>Paste the contents of the YAML file you exported into the text box below:</p>
0
+ <div id="feeds">
0
+  <div id="feedlegend">
0
+   <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>
0
+   <p>Paste the contents of the YAML file you exported into the text box below:</p>
0
   </div>
0
-
0
-<p>
0
-<% form_for :import, @import, :url => {:controller => 'data', :action => 'yaml_import'} do |f| %>
0
-  <%= f.text_area :yaml %><br />
0
-  <input type="submit" value="Import data">
0
-<% end %>
0
-</p>
0
-
0
-  </div><!-- End of feeds -->
0
+  <p>
0
+  <% form_for :import, @import, :url => {:controller => 'data', :action => 'yaml_import'} do |f| %>
0
+   <%= f.text_area :yaml %><br />
0
+   <input type="submit" value="Import data">
0
+  <% end %>
0
+  </p>
0
+ </div><!-- End of feeds -->
0
 </div><!-- End of display_box -->
0
 
0
 <div id="input_box">
0
-
0
-</div><!-- End of input box -->
0
\ No newline at end of file
0
+</div><!-- End of input box -->
...
1
2
 
 
 
 
 
 
 
...
 
1
2
3
4
5
6
7
8
0
@@ -1 +1,7 @@
0
-<p>Import was successful</p>
0
\ No newline at end of file
0
+<% if !(@errmessage == '') %>
0
+  <p>There were these errors:
0
+  <pre><%= @errmessage  %></pre>
0
+  </p>
0
+<% else %>
0
+  <p>Import was successful.</p>
0
+<% end %>
...
31
32
33
34
 
35
36
37
...
31
32
33
 
34
35
36
37
0
@@ -31,7 +31,7 @@ RewriteEngine On
0
 RewriteRule ^$ index.html [QSA]
0
 RewriteRule ^([^.]+)$ $1.html [QSA]
0
 RewriteCond %{REQUEST_FILENAME} !-f
0
-RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
0
+RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
0
 
0
 # In case Rails experiences terminal errors
0
 # Instead of displaying this message you can supply a file here which will be rendered instead

Comments