Skip to content

Commit

Permalink
beginnings of import GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
smparkes committed May 17, 2009
1 parent 14d1264 commit 72e12fc
Show file tree
Hide file tree
Showing 21 changed files with 474 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/controllers/import_controller.rb
@@ -0,0 +1,19 @@
class ImportController < ApplicationController

require_role [ :admin ]

def create
action = params[:commit]

case action
when "Edit"
redirect_to edit_import_map_path( params[:map] )
when "New";
redirect_to new_import_map_path
when "import";
else render_nothing
end

end

end
63 changes: 63 additions & 0 deletions app/controllers/import_maps_controller.rb
@@ -0,0 +1,63 @@
class ImportMapsController < ApplicationController

def yml
@import_map = ImportMap.find(params[:id])
end

def new
@verb = "New"
@import_map = ImportMap.new
render "form"
end

def edit
@verb = "Edit"
@import_map = ImportMap.find(params[:id])
@yml = YAML::load( @import_map.yml )
render "form"
end

# POST /import_maps
# POST /import_maps.xml
def _create
@import_map = ImportMap.new(params[:import_map])

respond_to do |format|
if @import_map.save
flash[:notice] = 'ImportMap was successfully created.'
format.html { redirect_to(@import_map) }
format.xml { render :xml => @import_map, :status => :created, :location => @import_map }
else
format.html { render :action => "new" }
format.xml { render :xml => @import_map.errors, :status => :unprocessable_entity }
end
end
end

def update
@import_map = ImportMap.find(params[:id])
if @import_map.update_attributes(params[:import_map])
flash[:notice] = 'ImportMap was successfully updated.'
redirect_to edit_import_map_path @import_map
else
if params[:import_map][:yml]
render :action => "yml"
else
raise "hell"
render :action => "edit"
end
end
end

# DELETE /import_maps/1
# DELETE /import_maps/1.xml
def _destroy
@import_map = ImportMap.find(params[:id])
@import_map.destroy

respond_to do |format|
format.html { redirect_to(import_maps_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/import_helper.rb
@@ -0,0 +1,2 @@
module ImportHelper
end
45 changes: 45 additions & 0 deletions app/helpers/import_maps_helper.rb
@@ -0,0 +1,45 @@
module ImportMapsHelper

def fields
@fields ||= begin
[ "not mapped" ] +
(( %w(title sentence abstract duration local_id donor
alternative_title series_title audience classification
language_note creation_credits participation_credits
preservation_note transcript notes public tags Asset) +
PropertyType.find(:all).to_a.map(&:name) ).
sort {|a,b| a.downcase <=> b.downcase || a <=> b })

end
end

def primary value
case value
when Hash; value = value.keys[0]
end
value
end

def secondary value
case value
when Hash; value.values[0]
else; nil
end
end

def field_options
fields.map do |field|
[ field, field ]
end
end

def tertiary value
set = PropertyType.find_by_name value
if set
set.values.map(&:to_s).sort {|a,b| a.downcase <=> b.downcase || a <=> b }
else
raise "#{value.inspect}"
end
end

end
11 changes: 11 additions & 0 deletions app/models/import_map.rb
@@ -0,0 +1,11 @@
class ImportMap < ActiveRecord::Base

validate do |map|
begin
YAML.load StringIO.new( map.yml )
rescue Exception => e
map.errors.add :yml, e.message
end
end

end
Empty file.
25 changes: 25 additions & 0 deletions app/views/import/show.html.haml
@@ -0,0 +1,25 @@
- form_tag( { :action => "create" }, { :multipart => true } ) do

%fieldset.import
%legend Import Videos

%div
%label
Select Import Map:
%select{:name => "map"}
= options_for_select |
( ImportMap.find(:all).to_a.map { |im| [ im.name, im.id ] } ) |
= submit_tag "Edit"
= submit_tag "New"

%br

%div
%label
Select CSV File:
= file_field_tag "csv"

%br

%div
= submit_tag "import"
21 changes: 21 additions & 0 deletions app/views/import_maps/_map_entry.html.haml
@@ -0,0 +1,21 @@
- ( key, value, set ) = map_entry
- set ||= field_options
%div
%label
%span.key{:style=>"display:inline-block;width:10em;"}
= "#{key}: "
%span.value
%select{:name => key}
= options_for_select set, primary(value)
/ = debug( secondary(value) )
- if sec = secondary(value) and |
primary(value) != "tags" and |
primary(value) != "public" |
%div{:style=>"margin-left: 2em"}
- keys = sec.keys.sort {|a,b| a.downcase <=> b.downcase || a <=> b }
- keys.each do |k|
- v = sec[k]
= render :partial => "map_entry", |
:object => [k, v, tertiary(primary(value) ) ] |


42 changes: 42 additions & 0 deletions app/views/import_maps/form.html.haml
@@ -0,0 +1,42 @@
%fieldset.import_map

%legend
= @verb
Import Map

- form_for template_import_map_path( @import_map ), |
{ :multipart => true } do |
-
%div{:style=>"text-align:center;width:100%"}
%div
%label
Upload template CSV File:
= file_field_tag "csv"
= submit_tag "upload"

%br

%hr{:style=>'width:75%;margin:0 auto 0 auto'}

%br

= link_to "Edit YAML", yml_import_map_path( @import_map )

%br
%br

%hr{:style=>'width:75%;margin:0 auto 0 auto'}

%br

- form_for @import_map, { :multipart => true } do

%div{:style => "margin-left: 5em;"}
- keys = @yml.keys.sort {|a,b| a.downcase <=> b.downcase || a <=> b }
- keys.each do |k|
- v = @yml[k]
= render :partial => "map_entry", :object => [k, v, nil]

%div
= submit_tag "save"
= link_to "cancel", import_path
15 changes: 15 additions & 0 deletions app/views/import_maps/yml.html.haml
@@ -0,0 +1,15 @@
- if !(errors = error_message_on @import_map, :yml).blank?
%div.flash.errors{:style=>"color:red"}
= errors

- form_for @import_map do |form|

%div{:style=>"width:100%;text-align:center;"}
= form.text_area :yml, :rows => 50, |
:style=>"width:80%;font-size:2em;" |

%br
%br

= form.submit "Save"
= link_to "Cancel", edit_import_map_path( @import_map )
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.haml
Expand Up @@ -35,6 +35,8 @@
- if current_user and current_user.has_role?( "admin" )
.admin
.button= link_to "Administration", library_path
.admin
.button= link_to "Import", import_path
.clear

#header
Expand Down
7 changes: 7 additions & 0 deletions config/routes.rb
Expand Up @@ -48,6 +48,13 @@
}


map.resource :import, :controller => :import

map.resources :import_maps,
:member => { :yml => :get,
:save_yml => :post,
:template => :post }

map.resource :my, :controller => :my,
:member => { :home => :get,
:favorites => :get,
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20090517185624_create_import_maps.rb
@@ -0,0 +1,15 @@
class CreateImportMaps < ActiveRecord::Migration
def self.up
create_table :import_maps do |t|
t.string :name, :null => false
t.text :yml, :null => false
t.timestamps
end

add_index :import_maps, :name, :unique => true
end

def self.down
drop_table :import_maps
end
end
13 changes: 13 additions & 0 deletions doc/import/samples/openvideo_source_10-21-2008/fixture.rb
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby

require 'yaml'

File.open( "openvideo_source_10-21-2008_vid-Table 1.map" ) do |f|
yml = f.read
hash = { "open_video" => { "name" => "Open Video Test Map",
"yml" => yml } }

File.open( "fixture.yml", "w" ) do |w|
YAML::dump hash, w
end
end
65 changes: 65 additions & 0 deletions doc/import/samples/openvideo_source_10-21-2008/fixture.yml
@@ -0,0 +1,65 @@
---
open_video:
name: Open Video Test Map
yml: |
videoid: local_id
asset: Asset
locationurl: nil
title: title
description: abstract
digitizationdate: Digitization
amtMotion: nil
transcriptText: nil
statsText: nil
numberOfFrames: nil
verified?:
public:
"Yes": true
"No": false
timestamp: nil
keywords:
tags: Tag
posterframe: nil
duration: duration
restricted?: nil
creationdate: Creation
framerate: nil
sound:
Sound:
"Yes": Sound
"No": Silent
color:
Color:
"Yes": Color
"No": Black and White
edited?: nil
segmentation: nil
sponsorname: nil
contribOrgID: nil
languageid:
Language:
"1": English
frameDimensionID: nil
digitizOrgID: nil
genrePrimaryID:
Genre:
"1": Documentary
"2": Corporate
"3": Historical
"4": Ephemeral
"5": Lecture
genreSecondaryID:
Genre:
"1": Documentary
"2": Corporate
"3": Historical
"4": Ephemeral
"5": Lecture
RightsInformationID:
Rights Statement:
"1": All Rights Reserved
# Local Variables:
# mode:yaml
# End:
10 changes: 10 additions & 0 deletions spec/controllers/import_controller_spec.rb
@@ -0,0 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe ImportController do

#Delete this example and add some real ones
it "should use ImportController" do
controller.should be_an_instance_of(ImportController)
end

end
4 changes: 4 additions & 0 deletions spec/controllers/import_maps_controller_spec.rb
@@ -0,0 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe ImportMapsController do
end

0 comments on commit 72e12fc

Please sign in to comment.