Commit 3deb8fb0 authored by Adam Rambousek's avatar Adam Rambousek
Browse files

Fix #136 csv export

parent 4c244c12
Loading
Loading
Loading
Loading
+71 −13
Original line number Diff line number Diff line
@@ -153,12 +153,8 @@ class TeCu < DictSedna
    return alphabet
  end

  def get_term(id, work=false)
    if work
      xml = @admindict.array["tecuwork"].get(id)
    else
      xml = @admindict.array["tecu"].get(id)
    end
  def get_term(id, dict='')
    xml = @admindict.array["tecu"+dict].get(id)
    $stderr.puts id
    $stderr.puts xml
    term = ''
@@ -371,6 +367,56 @@ class TeCu < DictSedna
    return to_csv
  end

  def export_csv
    rows = []
    to_csv = []
    query = 'for $x in collection("tecu")/entry return $x'
    @sedna.query(query).each{|xml|
      if xml != ''
        doc = load_xml_string(xml)
        eid = doc.root['id']
        rows = [ [eid,doc.root['abbr'],'','','','','','','','','','',''] ]
        ri = 0
        doc.find('terms/term').each{|t|
          rows[ri] = ['','','','','','','','','','','',''] if rows[ri].nil?
          rows[ri][2] = t.content
          rows[ri][3] = t['lang']
          ri += 1
        }
        ri = 0
        doc.find('defs/def').each{|t|
          rows[ri] = ['','','','','','','','','','','','',''] if rows[ri].nil?
          rows[ri][4] = t.find('text').first.content.to_s unless t.find('text').first.nil?
          rows[ri][5] = t['lang'].to_s
          rows[ri][6] = t['obor'].to_s
          rows[ri][7] = t.find('zdroj').first.content.to_s unless t.find('zdroj').first.nil?
          ri += 1
        }
        ri = 0
        ri = 0
        doc.find('hyper').each{|t|
          rows[ri] = ['','','','','','','','','','','','',''] if rows[ri].nil?
          rows[ri][8] = t['id'].to_s #get_term(t['id'])
          ri += 1
        }
        ri = 0
        doc.find('synonyms/synonym').each{|t|
          rows[ri] = ['','','','','','','','','','','','',''] if rows[ri].nil?
          rows[ri][9] = t.content
          ri += 1
        }
        ri = 0
        doc.find('antonyms/antonym').each{|t|
          rows[ri] = ['','','','','','','','','','','','',''] if rows[ri].nil?
          rows[ri][10] = t.content
          ri += 1
        }
      end
      to_csv += rows
    }
    return to_csv
  end

  def get_top
    res = []
    query = '[entry[not(hyper)]]'
@@ -1127,9 +1173,17 @@ Subject: #{subject}
    query = 'for $x in collection("tecuwork")/entry return <a>{data($x/@id)};{data($x/@wait_for)}</a>'
    @sedna.query(query).each{|r|
      ri = r.sub('<a>','').sub('</a>','').split(';')
      ra = ri[1].split(',')
      ra = ri[1].to_s.split(',')
      ra.delete_at(ra.index('hl_redaktor') || ra.length) if ra.length > 1
      res['waiting'] << {'id'=>ri[0], 'wait_for'=>ra, 'term'=>get_term(ri[0].to_s, true)} unless ra.length == 0
      res['waiting'] << {'id'=>ri[0], 'wait_for'=>ra, 'term'=>get_term(ri[0].to_s, 'work')} unless ra.length == 0
    }
    return res
  end
  def get_deleted
    res = {'deleted'=>[]}
    query = 'for $x in collection("tecudel")/entry return data($x/@id)'
    @sedna.query(query).each{|r|
      res['deleted'] << {'id'=>r, 'term'=>get_term(r, 'del')} 
    }
    return res
  end
@@ -1149,10 +1203,14 @@ Subject: #{subject}
      username = settings['tech']['smtp_user']
      password = settings['tech']['smtp_pass']
    end
    smtp = Net::SMTP.new(server, port)
    smtp.enable_starttls_auto
    smtp.start(server, username, password, :plain)
    smtp.send_message(message, from, to)
    smtp.send_message(message, from, copy) if copy != ''
    #smtp = Net::SMTP.new(server, port)
    #smtp.enable_starttls_auto if port != 25
    #if username.nil? or username == ''
    #  smtp.start(server)
    #else
    #  smtp.start(server, username, password, :plain)
    #end
    #smtp.send_message(message, from, to)
    #smtp.send_message(message, from, copy) if copy != ''
  end
end
+10 −1
Original line number Diff line number Diff line
@@ -700,6 +700,15 @@ def do_GET(request, response)
        response['Content-Type'] = 'application/json; charset=utf-8'
      end
    end
  when 'export_csv'
    res = @dict.export_csv
    csvstring = CSV.generate({:col_sep => "\t",:force_quotes=>true}){|csv| 
      csv << ['id', 'zkratka','termin','jazyk terminu','definice','jazyk definice','obor','zdroj','hyperonyma','synonyma','antonyma']
      res.each{|r| csv << r}
    }
    response.body = csvstring
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment; filename=tecu_export.csv'
  when 'export_subtree'
    if request.query['id'].to_s != ''
      if request.query['id'].to_s == '0'
@@ -1073,7 +1082,7 @@ def do_GET(request, response)
  when 'ie'
    res = '<div id="mainBody"><ul><li><a href="/tecu?action=download_all" target="_blank">Export XML</a>'
    res += '<li><a href="/tecu?action=download_all&tr=skos" target="_blank">Export SKOS</a>'
    res += '<li><a href="/tecu?action=export_subtree&id=0" target="_blank">Export CSV</a>'
    res += '<li><a href="/tecu?action=export_csv" target="_blank">Export CSV</a>'
    res += '<li><a href="/tecu?action=export_tree" target="_blank">Export strom termínů</a>'
    res += '<li>Import: <form method="POST" target="_blank"  enctype="multipart/form-data" action="/tecu"><input type="hidden" name="action" value="import_xml"> <input name="file" type="file"><select name="format"><option value="xml">XML<option value="skos">SKOS<option value="csv">CSV</select><label><input type="checkbox" name="delete"/> smazat existující data</label> <input type="submit" value="Import"/></form>'
    res += '<li>Vložit dokument do korpusu: <form method="POST" target="_blank"  enctype="multipart/form-data" action="/tecu"><input type="hidden" name="action" value="import_corpora"> <input name="file" type="file"><input type="submit" value="Import"/></form>'