whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ru...@apache.org
Subject [whimsy] branch master updated: add a button to upload financials
Date Tue, 14 Feb 2017 18:23:56 GMT
This is an automated email from the ASF dual-hosted git repository.

rubys pushed a commit to branch master
in repository https://git-dual.apache.org/repos/asf/whimsy.git

The following commit(s) were added to refs/heads/master by this push:
       new  91ec773   add a button to upload financials
91ec773 is described below

commit 91ec7731efa56b40b10f9c8be78acb71dad0d117
Author: Sam Ruby <rubys@intertwingly.net>
AuthorDate: Tue Feb 14 13:23:39 2017 -0500

    add a button to upload financials
---
 www/board/agenda/Gemfile                          |  2 +
 www/board/agenda/public/stylesheets/app.css       |  4 ++
 www/board/agenda/views/actions/financials.json.rb | 79 +++++++++++++++++++++++
 www/board/agenda/views/buttons/post.js.rb         | 37 ++++++++++-
 4 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/www/board/agenda/Gemfile b/www/board/agenda/Gemfile
index f0d4617..a55f95c 100644
--- a/www/board/agenda/Gemfile
+++ b/www/board/agenda/Gemfile
@@ -27,6 +27,8 @@ gem 'concurrent-ruby'
 
 gem 'mime-types', '~> 2.6'
 
+gem 'rubyXL'
+
 group :test do
   gem 'rspec'
   gem 'capybara'
diff --git a/www/board/agenda/public/stylesheets/app.css b/www/board/agenda/public/stylesheets/app.css
index 95fc9a6..9b66120 100644
--- a/www/board/agenda/public/stylesheets/app.css
+++ b/www/board/agenda/public/stylesheets/app.css
@@ -490,3 +490,7 @@ h5.history {
   margin-left: 1em;
 }
 
+input[type=file]#upload {
+  display: inline-block;
+  margin-right: 1em;
+}
diff --git a/www/board/agenda/views/actions/financials.json.rb b/www/board/agenda/views/actions/financials.json.rb
new file mode 100644
index 0000000..1919ba2
--- /dev/null
+++ b/www/board/agenda/views/actions/financials.json.rb
@@ -0,0 +1,79 @@
+#
+# Extract the Board Summary from the Virtual xlsx spreadsheet and format it as
+# plain text
+#
+
+require 'base64'
+require 'rubyXL'
+require 'active_support'
+require 'active_support/core_ext/numeric/conversions'
+
+# parse worksheet
+# https://github.com/weshatheleopard/rubyXL/issues/235
+begin
+  stdout, $stdout = $stdout, File.new("/dev/null", "w")
+  worksheet = RubyXL::Parser.parse_buffer(Base64.decode64(@spreadsheet))
+ensure
+  $stdout = stdout
+end
+
+# extract data
+rows = []
+worksheet['Board Summary'].each do |row|
+  data = []
+
+  row && row.cells.each do |cell|
+    value = cell && cell.value
+
+    # format number values
+    if value.is_a? Numeric
+      value = ActiveSupport::NumberHelper::number_to_currency(value, unit: '').
+        rjust(12)
+    end
+
+    data << value
+  end
+
+  # strip trailing empty cells
+  data.pop while data.length > 0 and data[-1] == nil
+
+  # combine the first four columns into a single column
+  if data.length
+    heading = data.shift(4)
+    data.unshift heading.join('  ').sub(/\s+$/, '').sub(/^ {1,4}/, '').ljust(29)
+  end
+
+  rows << data
+end
+
+# delete first two rows
+rows.shift(2)
+
+# move headings from second to seventh row
+headings = rows.delete_at(1)
+headings[2] = headings[6] = 'Budget'
+1.upto(7) {|i| headings[i] = headings[i].rjust(12) if headings[i]}
+rows.insert(6, headings)
+
+# delete empty rows
+rows.pop while rows[-1].length == 1 and rows[-1][0].strip == ''
+
+# print out current month totals
+output = []
+rows.each do |row|
+  output << row[0..4].join(' ')
+end
+
+# drop current balances
+rows.shift(5)
+
+# print out YTD totals
+rows.each do |row|
+  if row.length >= 7
+    output << ([row[0]] + row[5..7]).join(' ')
+  else
+    output << row[0].strip
+  end
+end
+
+{table: output.join("\n")}
diff --git a/www/board/agenda/views/buttons/post.js.rb b/www/board/agenda/views/buttons/post.js.rb
index 9afbd52..757cbd8 100644
--- a/www/board/agenda/views/buttons/post.js.rb
+++ b/www/board/agenda/views/buttons/post.js.rb
@@ -36,6 +36,18 @@ class Post < React
         placeholder: @label, rows: 17, disabled: @disabled, 
         onChange: self.change_text
 
+      # upload of spreadsheet from virtual
+      if @@item.title == 'Treasurer'
+        _form do
+          _div.form_group do
+            _label 'financial spreadsheet from virtual', for: 'upload'
+            _input.upload! type: 'file', value: @upload
+            _button.btn.btn_primary 'Upload', onClick: self.upload,
+              disabled: @disabled || !@upload
+          end
+        end
+      end
+
       #input field: commit_message
       if @@button.text != 'add resolution'
         _input.post_report_message! label: 'commit message', 
@@ -171,8 +183,31 @@ class Post < React
     end
   end
 
-  # when save button is pushed, post comment and dismiss modal when complete
+  # upload contents of spreadsheet in base64; append extracted table to report
+  def upload(event)
+    @disabled = true
+    event.preventDefault()
+
+    reader = FileReader.new
+    def reader.onload(event)
+      result = event.target.result
+      base64 = btoa(String.fromCharCode.apply(null, Uint8Array.new(result)))
+      post 'financials', spreadsheet: base64 do |response|
+        report = @report
+        report += "\n" if report and not report.end_with? "\n"
+        report += "\n" if report
+        report += response.table
+
+        self.change_text target: {value: report}
+
+        @upload = nil
+        @disabled = false
+      end
+    end
+    reader.readAsArrayBuffer(document.getElementById('upload').files[0])
+  end
 
+  # when save button is pushed, post comment and dismiss modal when complete
   def submit(event)
     @edited = false
 

-- 
To stop receiving notification emails like this one, please contact
['"commits@whimsical.apache.org" <commits@whimsical.apache.org>'].

Mime
View raw message