Merge Request #2

Merged
softwarepublico/mpog_software!2
Created by Arthur Esposte

To csv

Add a new task to export catalog data to CSV files.

Assignee: Thiago Ribeiro
Milestone: None

Merged by Thiago Ribeiro

Source branch has been removed
Commits (3)
3 participants
lib/tasks/export.rake 0 → 100644
... ... @@ -0,0 +1,133 @@
  1 +require 'csv'
  2 +
  3 +namespace :export do
  4 + namespace :catalog do
  5 + desc "Export all softwares to CSV"
  6 + task :csv => :environment do
  7 + Environment.all.each do |env|
  8 + if env.plugin_enabled?("MpogSoftware") or env.plugin_enabled?("SoftwareCommunitiesPlugin")
  9 + softwares_to_csv
  10 + categories_to_csv
  11 + software_categories_to_csv
  12 +
  13 + compress_files
  14 + end
  15 + end
  16 + end
  17 + end
  18 +
  19 + def softwares_to_csv
  20 + print "Exporting softwares to softwares.csv: "
  21 +
  22 + CSV.open('/tmp/softwares.csv', 'w') do |csv|
  23 + csv << [
  24 + "id",
  25 + "community_id",
  26 + "identifier",
  27 + "name",
  28 + "finality",
  29 + "acronym",
  30 + "created_at",
  31 + "image_filename",
  32 + "home_page_name",
  33 + "home_page_slug",
  34 + "home_page_path",
  35 + "home_page_body",
  36 + "home_page_abstract",
  37 + "home_page_published_at"
  38 + ]
  39 +
  40 + SoftwareInfo.all.each do |software|
  41 + if software.community
  42 + begin
  43 + csv << [
  44 + software.id,
  45 + software.community.id,
  46 + software.community.identifier,
  47 + software.community.name,
  48 + software.finality,
  49 + software.acronym,
  50 + software.community.created_at,
  51 + (software.community.image.nil? ? nil : software.community.image.filename),
  52 + (software.community.home_page.nil? ? nil : software.community.home_page.name),
  53 + (software.community.home_page.nil? ? nil : software.community.home_page.slug),
  54 + (software.community.home_page.nil? ? nil : software.community.home_page.path),
  55 + (software.community.home_page.nil? ? nil : software.community.home_page.body),
  56 + (software.community.home_page.nil? ? nil : software.community.home_page.abstract),
  57 + (software.community.home_page.nil? ? nil : software.community.home_page.published_at),
  58 + ]
  59 +
  60 + print '.'
  61 + rescue
  62 + print 'F'
  63 + end
  64 + end
  65 + end
  66 + end
  67 +
  68 + print "\n"
  69 + end
  70 +
  71 + def categories_to_csv
  72 + print "Exporting categories to categories.csv: "
  73 +
  74 + CSV.open('/tmp/categories.csv', 'w') do |csv|
  75 + csv << [
  76 + "id",
  77 + "name",
  78 + "path",
  79 + ]
  80 +
  81 + Category.all.each do |category|
  82 + begin
  83 + csv << [
  84 + category.id,
  85 + category.name,
  86 + category.path,
  87 + ]
  88 +
  89 + print '.'
  90 + rescue
  91 + print 'F'
  92 + end
  93 + end
  94 + end
  95 +
  96 + print "\n"
  97 + end
  98 +
  99 + def software_categories_to_csv
  100 + print "Exporting software and categories relation to software_categories.csv: "
  101 + CSV.open('/tmp/software_categories.csv', 'w') do |csv|
  102 + csv << [
  103 + "software_id",
  104 + "category_id"
  105 + ]
  106 +
  107 + SoftwareInfo.all.each do |software|
  108 + if software.community
  109 + software.community.categories.each do |category|
  110 + begin
  111 + csv << [
  112 + software.id,
  113 + category.id
  114 + ]
  115 +
  116 + print '.'
  117 + rescue
  118 + print 'F'
  119 + end
  120 + end
  121 + end
  122 + end
  123 + end
  124 +
  125 + print "\n"
  126 + end
  127 +
  128 + def compress_files
  129 + `cd /tmp/ && tar -zcvf software_catalog_csvs.tar.gz softwares.csv categories.csv software_categories.csv`
  130 +
  131 + `cd /tmp/ && rm softwares.csv categories.csv software_categories.csv`
  132 + end
  133 +end
0 134 \ No newline at end of file
... ...
views/blocks/categories_software.html.erb
... ... @@ -8,9 +8,12 @@
8 8 <p><%= _("Categories:") %></p>
9 9 <ul class="categories-mais-software">
10 10  
11   - <% Category.where(:name => "Software").first.children.each do |category| %>
12   - <% unless category.software_infos.count < 1 %>
13   - <li><%= link_to _("#{category.name}") + " (#{category.software_infos.count})", {:controller => :search, :action => :software_infos, :selected_categories_id => [category.id]} %></li>
  11 + <% categories = Category.where(:name => "Software") %>
  12 + <% unless categories.blank? %>
  13 + <% categories.first.children.each do |category| %>
  14 + <% unless category.software_infos.count < 1 %>
  15 + <li><%= link_to _("#{category.name}") + " (#{category.software_infos.count})", {:controller => :search, :action => :software_infos, :selected_categories_id => [category.id]} %></li>
  16 + <% end %>
14 17 <% end %>
15 18 <% end %>
16 19 </ul>
... ...