-
Notifications
You must be signed in to change notification settings - Fork 45
/
parts_server.rb
579 lines (482 loc) · 19.6 KB
/
parts_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# Copyright 2012 Team 254. All Rights Reserved.
# @author [email protected] (Patrick Fairbank)
#
# The main class of the parts management web server.
require "active_support/time"
require "cgi"
require "dedent"
require "eventmachine"
require "json"
require "pathological"
require "pony"
require "sinatra/base"
require "models"
module CheesyParts
class Server < Sinatra::Base
use Rack::Session::Cookie, :key => "rack.session", :expire_after => 3600
# Enforce authentication for all routes except login and user registration.
before do
@user = User[session[:user_id]]
authenticate! unless ["/login", "/register"].include?(request.path)
end
def authenticate!
redirect "/login?redirect=#{request.path}" if @user.nil?
if @user.enabled == 0
session[:user_id] = nil
redirect "/login?disabled=1"
end
end
def require_permission(user_permitted)
halt(400, "Insufficient permissions.") unless user_permitted
end
# Helper function to send an e-mail through Gmail's SMTP server.
def send_email(to, subject, body)
# Run this asynchronously using EventMachine since it takes a couple of seconds.
EM.defer do
Pony.mail(:from => "Cheesy Parts <#{CheesyCommon::Config.gmail_user}>", :to => to,
:subject => subject, :body => body, :via => :smtp,
:via_options => { :address => "smtp.gmail.com", :port => "587",
:enable_starttls_auto => true,
:user_name => CheesyCommon::Config.gmail_user.split("@").first,
:password => CheesyCommon::Config.gmail_password,
:authentication => :plain, :domain => "localhost.localdomain" })
end
end
get "/" do
redirect "/projects"
end
get "/login" do
redirect "/logout" if @user
@redirect = params[:redirect] || "/"
if CheesyCommon::Config.enable_wordpress_auth
member = CheesyCommon::Auth.get_user(request)
if member.nil?
redirect "#{CheesyCommon::Config.members_url}?site=parts&path=#{request.path}"
else
user = User[:wordpress_user_id => member.id]
unless user
user = User.create(:wordpress_user_id => member.id, :first_name => member.name[1],
:last_name => member.name[0], :permission => "editor", :enabled => 1,
:email => member.email, :password => "", :salt => "")
end
session[:user_id] = user.id
redirect @redirect
end
end
if params[:failed] == "1"
@alert = "Invalid e-mail address or password."
elsif params[:disabled] == "1"
@alert = "Your account is currently disabled."
end
erb :login
end
post "/login" do
user = User.authenticate(params[:email], params[:password])
redirect "/login?failed=1" if user.nil?
redirect "/login?disabled=1" if user.enabled == 0
session[:user_id] = user.id
redirect params[:redirect]
end
get "/logout" do
session[:user_id] = nil
if CheesyCommon::Config.enable_wordpress_auth
redirect "#{CheesyCommon::Config.members_url}/logout"
else
redirect "/login"
end
end
get "/new_project" do
require_permission(@user.can_edit?)
erb :new_project
end
get "/projects" do
erb :projects
end
post "/projects" do
require_permission(@user.can_edit?)
# Check parameter existence and format.
halt(400, "Missing project name.") if params[:name].nil?
halt(400, "Missing part number prefix.") if params[:part_number_prefix].nil?
project = Project.create(:name => params[:name], :part_number_prefix => params[:part_number_prefix], :hide_dashboards => false)
redirect "/projects/#{project.id}"
end
before "/projects/:id*" do
@project = Project[params[:id]]
halt(400, "Invalid project.") if @project.nil?
end
get "/projects/:id" do
if ["type", "name", "parent_part_id", "status"].include?(params[:sort])
@part_sort = params[:sort].to_sym
else
@part_sort = :id
end
erb :project
end
get "/projects/:id/edit" do
require_permission(@user.can_edit?)
erb :project_edit
end
post "/projects/:id/edit" do
require_permission(@user.can_edit?)
@project.name = params[:name] if params[:name]
if params[:part_number_prefix]
@project.part_number_prefix = params[:part_number_prefix]
end
@project.save
redirect "/projects/#{params[:id]}"
end
get "/projects/:id/delete" do
require_permission(@user.can_edit?)
erb :project_delete
end
post "/projects/:id/delete" do
require_permission(@user.can_edit?)
@project.delete
redirect "/projects"
end
get "/projects/:id/dashboard" do
erb :dashboard
end
get "/projects/:id/dashboard/parts" do
@status = params[:status] if Part::STATUS_MAP.has_key?(params[:status])
erb :dashboard_parts
end
get "/projects/:id/new_part" do
require_permission(@user.can_edit?)
@parent_part_id = params[:parent_part_id]
@type = params[:type] || "part"
halt(400, "Invalid part type.") unless Part::PART_TYPES.include?(@type)
erb :new_part
end
get "/dashboards" do
erb :dashboards
end
post "/parts" do
require_permission(@user.can_edit?)
# Check parameter existence and format.
halt(400, "Missing project ID.") if params[:project_id].nil? || params[:project_id] !~ /^\d+$/
halt(400, "Missing part type.") if params[:type].nil?
halt(400, "Invalid part type.") unless Part::PART_TYPES.include?(params[:type])
halt(400, "Missing part name.") if params[:name].nil? || params[:name].empty?
if params[:parent_part_id] && params[:parent_part_id] !~ /^\d+$/
halt(400, "Invalid parent part ID.")
end
project = Project[params[:project_id].to_i]
halt(400, "Invalid project.") if project.nil?
parent_part = nil
if params[:parent_part_id]
parent_part = Part[:id => params[:parent_part_id].to_i, :project_id => project.id,
:type => "assembly"]
halt(400, "Invalid parent part.") if parent_part.nil?
end
part = Part.generate_number_and_create(project, params[:type], parent_part)
part.name = params[:name].gsub("\"", """)
part.status = "designing"
part.source_material = ""
part.have_material = 0
part.quantity = ""
part.cut_length = ""
part.priority = 1
part.drawing_created = 0
part.save
redirect "/parts/#{part.id}"
end
get "/parts/:id" do
@part = Part[params[:id]]
halt(400, "Invalid part.") if @part.nil?
if ["type", "name", "parent_part_id", "status"].include?(params[:sort])
@part_sort = params[:sort].to_sym
else
@part_sort = :id
end
erb :part
end
get "/parts/:id/edit" do
require_permission(@user.can_edit?)
@part = Part[params[:id]]
halt(400, "Invalid part.") if @part.nil?
@referrer = request.referrer
erb :part_edit
end
post "/parts/:id/edit" do
require_permission(@user.can_edit?)
@part = Part[params[:id]]
halt(400, "Invalid part.") if @part.nil?
halt(400, "Missing part name.") if params[:name] && params[:name].empty?
@part.name = params[:name].gsub("\"", """) if params[:name]
if params[:status]
halt(400, "Invalid status.") unless Part::STATUS_MAP.include?(params[:status])
@part.status = params[:status]
end
@part.notes = params[:notes] if params[:notes]
@part.source_material = params[:source_material] if params[:source_material]
@part.have_material = (params[:have_material] == "on") ? 1 : 0 if params[:have_material]
@part.cut_length = params[:cut_length] if params[:cut_length]
@part.quantity = params[:quantity] if params[:quantity]
@part.drawing_created = (params[:drawing_created] == "on") ? 1 : 0 if params[:drawing_created]
@part.priority = params[:priority] if params[:priority]
@part.save
redirect params[:referrer] || "/parts/#{params[:id]}" unless !params[:redirect].nil? && params[:redirect]
end
get "/parts/:id/delete" do
require_permission(@user.can_edit?)
@part = Part[params[:id]]
halt(400, "Invalid part.") if @part.nil?
@referrer = request.referrer
erb :part_delete
end
post "/parts/:id/delete" do
require_permission(@user.can_edit?)
@part = Part[params[:id]]
project_id = @part.project_id
halt(400, "Invalid part.") if @part.nil?
halt(400, "Can't delete assembly with existing children.") unless @part.child_parts.empty?
@part.delete
params[:referrer] = nil if params[:referrer] =~ /\/parts\/#{params[:id]}$/
redirect params[:referrer] || "/projects/#{project_id}"
end
get "/new_user" do
require_permission(@user.can_administer?)
@admin_new_user = true
erb :new_user
end
get "/users" do
require_permission(@user.can_administer?)
erb :users
end
post "/users" do
require_permission(@user.can_administer?)
halt(400, "Missing email.") if params[:email].nil? || params[:email].empty?
halt(400, "Invalid email.") unless params[:email] =~ /^\S+@\S+\.\S+$/
halt(400, "User #{params[:email]} already exists.") if User[:email => params[:email]]
halt(400, "Missing first name.") if params[:first_name].nil? || params[:first_name].empty?
halt(400, "Missing last name.") if params[:last_name].nil? || params[:last_name].empty?
halt(400, "Missing password.") if params[:password].nil? || params[:password].empty?
halt(400, "Missing permission.") if params[:permission].nil? || params[:permission].empty?
halt(400, "Invalid permission.") unless User::PERMISSION_MAP.include?(params[:permission])
user = User.new(:email => params[:email], :first_name => params[:first_name],
:last_name => params[:last_name], :permission => params[:permission],
:enabled => (params[:enabled] == "on") ? 1 : 0)
user.set_password(params[:password])
user.save
redirect "/users"
end
get "/users/:id/edit" do
require_permission(@user.can_administer?)
@user_edit = User[params[:id]]
halt(400, "Invalid user.") if @user_edit.nil?
erb :user_edit
end
post "/users/:id/edit" do
require_permission(@user.can_administer?)
@user_edit = User[params[:id]]
halt(400, "Invalid user.") if @user_edit.nil?
@user_edit.email = params[:email] if params[:email]
@user_edit.first_name = params[:first_name] if params[:first_name]
@user_edit.last_name = params[:last_name] if params[:last_name]
@user_edit.set_password(params[:password]) if params[:password] && !params[:password].empty?
@user_edit.permission = params[:permission] if params[:permission]
old_enabled = @user_edit.enabled
@user_edit.enabled = (params[:enabled] == "on") ? 1 : 0
if @user_edit.enabled == 1 && old_enabled == 0
email_body = <<-EOS.dedent
Hello #{@user_edit.first_name},
Your account on Cheesy Parts has been approved.
You can log into the system at #{URL}.
Cheers,
The Cheesy Parts Robot
EOS
send_email(@user_edit.email, "Account approved", email_body)
end
@user_edit.save
redirect "/users"
end
get "/users/:id/delete" do
require_permission(@user.can_administer?)
@user_delete = User[params[:id]]
halt(400, "Invalid user.") if @user_delete.nil?
erb :user_delete
end
post "/users/:id/delete" do
require_permission(@user.can_administer?)
@user_delete = User[params[:id]]
halt(400, "Invalid user.") if @user_delete.nil?
@user_delete.delete
redirect "/users"
end
get "/change_password" do
erb :change_password
end
post "/change_password" do
halt(400, "Missing password.") if params[:password].nil? || params[:password].empty?
halt(400, "Invalid old password.") unless User.authenticate(@user.email, params[:old_password])
@user.set_password(params[:password])
@user.save
redirect "/"
end
get "/register" do
@admin_new_user = false
erb :new_user
end
post "/register" do
halt(400, "Missing email.") if params[:email].nil? || params[:email].empty?
halt(400, "Invalid email.") unless params[:email] =~ /^\S+@\S+\.\S+$/
halt(400, "User #{params[:email]} already exists.") if User[:email => params[:email]]
halt(400, "Missing first name.") if params[:first_name].nil? || params[:first_name].empty?
halt(400, "Missing last name.") if params[:last_name].nil? || params[:last_name].empty?
halt(400, "Missing password.") if params[:password].nil? || params[:password].empty?
user = User.new(:email => params[:email], :first_name => params[:first_name],
:last_name => params[:last_name], :permission => "readonly",
:enabled => 0)
user.set_password(params[:password])
user.save
email_body = <<-EOS.dedent
Hello,
This is a notification that #{user.first_name} #{user.last_name} has created an account on Cheesy
Parts and it is disabled pending approval.
Please visit the user control panel at #{URL}/users to take action.
Cheers,
The Cheesy Parts Robot
EOS
send_email(CheesyCommon::Config.gmail_user, "Approval needed for #{user.email}", email_body)
erb :register_confirmation
end
get "/orders" do
erb :orders_project_list
end
get "/projects/:id/orders/open" do
@no_vendor_order_items = OrderItem.where(:order_id => nil, :project_id => params[:id])
@vendor_orders = Order.filter(:status => "open").where(:project_id => params[:id]).
order(:vendor_name, :ordered_at)
@show_new_item_form = params[:new_item] == "true"
erb :open_orders
end
get "/projects/:id/orders/ordered" do
@vendor_orders = Order.filter(:status => "ordered").where(:project_id => params[:id]).
order(:vendor_name, :ordered_at)
erb :completed_orders
end
get "/projects/:id/orders/complete" do
@vendor_orders = Order.filter(:status => "received").where(:project_id => params[:id]).
order(:vendor_name, :ordered_at)
erb :completed_orders
end
get "/projects/:id/orders/all" do
@vendor_orders = Order.where(:project_id => params[:id]).order(:vendor_name, :ordered_at)
if params[:filter]
key, value = params[:filter].split(":")
@vendor_orders = @vendor_orders.filter(key.to_sym => value)
end
erb :completed_orders
end
get "/projects/:id/orders/stats" do
@orders = Order.filter(:status => "open").invert.where(:project_id => params[:id]).all
@orders_by_vendor = @orders.inject({}) do |map, order|
map[order.vendor_name] ||= []
map[order.vendor_name] << order
map
end
@orders_by_purchaser = @orders.inject({}) do |map, order|
map[order.paid_for_by] ||= {}
map[order.paid_for_by][:reimbursed] ||= 0
map[order.paid_for_by][:outstanding] ||= 0
if order.reimbursed == 1
map[order.paid_for_by][:reimbursed] += order.total_cost
else
map[order.paid_for_by][:outstanding] += order.total_cost
end
map
end
erb :order_stats
end
post "/projects/:id/order_items" do
require_permission(@user.can_edit?)
# Match vendor to an existing open order or create it if there isn't one.
if params[:vendor].nil? || params[:vendor].empty?
order_id = nil
else
order = Order.where(:project_id => @project.id, :vendor_name => params[:vendor],
:status => "open").first
if order.nil?
order = Order.create(:project => @project, :vendor_name => params[:vendor], :status => "open")
end
order_id = order.id
end
OrderItem.create(:project => @project, :order_id => order_id, :quantity => params[:quantity].to_i,
:part_number => params[:part_number], :description => params[:description],
:unit_cost => params[:unit_cost].to_f, :notes => params[:notes])
redirect "/projects/#{@project.id}/orders/open"
end
get "/projects/:project_id/order_items/:id/editable" do
require_permission(@user.can_edit?)
@item = OrderItem[params[:id]]
halt(400, "Invalid order item.") if @item.nil?
erb :edit_order_item
end
post "/projects/:project_id/order_items/edit" do
require_permission(@user.can_edit?)
@item = OrderItem[params[:order_item_id]]
halt(400, "Invalid order item.") if @item.nil?
# Handle a vendor change.
order_id = @item.order.id rescue nil
old_vendor = @item.order.vendor_name rescue ""
new_vendor = params[:vendor]
unless old_vendor == new_vendor
order = Order.where(:project_id => @project.id, :vendor_name => params[:vendor],
:status => "open").first
if order.nil?
order = Order.create(:project => @project, :vendor_name => params[:vendor], :status => "open")
end
order_id = order.id
end
@item.update(:order_id => order_id, :quantity => params[:quantity].to_i,
:part_number => params[:part_number], :description => params[:description],
:unit_cost => params[:unit_cost].gsub(/\$/, "").to_f, :notes => params[:notes])
redirect params[:referrer]
end
get "/projects/:project_id/order_items/:id/delete" do
require_permission(@user.can_edit?)
@item = OrderItem[params[:id]]
halt(400, "Invalid order item.") if @item.nil?
@referrer = request.referrer
erb :order_item_delete
end
post "/projects/:project_id/order_items/:id/delete" do
require_permission(@user.can_edit?)
@item = OrderItem[params[:id]]
halt(400, "Invalid order item.") if @item.nil?
@item.delete
redirect params[:referrer]
end
get "/projects/:id/orders/:order_id" do
@order = Order[params[:order_id]]
halt(400, "Invalid order.") if @order.nil?
erb :order
end
post "/projects/:id/orders/:order_id/edit" do
require_permission(@user.can_edit?)
@order = Order[params[:order_id]]
halt(400, "Invalid order.") if @order.nil?
@order.update(:status => params[:status], :ordered_at => params[:ordered_at],
:paid_for_by => params[:paid_for_by], :tax_cost => params[:tax_cost].gsub(/\$/, ""),
:shipping_cost => params[:shipping_cost].gsub(/\$/, ""), :notes => params[:notes],
:reimbursed => params[:reimbursed] ? 1 : 0)
redirect "/projects/#{@project.id}/orders/#{@order.id}"
end
get "/projects/:id/orders/:order_id/delete" do
require_permission(@user.can_edit?)
@order = Order[params[:order_id]]
halt(400, "Invalid order.") if @order.nil?
halt(400, "Can't delete a non-empty order.") unless @order.order_items.empty?
erb :order_delete
end
post "/projects/:id/orders/:order_id/delete" do
require_permission(@user.can_edit?)
@order = Order[params[:order_id]]
halt(400, "Invalid order.") if @order.nil?
halt(400, "Can't delete a non-empty order.") unless @order.order_items.empty?
@order.delete
redirect "/orders"
end
end
end