Description:
modify user list creation into user list update
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r469:b28cd479ede6 - - 1 file changed: 11 inserted, 5 deleted
@@ -1,187 +1,193 | |||||
|
1 | require 'csv' |
|
1 | require 'csv' |
|
2 |
|
2 | ||
|
3 | class UserAdminController < ApplicationController |
|
3 | class UserAdminController < ApplicationController |
|
4 |
|
4 | ||
|
5 |
|
5 | ||
|
6 | include MailHelperMethods |
|
6 | include MailHelperMethods |
|
7 |
|
7 | ||
|
8 | before_filter :admin_authorization |
|
8 | before_filter :admin_authorization |
|
9 |
|
9 | ||
|
10 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
10 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
11 | verify :method => :post, :only => [ :destroy, |
|
11 | verify :method => :post, :only => [ :destroy, |
|
12 | :create, :create_from_list, |
|
12 | :create, :create_from_list, |
|
13 | :update, |
|
13 | :update, |
|
14 | :manage_contest, |
|
14 | :manage_contest, |
|
15 | :bulk_mail |
|
15 | :bulk_mail |
|
16 | ], |
|
16 | ], |
|
17 | :redirect_to => { :action => :list } |
|
17 | :redirect_to => { :action => :list } |
|
18 |
|
18 | ||
|
19 | def index |
|
19 | def index |
|
20 | list |
|
20 | list |
|
21 | render :action => 'list' |
|
21 | render :action => 'list' |
|
22 | end |
|
22 | end |
|
23 |
|
23 | ||
|
24 | def list |
|
24 | def list |
|
25 | @user_count = User.count |
|
25 | @user_count = User.count |
|
26 | if params[:page] == 'all' |
|
26 | if params[:page] == 'all' |
|
27 | @users = User.all |
|
27 | @users = User.all |
|
28 | @paginated = false |
|
28 | @paginated = false |
|
29 | else |
|
29 | else |
|
30 | @users = User.paginate :page => params[:page] |
|
30 | @users = User.paginate :page => params[:page] |
|
31 | @paginated = true |
|
31 | @paginated = true |
|
32 | end |
|
32 | end |
|
33 | @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] |
|
33 | @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at'] |
|
34 | @contests = Contest.enabled |
|
34 | @contests = Contest.enabled |
|
35 | end |
|
35 | end |
|
36 |
|
36 | ||
|
37 | def active |
|
37 | def active |
|
38 | sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago]) |
|
38 | sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago]) |
|
39 | @users = [] |
|
39 | @users = [] |
|
40 | sessions.each do |session| |
|
40 | sessions.each do |session| |
|
41 | if session.data[:user_id] |
|
41 | if session.data[:user_id] |
|
42 | @users << User.find(session.data[:user_id]) |
|
42 | @users << User.find(session.data[:user_id]) |
|
43 | end |
|
43 | end |
|
44 | end |
|
44 | end |
|
45 | end |
|
45 | end |
|
46 |
|
46 | ||
|
47 | def show |
|
47 | def show |
|
48 | @user = User.find(params[:id]) |
|
48 | @user = User.find(params[:id]) |
|
49 | end |
|
49 | end |
|
50 |
|
50 | ||
|
51 | def new |
|
51 | def new |
|
52 | @user = User.new |
|
52 | @user = User.new |
|
53 | end |
|
53 | end |
|
54 |
|
54 | ||
|
55 | def create |
|
55 | def create |
|
56 | @user = User.new(params[:user]) |
|
56 | @user = User.new(params[:user]) |
|
57 | @user.activated = true |
|
57 | @user.activated = true |
|
58 | if @user.save |
|
58 | if @user.save |
|
59 | flash[:notice] = 'User was successfully created.' |
|
59 | flash[:notice] = 'User was successfully created.' |
|
60 | redirect_to :action => 'list' |
|
60 | redirect_to :action => 'list' |
|
61 | else |
|
61 | else |
|
62 | render :action => 'new' |
|
62 | render :action => 'new' |
|
63 | end |
|
63 | end |
|
64 | end |
|
64 | end |
|
65 |
|
65 | ||
|
66 | def create_from_list |
|
66 | def create_from_list |
|
67 | lines = params[:user_list] |
|
67 | lines = params[:user_list] |
|
68 |
|
68 | ||
|
69 | note = [] |
|
69 | note = [] |
|
70 |
|
70 | ||
|
71 | lines.split("\n").each do |line| |
|
71 | lines.split("\n").each do |line| |
|
72 | items = line.chomp.split(',') |
|
72 | items = line.chomp.split(',') |
|
73 | if items.length>=2 |
|
73 | if items.length>=2 |
|
74 | login = items[0] |
|
74 | login = items[0] |
|
75 | full_name = items[1] |
|
75 | full_name = items[1] |
|
76 |
|
76 | ||
|
77 | added_random_password = false |
|
77 | added_random_password = false |
|
78 | if items.length>=3 |
|
78 | if items.length>=3 |
|
79 | password = items[2].chomp(" ") |
|
79 | password = items[2].chomp(" ") |
|
80 | user_alias = (items.length>=4) ? items[3] : login |
|
80 | user_alias = (items.length>=4) ? items[3] : login |
|
81 | else |
|
81 | else |
|
82 | password = random_password |
|
82 | password = random_password |
|
83 | user_alias = (items.length>=4) ? items[3] : login |
|
83 | user_alias = (items.length>=4) ? items[3] : login |
|
84 | added_random_password = true |
|
84 | added_random_password = true |
|
85 | end |
|
85 | end |
|
86 |
|
86 | ||
|
87 |
- user = User. |
|
87 | + user = User.find_by_login(login) |
|
88 | - :full_name => full_name, |
|
88 | + if (user) |
|
89 | - :password => password, |
|
89 | + user.full_name = full_name |
|
90 |
- |
|
90 | + user.password = password |
|
91 | - :alias => user_alias}) |
|
91 | + else |
|
|
92 | + user = User.new({:login => login, | ||
|
|
93 | + :full_name => full_name, | ||
|
|
94 | + :password => password, | ||
|
|
95 | + :password_confirmation => password, | ||
|
|
96 | + :alias => user_alias}) | ||
|
|
97 | + end | ||
|
92 | user.activated = true |
|
98 | user.activated = true |
|
93 | user.save |
|
99 | user.save |
|
94 |
|
100 | ||
|
95 | if added_random_password |
|
101 | if added_random_password |
|
96 | note << "'#{login}' (+)" |
|
102 | note << "'#{login}' (+)" |
|
97 | else |
|
103 | else |
|
98 | note << login |
|
104 | note << login |
|
99 | end |
|
105 | end |
|
100 | end |
|
106 | end |
|
101 | end |
|
107 | end |
|
102 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
108 | flash[:notice] = 'User(s) ' + note.join(', ') + |
|
103 | ' were successfully created. ' + |
|
109 | ' were successfully created. ' + |
|
104 | '( (+) - created with random passwords.)' |
|
110 | '( (+) - created with random passwords.)' |
|
105 | redirect_to :action => 'list' |
|
111 | redirect_to :action => 'list' |
|
106 | end |
|
112 | end |
|
107 |
|
113 | ||
|
108 | def edit |
|
114 | def edit |
|
109 | @user = User.find(params[:id]) |
|
115 | @user = User.find(params[:id]) |
|
110 | end |
|
116 | end |
|
111 |
|
117 | ||
|
112 | def update |
|
118 | def update |
|
113 | @user = User.find(params[:id]) |
|
119 | @user = User.find(params[:id]) |
|
114 | if @user.update_attributes(params[:user]) |
|
120 | if @user.update_attributes(params[:user]) |
|
115 | flash[:notice] = 'User was successfully updated.' |
|
121 | flash[:notice] = 'User was successfully updated.' |
|
116 | redirect_to :action => 'show', :id => @user |
|
122 | redirect_to :action => 'show', :id => @user |
|
117 | else |
|
123 | else |
|
118 | render :action => 'edit' |
|
124 | render :action => 'edit' |
|
119 | end |
|
125 | end |
|
120 | end |
|
126 | end |
|
121 |
|
127 | ||
|
122 | def destroy |
|
128 | def destroy |
|
123 | User.find(params[:id]).destroy |
|
129 | User.find(params[:id]).destroy |
|
124 | redirect_to :action => 'list' |
|
130 | redirect_to :action => 'list' |
|
125 | end |
|
131 | end |
|
126 |
|
132 | ||
|
127 | def user_stat |
|
133 | def user_stat |
|
128 | if params[:commit] == 'download csv' |
|
134 | if params[:commit] == 'download csv' |
|
129 | @problems = Problem.all |
|
135 | @problems = Problem.all |
|
130 | else |
|
136 | else |
|
131 | @problems = Problem.find_available_problems |
|
137 | @problems = Problem.find_available_problems |
|
132 | end |
|
138 | end |
|
133 | @users = User.find(:all, :include => [:contests, :contest_stat]) |
|
139 | @users = User.find(:all, :include => [:contests, :contest_stat]) |
|
134 | @scorearray = Array.new |
|
140 | @scorearray = Array.new |
|
135 | @users.each do |u| |
|
141 | @users.each do |u| |
|
136 | ustat = Array.new |
|
142 | ustat = Array.new |
|
137 | ustat[0] = u |
|
143 | ustat[0] = u |
|
138 | @problems.each do |p| |
|
144 | @problems.each do |p| |
|
139 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
145 | sub = Submission.find_last_by_user_and_problem(u.id,p.id) |
|
140 | if (sub!=nil) and (sub.points!=nil) |
|
146 | if (sub!=nil) and (sub.points!=nil) |
|
141 | ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] |
|
147 | ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)] |
|
142 | else |
|
148 | else |
|
143 | ustat << [0,false] |
|
149 | ustat << [0,false] |
|
144 | end |
|
150 | end |
|
145 | end |
|
151 | end |
|
146 | @scorearray << ustat |
|
152 | @scorearray << ustat |
|
147 | end |
|
153 | end |
|
148 |
|
154 | ||
|
149 | if params[:commit] == 'download csv' then |
|
155 | if params[:commit] == 'download csv' then |
|
150 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
156 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
151 | send_data csv, filename: 'last_score.csv' |
|
157 | send_data csv, filename: 'last_score.csv' |
|
152 | else |
|
158 | else |
|
153 | render template: 'user_admin/user_stat' |
|
159 | render template: 'user_admin/user_stat' |
|
154 | end |
|
160 | end |
|
155 | end |
|
161 | end |
|
156 |
|
162 | ||
|
157 | def user_stat_max |
|
163 | def user_stat_max |
|
158 | if params[:commit] == 'download csv' |
|
164 | if params[:commit] == 'download csv' |
|
159 | @problems = Problem.all |
|
165 | @problems = Problem.all |
|
160 | else |
|
166 | else |
|
161 | @problems = Problem.find_available_problems |
|
167 | @problems = Problem.find_available_problems |
|
162 | end |
|
168 | end |
|
163 | @users = User.find(:all, :include => [:contests, :contest_stat]) |
|
169 | @users = User.find(:all, :include => [:contests, :contest_stat]) |
|
164 | @scorearray = Array.new |
|
170 | @scorearray = Array.new |
|
165 | #set up range from param |
|
171 | #set up range from param |
|
166 | since_id = params.fetch(:since_id, 0).to_i |
|
172 | since_id = params.fetch(:since_id, 0).to_i |
|
167 | until_id = params.fetch(:until_id, 0).to_i |
|
173 | until_id = params.fetch(:until_id, 0).to_i |
|
168 | @users.each do |u| |
|
174 | @users.each do |u| |
|
169 | ustat = Array.new |
|
175 | ustat = Array.new |
|
170 | ustat[0] = u |
|
176 | ustat[0] = u |
|
171 | @problems.each do |p| |
|
177 | @problems.each do |p| |
|
172 | max_points = 0 |
|
178 | max_points = 0 |
|
173 | Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub| |
|
179 | Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub| |
|
174 | max_points = sub.points if sub and sub.points and (sub.points > max_points) |
|
180 | max_points = sub.points if sub and sub.points and (sub.points > max_points) |
|
175 | end |
|
181 | end |
|
176 | ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)] |
|
182 | ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)] |
|
177 | end |
|
183 | end |
|
178 | @scorearray << ustat |
|
184 | @scorearray << ustat |
|
179 | end |
|
185 | end |
|
180 |
|
186 | ||
|
181 | if params[:commit] == 'download csv' then |
|
187 | if params[:commit] == 'download csv' then |
|
182 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
188 | csv = gen_csv_from_scorearray(@scorearray,@problems) |
|
183 | send_data csv, filename: 'max_score.csv' |
|
189 | send_data csv, filename: 'max_score.csv' |
|
184 | else |
|
190 | else |
|
185 | render template: 'user_admin/user_stat' |
|
191 | render template: 'user_admin/user_stat' |
|
186 | end |
|
192 | end |
|
187 | end |
|
193 | end |
You need to be logged in to leave comments.
Login now