6
6
from drls .utils import read_excel , cal_range , file_exists ,allowed_file ,app_dir ,load_md ,JSONR
7
7
from drls .rng import RandomGenerator
8
8
from werkzeug .utils import secure_filename
9
- import hashlib
10
9
11
10
import os
12
- import json
13
11
import drls .errcode as ERRCODE
14
12
13
+
14
+
15
15
blueprint = Blueprint ('public' , __name__ , static_folder = '../static' )
16
16
17
17
@@ -22,8 +22,8 @@ def home():
22
22
# if lock.txt exits, hide select excel form
23
23
# 如果 studta.xls文件存在,则显示之前的random连接
24
24
# 如果 lock.txt文件存在,则不显示选取文件表单
25
- show_previous = file_exists (os . path . join ( app .config [ 'UPLOAD_FOLDER' ], app . config [ 'STUDATA_FILE_NAME' ]) )
26
- lock_form = file_exists (os . path . join ( app .config [ 'UPLOAD_FOLDER' ], app . config [ 'LOCK_FILE_NAME' ]) )
25
+ show_previous = file_exists (app .config . xls_file_path )
26
+ lock_form = file_exists (app .config . lock_file_path )
27
27
return render_template ('public/home.html' , data_exist = show_previous , lock = lock_form )
28
28
29
29
@blueprint .route ('/about/' )
@@ -41,34 +41,37 @@ def upload():
41
41
app .logger .info (seed )
42
42
app .logger .info (num )
43
43
# check passwd
44
- lock_file_path = os .path .join (app .config ['UPLOAD_FOLDER' ],app .config ['LOCK_FILE_NAME' ])
45
- lock = file_exists (lock_file_path )
44
+ lock = file_exists (app .config .lock_file_path )
46
45
if lock :
47
46
passwd = request .form ['passwd' ]
48
47
app .logger .info (passwd )
49
48
app .logger .info (lock )
50
- passwd_file = open (lock_file_path ,'r' )
51
- real_passwd = passwd_file .readline ().strip ()
52
- passwd_file .close ()
49
+ app .config .lock_file_lock .acquire ()
50
+ passwd_file = open (app .config .lock_file_path ,'r' )
51
+ try :
52
+ real_passwd = passwd_file .readline ().strip ()
53
+ finally :
54
+ passwd_file .close ()
55
+ app .config .lock_file_lock .release ()
53
56
if real_passwd != passwd :
54
57
return JSONR (ERRCODE .UNAUTHORIZED ,'password was wrong' )
55
- else :
56
- pass
57
- seed_file_path = os .path .join (app .config ['UPLOAD_FOLDER' ], app .config ['RANDOMSEED_FILE_NAME' ])
58
- num_file_path = os .path .join (app .config ['UPLOAD_FOLDER' ], app .config ['RANDOMNUM_FILE_NAME' ])
59
58
60
59
# save random_seed
61
- seed_file = open (seed_file_path ,'w' )
60
+ app .config .seed_file_lock .acquire ()
61
+ seed_file = open (app .config .seed_file_path ,'w' )
62
62
try :
63
63
random_seed = int (seed )
64
64
seed_file .write (str (random_seed ))
65
65
except ValueError :
66
66
return JSONR (ERRCODE .FORMAT_ERROR ,'random seed format error' , seed )
67
67
finally :
68
68
seed_file .close ()
69
+ app .config .seed_file_lock .release ()
69
70
71
+ app .config .num_file_lock .acquire ()
70
72
# save random_num
71
- num_file = open (num_file_path ,'w' )
73
+ num_file = open (app .config .num_file_path ,'w' )
74
+
72
75
try :
73
76
random_num = int (num )
74
77
num_file .write (str (random_num ))
@@ -77,67 +80,73 @@ def upload():
77
80
return JSONR (ERRCODE .FORMAT_ERROR ,'random num format error' , num )
78
81
finally :
79
82
num_file .close ()
83
+ app .config .num_file_lock .release ()
80
84
81
85
# save stu file
82
- file = request .files ['fileUploaded' ]
83
- if file and allowed_file (file .filename ,app .config ['ALLOWED_EXTENSIONS' ]):
84
- filename = secure_filename (file .filename )
85
- # app.logger.info(filename.split('.',1)[0])
86
- # target_filename = filename.split('.',1)[0] + '-' + str(hashlib.sha224(filename).hexdigest()) +'.'+ filename.split('.',1)[1]
87
- # TODO only support xls type file
88
- target_filename = app .config ['STUDATA_FILE_NAME' ]
89
- file .save (os .path .join (app .config ['UPLOAD_FOLDER' ], target_filename ))
90
- # return redirect(url_for('public.random',filename=filename))
91
- return JSONR (ERRCODE .SUCCESS ,'success' )
92
- else :
93
- return JSONR (ERRCODE .INVALID_FILE ,'invalid file extension' )
86
+ app .config .xls_file_lock .acquire ()
87
+ try :
88
+ file = request .files ['fileUploaded' ]
89
+ if file and allowed_file (file .filename ,app .config ['ALLOWED_EXTENSIONS' ]):
90
+ filename = secure_filename (file .filename )
91
+ # app.logger.info(filename.split('.',1)[0])
92
+ # target_filename = filename.split('.',1)[0] + '-' + str(hashlib.sha224(filename).hexdigest()) +'.'+ filename.split('.',1)[1]
93
+ # TODO only support xls type file
94
+ target_filename = app .config ['STUDATA_FILE_NAME' ]
95
+ file .save (os .path .join (app .config ['UPLOAD_FOLDER' ], target_filename ))
96
+ # return redirect(url_for('public.random',filename=filename))
97
+ return JSONR (ERRCODE .SUCCESS ,'success' )
98
+ else :
99
+ return JSONR (ERRCODE .INVALID_FILE ,'invalid file extension' )
100
+ finally :
101
+ app .config .xls_file_lock .release ()
102
+
94
103
return JSONR (ERRCODE .INVALID_REQUEST ,'only support post method' )
95
104
96
105
@blueprint .route ('/random/' , methods = ['GET' ])
97
106
def random ():
98
107
"""random page."""
99
108
if file_exists (os .path .join (app .config ['UPLOAD_FOLDER' ],app .config ['STUDATA_FILE_NAME' ])):
100
- seed_file_path = os .path .join (app .config ['UPLOAD_FOLDER' ],app .config ['RANDOMSEED_FILE_NAME' ])
101
- num_file_path = os .path .join (app .config ['UPLOAD_FOLDER' ],app .config ['RANDOMNUM_FILE_NAME' ])
102
109
103
110
random_num = 0
104
111
random_seed = 0
105
- if file_exists (seed_file_path ):
106
- seed_file = open (seed_file_path )
112
+ app .config .seed_file_lock .acquire ()
113
+ if file_exists (app .config .seed_file_path ):
114
+ seed_file = open (app .config .seed_file_path )
107
115
try :
108
116
seed = seed_file .readline ().strip ()
109
117
random_seed = int (seed )
110
118
except ValueError :
111
119
return render_template ('public/random.html' ,random_seed = 0 , random_num = 0 )
112
120
finally :
113
121
seed_file .close ()
122
+ app .config .seed_file_lock .release ()
114
123
115
- if file_exists (num_file_path ):
116
- num_file = open (num_file_path )
124
+ app .config .num_file_lock .acquire ()
125
+ if file_exists (app .config .num_file_path ):
126
+ num_file = open (app .config .num_file_path )
117
127
try :
118
128
num = num_file .readline ().strip ()
119
129
random_num = int (num )
120
130
except ValueError :
121
131
return render_template ('public/random.html' ,random_seed = random_seed , random_num = 0 )
122
132
finally :
123
133
num_file .close ()
124
- return render_template ('public/random.html' ,random_seed = random_seed , random_num = random_num )
134
+ app .config .num_file_lock .release ()
135
+ return render_template ('public/random.html' ,random_seed = random_seed , random_num = random_num )
125
136
126
137
return redirect (url_for ('public.home' ))
127
138
128
139
129
140
@blueprint .route ('/rand/' , methods = ['POST' ])
130
141
def rand ():
131
142
"""rand"""
132
- xls_file = os .path .join (app .config ['UPLOAD_FOLDER' ],app .config ['STUDATA_FILE_NAME' ])
133
143
# get random seed
134
- seed_file_path = os .path .join (app .config ['UPLOAD_FOLDER' ],app .config ['RANDOMSEED_FILE_NAME' ])
135
- num_file_path = os .path .join (app .config ['UPLOAD_FOLDER' ],app .config ['RANDOMNUM_FILE_NAME' ])
136
144
137
145
random_num = 0
138
146
random_seed = 0
139
- if file_exists (seed_file_path ):
140
- seed_file = open (seed_file_path )
147
+ app .config .seed_file_lock .acquire ()
148
+ if file_exists (app .config .seed_file_path ):
149
+ seed_file = open (app .config .seed_file_path )
141
150
seed = 0
142
151
try :
143
152
seed = seed_file .readline ().strip ()
@@ -146,8 +155,11 @@ def rand():
146
155
return JSONR (ERRCODE .FORMAT_ERROR ,'convert random seed failed' ,seed )
147
156
finally :
148
157
seed_file .close ()
149
- if file_exists (num_file_path ):
150
- num_file = open (num_file_path )
158
+ app .config .seed_file_lock .release ()
159
+
160
+ app .config .num_file_lock .acquire ()
161
+ if file_exists (app .config .num_file_path ):
162
+ num_file = open (app .config .num_file_path )
151
163
num = 0
152
164
try :
153
165
num = num_file .readline ().strip ()
@@ -156,17 +168,24 @@ def rand():
156
168
return JSONR (ERRCODE .FORMAT_ERROR ,'convert random num failed' ,num )
157
169
finally :
158
170
num_file .close ()
171
+ app .config .num_file_lock .release ()
172
+
159
173
# calc random result
160
- if file_exists (xls_file ):
174
+ app .config .xls_file_lock .acquire ()
175
+ if file_exists (app .config .xls_file_path ):
161
176
# rand
162
- dicts = read_excel (xls_file )
163
- res = RandomGenerator (random_seed , cal_range (dicts ), random_num , dicts )
164
- studs = res .GenerateResult ()
165
- res = []
166
- for key in dicts :
167
- res .append ({"key" :key ,"value" :dicts [key ]})
168
- app .logger .info (res )
169
- data = {'allstus' : res ,'studs' :studs }
170
- return JSONR (ERRCODE .SUCCESS ,'success' ,data )
177
+ try :
178
+ dicts = read_excel (app .config .xls_file_path )
179
+ res = RandomGenerator (random_seed , cal_range (dicts ), random_num , dicts )
180
+ studs = res .GenerateResult ()
181
+ res = []
182
+ for key in dicts :
183
+ res .append ({"key" :key ,"value" :dicts [key ]})
184
+ app .logger .info (res )
185
+ data = {'allstus' : res ,'studs' :studs }
186
+
187
+ return JSONR (ERRCODE .SUCCESS ,'success' ,data )
188
+ finally :
189
+ app .config .xls_file_lock .release ()
171
190
return JSONR (ERRCODE .UNKNOW ,'failed' )
172
191
0 commit comments