Skip to content

Commit 9001675

Browse files
author
Pietro
committed
First oltrino conf running
1 parent c649862 commit 9001675

File tree

8 files changed

+291
-5
lines changed

8 files changed

+291
-5
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,5 @@ yarn-debug.log*
3636
.yarn-integrity
3737

3838
# SWAPI related stuff
39-
/swapi-commoncoin-data
40-
/swapi-santacoin-data
39+
/swapi-*-data
4140
apikey.yaml

app/controllers/wallets_controller.rb

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class WalletsController < ApplicationController
2-
load_and_authorize_resource :commoner
2+
load_and_authorize_resource :commoner, except: :short_view
33
load_and_authorize_resource :group
4-
load_and_authorize_resource :wallet, through: [:commoner, :group]
4+
load_and_authorize_resource :wallet, through: [:commoner, :group], except: :short_view
55

66
before_action :set_currency
77

@@ -11,6 +11,15 @@ def show
1111
@grouped_transactions = @wallet.transactions.order(created_at: :desc).last(10).reverse.group_by {|t| t.created_at.to_date}
1212
end
1313

14+
def short_view
15+
wallet = Wallet.find_by id: params[:id]
16+
if wallet.present?
17+
redirect_to view_commoner_wallet_url(wallet.holder, wallet)
18+
else
19+
raise CanCan::AccessDenied.new
20+
end
21+
end
22+
1423
def view
1524
@group = @wallet.currency.group
1625
# The seller is redirected to a new withdraw transaction

app/models/ability.rb

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def initialize(user)
1818
# Commoncoin wallets not visible by guests
1919
cannot :read, Wallet, currency_id: nil
2020
# Only some group currency wallets are visible
21+
can :short_view, Wallet
2122
can :view, Wallet, currency_id: ENV['QR_CODE_ENABLED_CURRENCIES'].split(',').map(&:to_i)
2223
cannot :view, Wallet, walletable_type: 'Group'
2324
can [:affiliation, :affiliate], Group

config/routes.rb

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
end
5353
get 'story_builder', to: 'stories#builder'
5454

55+
# Usage: wallet_short_path(wallet_id)
56+
match 'w/:id', to: 'wallets#short_view', via: :get, as: 'wallet_short'
5557
resources :commoners, except: [:index] do
5658
resources :stories, only: :index
5759
# :index used for commoner/42/comments, visible only by comments' author

docker-compose.yml

+22
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ services:
102102
container_name: swapi_santacoin_mongo
103103
volumes:
104104
- ./swapi-santacoin-data/db:/data/db
105+
swapi_oltrino:
106+
image: dyne/social-wallet-api
107+
container_name: swapi_oltrino
108+
volumes:
109+
- ./swapi-oltrino-conf/social-wallet-api.yaml:/app/resources/social-wallet-api.yaml
110+
- ./swapi-oltrino-conf/apikey.yaml:/app/apikey.yaml
111+
ports:
112+
- "3038:3000" # if you specify the env variable SWAPI_PORT make sure it matches the container port here
113+
environment:
114+
- SWAPI_HOST=0.0.0.0 # hostname on which the social wallet api service will listen
115+
# - SWAPI_PORT=4000 # port on which the social wallet api service will listen (default is 3000)
116+
depends_on:
117+
- swapi_oltrino_mongo
118+
# network_mode: "host" # required if social wallet api service listens only to 'localhost'
119+
# These two allow debug with pry
120+
tty: true
121+
stdin_open: true
122+
swapi_oltrino_mongo:
123+
image: mongo
124+
container_name: swapi_oltrino_mongo
125+
volumes:
126+
- ./swapi-oltrino-data/db:/data/db
105127
commonshare:
106128
image: commonfare/commonshare-python
107129
container_name: commonshare

docker-sync.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ syncs:
77
app-sync: # tip: add -sync and you keep consistent names as a convention
88
src: '.'
99
# sync_strategy: 'native_osx' # not needed, this is the default now
10-
sync_excludes: ['db-data', 'swapi-commoncoin-data', 'swapi-santacoin-data', '.git', 'tmp', '.DS_Store']
10+
sync_excludes: ['db-data', 'swapi-commoncoin-data', 'swapi-santacoin-data', 'swapi-oltrino-data', '.git', 'tmp', '.DS_Store']
1111
sync_strategy: 'unison'
1212
#sync_excludes_type: 'log'
1313
sync_host_ip: '127.0.0.1'

lib/tasks/oltrino.rake

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
require "prawn/measurement_extensions"
2+
3+
namespace :oltrino do
4+
5+
desc """
6+
Generates :num customers with wallets of :amount Oltrino in the given group
7+
and generates the related PDF and CSV files
8+
"""
9+
task :generate_customers, [:group_id, :num, :amount] => :environment do |t, args|
10+
group = Group.find_by(id: args[:group_id]) # improve this
11+
abort 'Invalid group_id' if group.nil?
12+
num = args[:num].to_i || 10
13+
amount = args[:amount].to_i || 50
14+
currency = group.currency
15+
wallets = []
16+
num.times do
17+
commoner = Commoner.find_or_create_by name: generate_commoner_name do |commoner|
18+
user_attributes_for(commoner)
19+
end
20+
Membership.create(commoner: commoner, group: group)
21+
wallet = Wallet.find_by(currency: currency, walletable: commoner)
22+
transfer_oltrino_to_wallet(wallet, amount)
23+
wallets << wallet
24+
end
25+
group.wallet.refresh_balance
26+
generate_pdf_for_wallets(wallets, amount)
27+
generate_csv_for_wallets(wallets, amount)
28+
end
29+
30+
desc "Destroy and recreate all the wallets in the given group"
31+
task :reset_group_wallets, [:group_id] => :environment do |t, args|
32+
group = Group.find_by(id: args[:group_id]) # improve this
33+
abort 'Invalid group_id' if group.nil?
34+
currency = group.currency
35+
# reset group wallet
36+
group.wallet.transactions.destroy_all
37+
group.wallet.destroy
38+
Wallet.create(walletable: group,
39+
address: '',
40+
currency: currency)
41+
# reset members' wallets
42+
group.members.find_each do |member|
43+
wallet = Wallet.find_by(currency: currency, walletable: member)
44+
wallet.transactions.destroy_all
45+
wallet.destroy
46+
new_wallet = Wallet.create(walletable: member,
47+
address: Digest::SHA2.hexdigest(member.email + Time.now.to_s),
48+
currency: currency)
49+
transfer_oltrino_to_wallet(new_wallet, 10)
50+
end
51+
group.wallet.refresh_balance
52+
end
53+
54+
desc "Generates a PDF with the QR codes of all the wallets in the given group"
55+
task :generate_qrcode_pdf, [:group_id] => :environment do |t, args|
56+
group = Group.find_by(id: args[:group_id]) # improve this
57+
abort 'Invalid group_id' if group.nil?
58+
currency = group.currency
59+
Prawn::Document.generate("#{host_tmp_path}/#{timestamp}_oltrino_qrcodes.pdf", page_options) do
60+
group.members.find_each do |member|
61+
wallet = Wallet.find_by(currency: currency, walletable: member)
62+
# see http://www.qrcode.com/en/about/version.html for versions
63+
# 7 -> 45x45 modules
64+
# 8 -> 49x49 modules
65+
qr = RQRCode::QRCode.new(
66+
commoner_wallet_url(member, wallet),
67+
size: 7)
68+
qr_svg = qr.as_svg(offset: 0,
69+
color: '000',
70+
shape_rendering: 'crispEdges',
71+
module_size: 5.5) # in pixels
72+
svg qr_svg, position: :center, width: 179
73+
move_down 2
74+
text "WalletID #{wallet.id}", align: :center
75+
start_new_page
76+
end
77+
end
78+
end
79+
80+
desc "Generates a CSV with the URLs of all the wallets in the given group"
81+
task :generate_wallet_urls_csv, [:group_id] => :environment do |t, args|
82+
group = Group.find_by(id: args[:group_id]) # improve this
83+
abort 'Invalid group_id' if group.nil?
84+
currency = group.currency
85+
output_file = File.join(host_tmp_path, ("#{timestamp}_oltrino_wallet_urls.csv"))
86+
CSV.open(output_file, 'wb') do |csv|
87+
csv << %w(Wallet_URL Wallet_ID)
88+
group.members.find_each do |member|
89+
wallet = Wallet.find_by(currency: currency, walletable: member)
90+
wallet_url = commoner_wallet_url(member, wallet)
91+
csv << [wallet_url, wallet.id]
92+
end
93+
end
94+
end
95+
96+
task :import_sellers, [:group_id] => :environment do |t, args|
97+
group = Group.find_by(id: args[:group_id])
98+
abort 'Invalid group_id' if group.nil?
99+
puts group.name
100+
currency = group.currency
101+
wallets = []
102+
# out_csv = CSV.new(File.join(host_tmp_path, 'sf_sellers_out.csv'), headers: true)
103+
CSV.open(File.join(host_tmp_path, 'sf_sellers_out.csv'), 'wb') do |out_csv|
104+
out_csv << %w(Name Email Password)
105+
CSV.foreach(File.join(host_tmp_path, 'sf_sellers.csv'), headers: true, header_converters: :symbol) do |row|
106+
hash_row = row.to_hash
107+
name = hash_row[:name]
108+
email = hash_row[:email]
109+
password = [('a'..'z').to_a.shuffle[0,8].join, rand(100..999)].join
110+
puts "#{hash_row[:name]} #{hash_row[:email]}"
111+
out_csv << [name, email, password]
112+
commoner = Commoner.find_or_create_by name: name do |commoner|
113+
seller_user_attributes_for(commoner, email, password)
114+
end
115+
Membership.find_or_create_by(commoner: commoner, group: group, role: 'editor')
116+
wallet = Wallet.find_by(currency: currency, walletable: commoner)
117+
wallets << wallet
118+
end
119+
end
120+
group.wallet.refresh_balance
121+
generate_pdf_for_wallets(wallets, 0)
122+
generate_csv_for_wallets(wallets, 0)
123+
end
124+
125+
def page_options
126+
{
127+
margin: 1.mm,
128+
page_size: [70.mm, 70.mm]
129+
}
130+
end
131+
132+
def host_tmp_path
133+
"/host_tmp" # A volume defined in the proper docker-compose file
134+
end
135+
136+
def timestamp
137+
I18n.l Time.zone.now, format: '%Y%m%d%H%M'
138+
end
139+
140+
def user_attributes_for(resource)
141+
resource.user_attributes = {
142+
email: "#{resource.name.parameterize}@sf.it",
143+
password: [('a'..'z').to_a.shuffle[0,8].join, rand(100..999)].join
144+
}
145+
end
146+
147+
def seller_user_attributes_for(resource, email, password)
148+
resource.user_attributes = {
149+
email: email,
150+
password: password
151+
}
152+
end
153+
154+
def transfer_oltrino_to_wallet(wallet, amount)
155+
client = SocialWallet::Client.new(
156+
api_endpoint: wallet.endpoint,
157+
api_key: wallet.api_key
158+
)
159+
resp = client.transactions.new(from_id: '', to_id: wallet.address, amount: amount, tags: ['initial_income', 'new_commoner'])
160+
wallet.refresh_balance if resp['amount'] == amount
161+
end
162+
163+
def commoner_wallet_url(commoner, wallet)
164+
Rails.application.routes.url_helpers.view_commoner_wallet_url(
165+
host: hostname,
166+
locale: 'it',
167+
commoner_id: commoner.id,
168+
id: wallet.id)
169+
end
170+
171+
def hostname
172+
case Rails.env
173+
when 'production'
174+
'https://commonfare.net'
175+
when 'staging'
176+
'https://cf-staging.fbk.eu'
177+
else
178+
'http://localhost:3000'
179+
end
180+
end
181+
182+
def generate_commoner_name
183+
name = "SF_#{[('a'..'z').to_a.shuffle[0,5].join, rand(100..999)].join}"
184+
if Commoner.exists?(["lower(name) = ?", name.downcase])
185+
generate_commoner_name
186+
else
187+
name
188+
end
189+
end
190+
191+
def generate_pdf_for_wallets(wallets, amount)
192+
sorted_ids = wallets.map(&:id).sort
193+
Prawn::Document.generate("#{host_tmp_path}/#{timestamp}_oltrino_qrcodes_#{amount}sc_#{sorted_ids.first}-#{sorted_ids.last}.pdf", page_options) do
194+
wallets.each do |wallet|
195+
# see http://www.qrcode.com/en/about/version.html for versions
196+
# 7 -> 45x45 modules
197+
# 8 -> 49x49 modules
198+
qr = RQRCode::QRCode.new(
199+
commoner_wallet_url(wallet.walletable, wallet),
200+
size: 7)
201+
qr_svg = qr.as_svg(offset: 0,
202+
color: '000',
203+
shape_rendering: 'crispEdges',
204+
module_size: 5.5) # in pixels
205+
svg qr_svg, position: :center, width: 179
206+
move_down 2
207+
text "WalletID #{wallet.id}", align: :center
208+
start_new_page
209+
end
210+
end
211+
end
212+
213+
def generate_csv_for_wallets(wallets, amount)
214+
sorted_ids = wallets.map(&:id).sort
215+
output_file = File.join(host_tmp_path, ("#{timestamp}_oltrino_wallet_urls_#{amount}sc_#{sorted_ids.first}-#{sorted_ids.last}.csv"))
216+
CSV.open(output_file, 'wb') do |csv|
217+
csv << %w(Wallet_URL Wallet_ID)
218+
wallets.each do |wallet|
219+
wallet_url = commoner_wallet_url(wallet.walletable, wallet)
220+
csv << [wallet_url, wallet.id]
221+
end
222+
end
223+
end
224+
225+
end
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ===============================================================
2+
# Example of the configuration file for Social Wallet API service
3+
# ===============================================================
4+
#
5+
# You will need to copy it to social-wallet-api.yaml and tweak
6+
# the configuration according to your specific needs.
7+
#
8+
# For further details and documentation please refer to:
9+
# https://github.com/Commonfare-net/social-wallet-api
10+
#
11+
# ===============================================================
12+
13+
log-level: debug
14+
15+
freecoin:
16+
mongo:
17+
host: swapi_oltrino_mongo
18+
port: 27017
19+
db: freecoin
20+
currency: Oltrino
21+
apikey: swapi_oltrino
22+
# faircoin:
23+
# currency: fair
24+
# number-confirmations: 6
25+
# frequency-confirmations-millis: 20000
26+
# rpc-config-path: /tmp/faircoin.conf
27+
# deposit-expiration-millis: 3600000
28+
# frequency-deposit-millis: 60000

0 commit comments

Comments
 (0)