<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -23,4 +23,5 @@ Use it like so:
 5. http://localhost:9292/admin/ (or whatever host:port) to add content.
 6. http://localhost:9292/admin/mypage edits a page that will be available at http://localhost:9292/mypage
 7. http://localhost:9292/admin/__all for a list of all pages
-8. http://localhost:9292/admin/__export to get a json dump of all pages
\ No newline at end of file
+8. http://localhost:9292/admin/__export to get a json dump of all pages
+9. http://localhost:9292/admin/__import to import one of those dumps
\ No newline at end of file</diff>
      <filename>README</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,5 @@
 require 'sinatra'
+require 'json'
 
 module Grat
   @@database_conf = {}</diff>
      <filename>lib/environment.rb</filename>
    </modified>
    <modified>
      <diff>@@ -35,6 +35,49 @@ class Grat::Application &lt; Sinatra::Base
     haml :import_form
   end
   
+  post '/admin/__import' do
+    json_text = file_import_text || params[:import][:text]
+    @import_results = import(json_text, params[:import][:strategy])
+    redirect '/admin/__all'
+  end
+  
+  # Rather inefficient at present.
+  def import(json_text, strategy)
+    json_to_import = JSON.parse json_text
+    if json_to_import.is_a? Array
+      model.delete_all if strategy == 'demolish'
+      json_to_import.each do |record|
+        puts &quot;Handling #{record['url']}&quot;
+        case strategy
+        when 'add'
+          import_record record
+        when 'replace'
+          import_record record, {:replace =&gt; true, :check=&gt; true}
+        when 'demolish'
+          import_record record, {:replace =&gt; true, :check =&gt; false}
+        end
+      end
+    end
+  end
+  
+  def import_record(hash, options = {:replace =&gt; false, :check =&gt; true})
+    record = model.find_by_url(hash['url']) if options[:check]
+    if record &amp;&amp; options[:replace]
+      puts &quot;Deleting #{hash['url']}&quot;
+      record.destroy if record
+    end
+    unless record &amp;&amp; !options[:replace]
+      puts &quot;Creating #{hash['url']}&quot;
+      model.create hash
+    end
+  end
+  
+  def file_import_text
+    if params[:import] &amp;&amp; params[:import][:file] &amp;&amp; params[:import][:file][:tempfile]
+      params[:import][:file][:tempfile].read
+    end
+  end
+  
   get '/admin/*' do
     haml :content_form
   end</diff>
      <filename>lib/grat.rb</filename>
    </modified>
    <modified>
      <diff>@@ -51,7 +51,7 @@ class Grat::Content
   end
   
   def template_url=(var)
-    super(var) unless var.empty?
+    super(var) unless var.nil? || var.empty?
   end
   
   def demo_string
@@ -75,9 +75,9 @@ class Grat::Content
     while problem_var = detect_problem_var
       counter += 1
       return false if counter &gt; 200
-      (detect_problem_var(problem_var =&gt; demo_string) || 
+      (detect_problem_var(problem_var =&gt; demo_string) ||
         default_content_vars.merge!(problem_var =&gt; demo_string)) or
-      (detect_problem_var(problem_var =&gt; demo_array) || 
+      (detect_problem_var(problem_var =&gt; demo_array) ||
         default_content_vars.merge!(problem_var =&gt; demo_string)) or
       raise &quot;Don't know how to reconcile.&quot;
     end</diff>
      <filename>lib/grat/content.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,25 +1,24 @@
 .grat_greet
   %p Hey, import some json if you like.
-%form{:action =&gt; &quot;/admin__import&quot;, :method =&gt; 'post', :enctype=&gt;'multipart/form-data' }
+%form{:action =&gt; &quot;/admin/__import&quot;, :method =&gt; 'post', :enctype=&gt;'multipart/form-data' }
   .import_file
     %label{:for =&gt; 'import_file'} Upload a json file:
-    %input{:id =&gt; 'import_file',:type =&gt; 'file', :name =&gt; 'import[:file]'}
+    %input{:id =&gt; 'import_file', :type =&gt; 'file', :name =&gt; 'import[file]'}
   %p
     %em or
   .import_text
     %label{:for =&gt; 'import_text'} Paste some json here:
-    %textarea{:id =&gt; 'import_text', :name =&gt; 'import[:text]'}
+    %textarea{:id =&gt; 'import_text', :name =&gt; 'import[text]'}
   .import_replace_or_add
-    / &lt;label for=&quot;r1&quot;&gt;&lt;input type=&quot;radio&quot; name=&quot;group1&quot; id=&quot;r1&quot; value=&quot;1&quot; /&gt; button one&lt;/label&gt;
     .add
       %label{:for =&gt; 'import_add'} Add new records to database, ignore existing records.
-      %input{:type=&gt; 'radio', :id =&gt; 'import_add', :value =&gt; 'add', :name =&gt; 'import[setting]', :checked =&gt; 'checked'}
+      %input{:type=&gt; 'radio', :id =&gt; 'import_add', :value =&gt; 'add', :name =&gt; 'import[strategy]', :checked =&gt; 'checked'}
     .replace
       %label{:for =&gt; 'import_replace'} Add new and replace matching records. (danger!)
-      %input{:type=&gt; 'radio', :id =&gt; 'import_replace', :value =&gt; 'replace', :name =&gt; 'import[setting]'}
+      %input{:type=&gt; 'radio', :id =&gt; 'import_replace', :value =&gt; 'replace', :name =&gt; 'import[strategy]'}
     .demolish
       %label{:for =&gt; 'import_demolish'} Demolish all existing records, then import. (omg danger!)
-      %input{:type=&gt; 'radio', :id =&gt; 'import_demolish', :value =&gt; 'demolish', :name =&gt; 'import[setting]'}
+      %input{:type=&gt; 'radio', :id =&gt; 'import_demolish', :value =&gt; 'demolish', :name =&gt; 'import[strategy]'}
   .submit
     %input{:name =&gt; 'import[submit]', :type =&gt; 'submit', :value =&gt; &quot;Let's save this thing&quot;}
     
\ No newline at end of file</diff>
      <filename>views/import_form.haml</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>cd744c768d64058875d2767151aed2132d803316</id>
    </parent>
  </parents>
  <author>
    <name>Sam Schenkman-Moore</name>
    <email>samsm@samsm.com</email>
  </author>
  <url>http://github.com/samsm/Grat/commit/dcb4f0c17e9d8d0443cab6a21056a93225a740e1</url>
  <id>dcb4f0c17e9d8d0443cab6a21056a93225a740e1</id>
  <committed-date>2009-11-09T13:44:25-08:00</committed-date>
  <authored-date>2009-11-09T13:44:25-08:00</authored-date>
  <message>Import json file to populate content.</message>
  <tree>c5870c9124fa2121fb05739b6adc367ed2e29fb8</tree>
  <committer>
    <name>Sam Schenkman-Moore</name>
    <email>samsm@samsm.com</email>
  </committer>
</commit>
