Commit e66821bd403bdc86d8605687835407d25b183575

Authored by Marcos Pereira
1 parent 82f42c57

Improve import_sisp task

- Acronym is not mandatory for institution anymore

Signed-off-by: Arthur Del Esposte<arthurmde@gmail.com>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
src/noosfero-spb/gov_user/lib/public_institution.rb
@@ -2,9 +2,6 @@ class PublicInstitution &lt; Institution @@ -2,9 +2,6 @@ class PublicInstitution &lt; Institution
2 validates :governmental_power, :governmental_sphere, :juridical_nature, 2 validates :governmental_power, :governmental_sphere, :juridical_nature,
3 :presence=>true 3 :presence=>true
4 4
5 - validates :acronym, :allow_blank => true, :allow_nil => true,  
6 - :uniqueness=>true  
7 -  
8 validates_format_of( 5 validates_format_of(
9 :cnpj, 6 :cnpj,
10 :with => /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/, 7 :with => /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/,
src/noosfero-spb/software_communities/lib/tasks/import_sisp_software.rake
@@ -15,12 +15,14 @@ namespace :software do @@ -15,12 +15,14 @@ namespace :software do
15 end 15 end
16 16
17 desc "Import sisp software from yml" 17 desc "Import sisp software from yml"
18 - task :import_sisp_from_yml => :environment do  
19 - $imported_data = YAML.load_file('script/sisp-catalogo.yml')  
20 - $env = Environment.default 18 + task :import_sisp => :environment do
  19 +
  20 + $imported_data = YAML.load_file('script/sisp-catalogo.yml') #TODO change yml folder
  21 + $env = Environment.default #TODO change this to correct environment
21 22
22 $software_category = Category.find_by_name("Software") 23 $software_category = Category.find_by_name("Software")
23 $software_category ||= Category.create(:name => "Software", :environment => $env) 24 $software_category ||= Category.create(:name => "Software", :environment => $env)
  25 +
24 $sisp_user = create_sisp_user 26 $sisp_user = create_sisp_user
25 27
26 $created_software={} 28 $created_software={}
@@ -30,6 +32,7 @@ namespace :software do @@ -30,6 +32,7 @@ namespace :software do
30 32
31 sisp = $imported_data[key]['software_info'] 33 sisp = $imported_data[key]['software_info']
32 34
  35 + next if sisp['3 - Identificação do software']['Nome'].size <= 2
33 sw = create_software_and_attrs sisp 36 sw = create_software_and_attrs sisp
34 37
35 sw.sisp_url = $imported_data[key]['url'] 38 sw.sisp_url = $imported_data[key]['url']
@@ -39,8 +42,8 @@ namespace :software do @@ -39,8 +42,8 @@ namespace :software do
39 set_sisp_hashes sw, sisp 42 set_sisp_hashes sw, sisp
40 43
41 if sw.valid? && sw.community.valid? 44 if sw.valid? && sw.community.valid?
42 - sw.community.save  
43 - sw.save 45 + sw.community.save!
  46 + sw.save!
44 print '.' 47 print '.'
45 else 48 else
46 puts sw.errors.full_messages 49 puts sw.errors.full_messages
@@ -49,21 +52,28 @@ namespace :software do @@ -49,21 +52,28 @@ namespace :software do
49 end 52 end
50 53
51 puts "\n Done" 54 puts "\n Done"
  55 + return
52 end 56 end
53 end 57 end
54 58
55 def create_community name 59 def create_community name
56 - community = Community.new  
57 - community.name = name 60 +
58 identifier = create_identifier name 61 identifier = create_identifier name
59 62
60 $created_software[identifier]= $created_software[identifier].nil? ? 0 : $created_software[identifier]+1 63 $created_software[identifier]= $created_software[identifier].nil? ? 0 : $created_software[identifier]+1
  64 +
  65 + name = name + " copy#{$created_software[identifier]}" if ($created_software[identifier] != 0)
61 identifier = (identifier + "-copy#{$created_software[identifier]}") if ($created_software[identifier] != 0) 66 identifier = (identifier + "-copy#{$created_software[identifier]}") if ($created_software[identifier] != 0)
62 67
63 - #TODO remove  
64 - p "created new: #{identifier}" 68 + community = Community.find_or_initialize_by_name(name)
65 69
66 community.identifier = identifier 70 community.identifier = identifier
  71 +
  72 + #p "achei? #{!community.id.nil?}"
  73 + #p "nome: #{name}"
  74 + #p "iden: #{identifier}"
  75 + #p " "
  76 + community.save!
67 community 77 community
68 end 78 end
69 79
@@ -73,8 +83,7 @@ def create_software_info name, finality = &quot;blank&quot;, acronym = &quot;&quot; @@ -73,8 +83,7 @@ def create_software_info name, finality = &quot;blank&quot;, acronym = &quot;&quot;
73 software_info.community = community 83 software_info.community = community
74 software_info.finality = finality 84 software_info.finality = finality
75 software_info.acronym = acronym 85 software_info.acronym = acronym
76 - software_info.public_software = true  
77 - 86 + software_info.save!
78 software_info 87 software_info
79 end 88 end
80 89
@@ -103,38 +112,45 @@ def set_sisp_hashes software, sisp_hash @@ -103,38 +112,45 @@ def set_sisp_hashes software, sisp_hash
103 end 112 end
104 113
105 def create_identifier name 114 def create_identifier name
106 - "#{name.to_slug}".truncate(150, :omission => '', :separator => '-') 115 + "#{name.to_slug}".truncate(240, :omission => '', :separator => '-')
107 end 116 end
108 117
109 def create_sisp_user #TODO change user info 118 def create_sisp_user #TODO change user info
110 user = User.find_by_login('sisp') 119 user = User.find_by_login('sisp')
111 user ||= User.new(:login => 'sisp', :email => 'sisp_user@changeme.com', :password => 'sisp1234', :password_confirmation => 'sisp1234', :environment => $env) 120 user ||= User.new(:login => 'sisp', :email => 'sisp_user@changeme.com', :password => 'sisp1234', :password_confirmation => 'sisp1234', :environment => $env)
  121 + user.save!
112 user.activate if !user.activated? 122 user.activate if !user.activated?
113 user 123 user
114 end 124 end
115 125
116 def create_institution sisp_hash 126 def create_institution sisp_hash
117 127
118 - #TODO create_institution if doesnt exist  
119 -  
120 name = sisp_hash['1 - Identificação do órgão']['Nome da Empresa/Órgão'] 128 name = sisp_hash['1 - Identificação do órgão']['Nome da Empresa/Órgão']
121 - institution_community = Community::new 129 +
  130 + if name.size <=2
  131 + return nil
  132 + end
  133 +
  134 + institution_community = Community.find_or_initialize_by_identifier(name.to_slug)
  135 +
  136 + #puts institution_community.inspect
  137 + institution = PublicInstitution.find_or_initialize_by_name(name)
122 institution_community.name = name 138 institution_community.name = name
123 - institution_community.country = "Brasil"  
124 - institution_community.state = "Unknown" #TODO see what to put here  
125 - institution_community.city = "Unknown" #TODO see what to put here  
126 - institution_community.save 139 + institution_community.country = "BR"
  140 + institution_community.state = "DF"
  141 + institution_community.city = "Unknown"
  142 + institution_community.save!
127 143
128 - institution = PublicInstitution.new  
129 institution.community = institution_community 144 institution.community = institution_community
130 institution.name = name 145 institution.name = name
131 - institution.juridical_nature = sisp_hash['1 - Identificação do órgão']['Natureza']  
132 - institution.acronym = sisp_hash['1 - Identificação do órgão']['Sigla'] #TODO empty if bigger than 10  
133 - institution.governmental_power = sisp_hash['1 - Identificação do órgão']['Abrangência']  
134 - institution.governmental_sphere = sisp_hash['1 - Identificação do órgão']['Esfera']  
135 - institution.cnpj = "Unknown" #TODO see what to put here  
136 - institution.corporate_name = name #TODO see what to put here  
137 - institution.save 146 + institution.juridical_nature = JuridicalNature.find_or_create_by_name(:name => sisp_hash['1 - Identificação do órgão']['Natureza'])
  147 + institution.acronym = sisp_hash['1 - Identificação do órgão']['Sigla'].split[0]
  148 + institution.acronym = nil if (institution.acronym.size > 10)
  149 + institution.governmental_power = GovernmentalPower.find_or_create_by_name(:name => sisp_hash['1 - Identificação do órgão']['Esfera'])
  150 + institution.governmental_sphere = GovernmentalSphere.find_or_create_by_name(:name => sisp_hash['1 - Identificação do órgão']['Abrangência'])
  151 + institution.cnpj = nil
  152 + institution.corporate_name = name
  153 + institution.save!
138 institution 154 institution
139 155
140 end 156 end
@@ -142,14 +158,21 @@ end @@ -142,14 +158,21 @@ end
142 def create_ratings community_identifier, sisp_hash 158 def create_ratings community_identifier, sisp_hash
143 software_community = Community.find_by_identifier(community_identifier) 159 software_community = Community.find_by_identifier(community_identifier)
144 160
145 - comment_system = Comment.create!(:body=>"Informações de custo de sistema importadas automaticamente do catálogo SISP.", :target => software_community, :author=> $sisp_user)  
146 - comment_maintenance = Comment.create!(:body=>"Informações de custo de manutenção importadas automaticamente do catálogo SISP.", :target => software_community, :author=> $sisp_user)  
147 -  
148 institution = create_institution(sisp_hash) 161 institution = create_institution(sisp_hash)
  162 + if institution.nil?
  163 + return nil
  164 + end
  165 +
  166 + comment_system = Comment.new(:body=>"Informações de custo de sistema importadas automaticamente do catálogo SISP.", :author=> $sisp_user.person)
  167 + comment_system.source = software_community
  168 + comment_system.save!
  169 +comment_maintenance = Comment.new(:body=>"Informações de custo de manutenção importadas automaticamente do catálogo SISP.", :author=> $sisp_user.person)
  170 + comment_maintenance.source = software_community
  171 + comment_maintenance.save!
149 172
150 - system_rating = OrganizationRating.create!( 173 + OrganizationRating.create!(
151 :comment => comment_system, 174 :comment => comment_system,
152 - :value => 3, 175 + :value => 1, #TODO see what to put here
153 :person => $sisp_user.person, 176 :person => $sisp_user.person,
154 :organization => software_community, 177 :organization => software_community,
155 :institution => institution, 178 :institution => institution,
@@ -157,9 +180,9 @@ def create_ratings community_identifier, sisp_hash @@ -157,9 +180,9 @@ def create_ratings community_identifier, sisp_hash
157 :people_benefited => 0 180 :people_benefited => 0
158 ) 181 )
159 182
160 - maintenance_rating = OrganizationRating.create!( 183 + OrganizationRating.create!(
161 :comment => comment_maintenance, 184 :comment => comment_maintenance,
162 - :value => 3, 185 + :value => 1, #TODO see what to put here
163 :person => $sisp_user.person, 186 :person => $sisp_user.person,
164 :organization => software_community, 187 :organization => software_community,
165 :institution => institution, 188 :institution => institution,
@@ -171,11 +194,9 @@ end @@ -171,11 +194,9 @@ end
171 194
172 def create_software_and_attrs sisp_hash 195 def create_software_and_attrs sisp_hash
173 name = sisp_hash['3 - Identificação do software']['Nome'].truncate(240, :omission => '', :separator => ' ') 196 name = sisp_hash['3 - Identificação do software']['Nome'].truncate(240, :omission => '', :separator => ' ')
  197 +
174 identifier = create_identifier name 198 identifier = create_identifier name
175 199
176 - #software = Community.find_by_identifier(identifier)  
177 - #software = software.software_info if software  
178 - #software ||= create_software_info(name)  
179 software = create_software_info(name) 200 software = create_software_info(name)
180 201
181 create_ratings identifier, sisp_hash 202 create_ratings identifier, sisp_hash