/
ussdefect1.py
1098 lines (919 loc) · 42 KB
/
ussdefect1.py
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding:utf-8
from bottle import route, run, Bottle, template, request, abort, redirect, static_file
import sqlite3 as sqlite
import os
import sys
import time
import datetime
from users import login_required
from urllib import urlencode, unquote
import re
import random
from pprint import pprint
from pychart import *
import subprocess
date_p = re.compile(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})")
_localDir=os.path.dirname(__file__)
_curpath=os.path.normpath(os.path.join(os.getcwd(),_localDir))
_staticpath = os.path.join(_curpath,"static")
#_picpath = os.path.join(_staticpath,"pics")
_defectpicpath = os.path.join(_staticpath,"ussdefects")
make_defect_py = os.path.join(_curpath,"ussdefectpic1.py")
defect_fun_fn1 = "ussdefect1"
defect_csv_fn1 = os.path.join(_defectpicpath,"ussdefect1.csv")
defect_eps_fn1 = os.path.join(_defectpicpath,"ussdefect1.eps")
defect_png_fn1 = os.path.join(_defectpicpath,"ussdefect1.png")
defect_fun_fn2 = "ussdefect2"
defect_csv_fn2 = os.path.join(_defectpicpath,"ussdefect2.csv")
defect_eps_fn2 = os.path.join(_defectpicpath,"ussdefect2.eps")
defect_png_fn2 = os.path.join(_defectpicpath,"ussdefect2.png")
defect_fun_fn3 = "ussdefect3"
defect_csv_fn3 = os.path.join(_defectpicpath,"ussdefect3.csv")
defect_eps_fn3 = os.path.join(_defectpicpath,"ussdefect3.eps")
defect_png_fn3 = os.path.join(_defectpicpath,"ussdefect3.png")
project_fun_fn0 = "ussproject0"
project_csv_fn0 = os.path.join(_defectpicpath,"ussproject0.csv")
project_eps_fn0 = os.path.join(_defectpicpath,"ussproject0.eps")
project_png_fn0 = os.path.join(_defectpicpath,"ussproject0.png")
project_fun_did = "ussprojectdid"
ussfvt_users = ["lljli","yoga","rucy","wenzhong","huangwji"]
Lineitem_filter = ["3204","133","133.1","1981"]
USSdefect_app = Bottle()
@USSdefect_app.route("/")
def index():
return template("ussdefect/index.htm",user=request.user)
@USSdefect_app.route("/defect/")
@login_required
def defect():
cx = sqlite.connect('branding.db')
cu = cx.cursor()
cu.execute('select * from ussdefects')
rs = cu.fetchall()
d_total = len(rs)
d_open = 0
d_verify = 0
d_close = 0
d_returned = 0
d_cancel = 0
d_other = 0
d_reject = 0
d_working = 0
for r in rs:
if r[8] == "open":
d_open += 1
elif r[8] == "verify":
d_verify += 1
elif r[8] == "close":
d_close += 1
elif r[8] == "returned":
d_returned += 1
elif r[8] == "cancel":
d_cancel += 1
elif r[8] == "reject":
d_reject += 1
elif r[8] == "working":
d_working += 1
else:
d_other += 1
return template("ussdefect/defect.htm",user=request.user, defects=rs, d_total=d_total, d_open=d_open, d_verify=d_verify, d_close=d_close,
d_returned=d_returned, d_cancel=d_cancel, d_reject=d_reject, d_working=d_working, d_other=d_other)
@USSdefect_app.route("/defect/new/")
@login_required
def defect_new():
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
return template("ussdefect/defect_new.htm", user=request.user)
@USSdefect_app.route("/defect/new/",method="POST")
@login_required
def defect_new_post():
component = request.forms.get('component',"")
number = request.forms.get('number',"")
title = request.forms.get('title',"")
Lineitem = request.forms.get('Lineitem',"")
open_date = request.forms.get('open_date',"")
close_date = request.forms.get('close_date',"")
poster = request.forms.get('poster',"")
status = request.forms.get('status',"")
comment = request.forms.get('comment',"")
component = component.replace("'","''")
number = number.replace("'","''")
title = title.replace("'","''")
Lineitem = Lineitem.replace("'","''")
open_date = open_date.replace("'","''")
close_date = close_date.replace("'","''")
poster = poster.replace("'","''")
status = status.replace("'","''")
comment = comment.replace("'","''")
if component == "":
return template("myerror.htm", user=request.user, msg="Error,component is null!")
if number == "":
return template("myerror.htm", user=request.user, msg="Error, number is null!")
if title == "":
return template("myerror.htm", user=request.user, msg="Error,title is null!")
if Lineitem == "":
return template("myerror.htm", user=request.user, msg="Error,Lineitem is null!")
if open_date == "":
return template("myerror.htm", user=request.user, msg="Error,open_date is null!")
if poster == "":
return template("myerror.htm", user=request.user, msg="Error,poster is null!")
if status == "":
return template("myerror.htm", user=request.user, msg="Error,status is null!")
m = date_p.match(open_date)
if not m:
return template("myerror.htm", user=request.user, msg="Error, open_date is invalid -- " + open_date)
m = date_p.match(close_date)
if close_date and not m:
return template("myerror.htm", user=request.user, msg="Error, close_date is invalid -- " + close_date)
try:
f2 = float(Lineitem)
except ValueError,e:
return template("myerror.htm", user=request.user, msg="Error, Lineitem format is invalid -- "+ Lineitem)
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "insert into ussdefects values(NULL,'%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (component, number, title, Lineitem, open_date, close_date, poster, status, comment)
cu.execute(command)
cx.commit()
cu.close()
redirect("/USSdefect/defect/")
@USSdefect_app.route("/defect/modify/<did>/")
@login_required
def defect_modify(did):
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "select * from ussdefects where id=%s" %did
cu.execute(command)
rs = cu.fetchone()
if rs == None:
return template("myerror.htm", user=request.user, msg="Error,invalid defect id -- "+did)
cu.close()
did = rs[0]
component = rs[1]
number = rs[2]
title = rs[3]
Lineitem = rs[4]
open_date = rs[5]
close_date = rs[6]
poster = rs[7]
status = rs[8]
comment = rs[9]
return template("ussdefect/defect_modify.htm", user=request.user, did=did, component=component, number=number, title=title, Lineitem=Lineitem,
open_date=open_date, close_date=close_date, poster=poster,status=status,comment=comment)
@USSdefect_app.route("/defect/modify/<did>/",method="POST")
@login_required
def defect_modify_post(did):
component = request.forms.get('component',"")
number = request.forms.get('number',"")
title = request.forms.get('title',"")
Lineitem = request.forms.get('Lineitem',"")
open_date = request.forms.get('open_date',"")
close_date = request.forms.get('close_date',"")
poster = request.forms.get('poster',"")
status = request.forms.get('status',"")
comment = request.forms.get('comment',"")
component = component.replace("'","''")
number = number.replace("'","''")
title = title.replace("'","''")
Lineitem = Lineitem.replace("'","''")
open_date = open_date.replace("'","''")
close_date = close_date.replace("'","''")
poster = poster.replace("'","''")
status = status.replace("'","''")
comment = comment.replace("'","''")
if component == "":
return template("myerror.htm", user=request.user, msg="Error,component is null!")
if number == "":
return template("myerror.htm", user=request.user, msg="Error, number is null!")
if title == "":
return template("myerror.htm", user=request.user, msg="Error,title is null!")
if Lineitem == "":
return template("myerror.htm", user=request.user, msg="Error,Lineitem is null!")
if open_date == "":
return template("myerror.htm", user=request.user, msg="Error,open_date is null!")
if poster == "":
return template("myerror.htm", user=request.user, msg="Error,poster is null!")
if status == "":
return template("myerror.htm", user=request.user, msg="Error,status is null!")
m = date_p.match(open_date)
if not m:
return template("myerror.htm", user=request.user, msg="Error, open_date is invalid -- " + open_date)
m = date_p.match(close_date)
if close_date and not m:
return template("myerror.htm", user=request.user, msg="Error, close_date is invalid -- " + close_date)
try:
f2 = float(Lineitem)
except ValueError,e:
return template("myerror.htm", user=request.user, msg="Error, Lineitem format is invalid -- "+ Lineitem)
if status in ["close","cancel"] and close_date=="":
return template("myerror.htm", user=request.user, msg="Error, you must input close_date for a close or cancel defect")
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "update ussdefects set component='%s',number='%s',title='%s',Lineitem='%s',open_date='%s',close_date='%s',poster='%s',status='%s',comment='%s' where id='%s';" % (component, number, title, Lineitem, open_date, close_date, poster, status, comment, did)
cu.execute(command)
cx.commit()
cu.close()
redirect("/USSdefect/defect/")
@USSdefect_app.route("/defect/delete/<did:int>/") #delete a user
@login_required
def defect_delete(did):
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
#if MessageBox(None, 'Do you confirm to delete this user?', 'Delete the user', 1)==1:
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "delete from ussdefects where id= '%s' " % did
cu.execute(command)
cx.commit()
redirect("/USSdefect/defect/")
def create_month_range(start_year, start_month, end_year, end_month):
if start_year*12+start_month > end_year*12+end_month:
raise Exception("invalid parameter")
year = start_year
month = start_month
while year*12+month <= end_year*12+end_month:
#yield datetime.date(year,month,1)
yield (year,month)
if month < 12:
month += 1
else:
month = 1
year += 1
@USSdefect_app.route("/defect/draw/") #delete a user
@login_required
def defect_draw():
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
result = []
# create pic1,pic2,pic3
result.append( create_defect_csv123() )
result.append( create_pic(make_defect_py, defect_fun_fn1, defect_csv_fn1, defect_eps_fn1, defect_png_fn1) )
result.append( create_pic(make_defect_py, defect_fun_fn2, defect_csv_fn2, defect_eps_fn2, defect_png_fn2) )
result.append( create_pic(make_defect_py, defect_fun_fn3, defect_csv_fn3, defect_eps_fn3, defect_png_fn3) )
return "<pre>\n"+"\n".join(result) +"</pre>\n"
def create_pic(targetpy, targetfun, targetcsv, targeteps, targetpng):
result = []
if sys.platform == "win32":
command1 = targetpy + " " + targetfun + " " + targetcsv + " > " +targeteps
command2 = "gswin32c -dBATCH -dNOPAUSE -dEPSCrop -sDEVICE=png16m -r300 -sOutputFile="+targetpng+" "+targeteps
else:
command1 = "python " + targetpy + " " + targetfun + " " + targetcsv + " > " +targeteps
command2 = "gs -dBATCH -dNOPAUSE -dEPSCrop -sDEVICE=png16m -r300 -sOutputFile="+targetpng+" "+targeteps
result.append(command1)
#time.sleep(1)
#t_f = os.popen(command1)
p = subprocess.Popen(command1,shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
#p.stdout, p.stderr
result.append( p.stdout.read() )
result.append( p.stderr.read() )
t_f = os.popen(command2)
result.append( t_f.read() )
return "\n".join(result)
def create_defect_csv123():
''' create csv for
(1) increase number of open_defect and close_defect pic per month
(2) total number of open_defect and close_defect pic per month
(3) defect number of each member
'''
result = []
result.append("<b>create_defect_csv1()</b>")
# we only use the data between (START_YEAR,START_MONTH) and (CURRENT_YEAR, CURRENT_MONTH)
START_YEAR = 2011
START_MONTH = 9
CURRENT_YEAR = datetime.date.today().year
CURRENT_MONTH = datetime.date.today().month
cx = sqlite.connect('branding.db')
cu = cx.cursor()
#cu.execute('select * from ussdefects')
cu.execute('select max(open_date),min(open_date) from ussdefects')
rs = cu.fetchall()
max_date = rs[0][0]
min_date = rs[0][1]
m = date_p.match(min_date)
if m:
min_date_year = int(m.group("year"))
min_date_month = int(m.group("month"))
else:
return template("myerror.htm", user=request.user, msg="Error, min(open_date) is invalid -- " + min_date)
from_date_year = min(min_date_year, START_YEAR)
from_date_month = min(min_date_month, START_MONTH)
# get the month range for x_axis
x_months = {}
month_generator = create_month_range(from_date_year, from_date_month, CURRENT_YEAR, CURRENT_MONTH)
for mm in month_generator:
x_months[mm] = [0,0] # first is number of open_defect, second is number of close_defect
cu.execute('select * from ussdefects')
rs = cu.fetchall()
for r in rs:
# calculate number of open defect for each month
if r[4] not in Lineitem_filter:
continue
open_date = r[5]
m = date_p.match(open_date)
if m:
year = int( m.group("year") )
month = int( m.group("month") )
open_number = x_months[(year,month)]
open_number[0] += 1
x_months[(year,month)] = open_number
else:
return template("myerror.htm", user=request.user, msg="Error, open_date is invalid, did-- " + str(r[0]))
# calculate number of close defect for each month
close_date = r[6]
m = date_p.match(close_date)
if m:
year = int( m.group("year") )
month = int( m.group("month") )
close_number = x_months[(year,month)]
close_number[1] += 1
x_months[(year,month)] = close_number
else:
if close_date != "":
return template("myerror.htm", user=request.user, msg="Error, close_date is invalid, did-- " + str(r[0]))
#pprint(x_months)
result.append(" read defects data successfully!")
###############################
# output data to csv file1 #
###############################
x_months2 = []
for key,value in x_months.items():
x_months2.append(["%d-%02d" % key,str(value[0]),str(value[1])])
x_months2.sort()
f1 = open(defect_csv_fn1,"w+")
for x in x_months2:
f1.write(",".join(x)+"\n")
f1.close()
result.append(" generate "+defect_csv_fn1+" successfully!")
###############################
# output data to csv file2 #
###############################
# x_months3 = [ ["%d-%02d" % x,0,0] for x in x_months.keys() ]
# for m2 in x_months2:
# for m3 in x_months3:
# if m2[0] >= m3[0]:
# m3[1] += int(m2[1])
# m3[2] += int(m2[2])
x_months3 = [ [x[0],int(x[1]),int(x[2])] for x in x_months2]
lens3 = len(x_months3)
if lens3 == 0:
return template("myerror.htm", user=request.user, msg="Error, x_months3 is emptry ")
for m3 in range(1,lens3):
x_months3[m3][1] += x_months3[m3-1][1]
x_months3[m3][2] += x_months3[m3-1][2]
f1 = open(defect_csv_fn2,"w+")
for x in x_months3:
f1.write(x[0]+","+ str(x[1]) +"," +str(x[2]) + "\n")
f1.close()
result.append(" generate "+defect_csv_fn2+" successfully!")
###############################
# output data to csv file3 #
###############################
member_defects = {}
for r in rs:
# calculate number of open defect for each month
if r[4] not in Lineitem_filter:
continue
open_number = member_defects.get(r[7],0)
open_number += 1
member_defects[r[7]] = open_number
f1 = open(defect_csv_fn3,"w+")
for key,value in member_defects.items():
f1.write(key+","+ str(value) + "\n")
f1.close()
result.append(" generate "+defect_csv_fn3+" successfully!")
return "<br>".join(result)
################################################################################
# #
# projects part #
# #
# #
################################################################################
@USSdefect_app.route("/projects/")
@login_required
def projects():
cx = sqlite.connect('branding.db')
cu = cx.cursor()
cu.execute('select * from ussprojects')
rs = cu.fetchall()
return template("ussdefect/projects.htm",user=request.user, projects=rs,)
@USSdefect_app.route("/projects/new/")
@login_required
def projects_new():
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
return template("ussdefect/projects_new.htm", user=request.user)
@USSdefect_app.route("/projects/new/",method="POST")
@login_required
def projects_new_post():
name = request.forms.get('name',"")
total_num = request.forms.get('total_num',"")
current_attempt = request.forms.get('current_attempt',"0")
current_succ = request.forms.get('current_succ',"0")
draw_flag = request.forms.get('draw_flag',"")
start_date = request.forms.get('start_date',"")
end_date = request.forms.get('end_date',"")
comment = request.forms.get('comment',"")
name = name.replace("'","''")
total_num = total_num.replace("'","''")
current_attempt = current_attempt.replace("'","''")
current_succ = current_succ.replace("'","''")
draw_flag = draw_flag.replace("'","''")
start_date = start_date.replace("'","''")
end_date = end_date.replace("'","''")
comment = comment.replace("'","''")
if name == "":
return template("myerror.htm", user=request.user, msg="Error,name is null!")
if total_num == "":
return template("myerror.htm", user=request.user, msg="Error, total_num is null!")
if current_attempt == "":
return template("myerror.htm", user=request.user, msg="Error,current_attempt is null!")
if current_succ == "":
return template("myerror.htm", user=request.user, msg="Error,current_succ is null!")
if draw_flag == "":
return template("myerror.htm", user=request.user, msg="Error,draw_flag is null!")
m = date_p.match(start_date)
if not m:
return template("myerror.htm", user=request.user, msg="Error, start_date is invalid -- " + open_date)
m = date_p.match(end_date)
if end_date and not m:
return template("myerror.htm", user=request.user, msg="Error, end_date is invalid -- " + end_date)
if end_date < start_date:
return template("myerror.htm", user=request.user, msg="Error, end_date < start_date -- " + end_date+" , "+start_date)
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "insert into ussprojects values(NULL,'%s','%s','%s','%s','%s','%s','%s','%s');" % (name, total_num, current_attempt, current_succ, draw_flag, start_date, end_date, comment)
cu.execute(command)
cx.commit()
cu.close()
redirect("/USSdefect/projects/")
@USSdefect_app.route("/projects/modify/<did>/")
@login_required
def projects_modify(did):
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "select * from ussprojects where id=%s" %did
cu.execute(command)
rs = cu.fetchone()
if rs == None:
return template("myerror.htm", user=request.user, msg="Error,invalid projects id -- "+did)
cu.close()
did = rs[0]
name = rs[1]
total_num = rs[2]
current_attempt = rs[3]
current_succ = rs[4]
draw_flag = rs[5]
start_date = rs[6]
end_date = rs[7]
comment = rs[8]
return template("ussdefect/projects_modify.htm", user=request.user, did=did, name=name, total_num=total_num, current_attempt=current_attempt, current_succ=current_succ,
draw_flag=draw_flag, start_date=start_date, end_date=end_date,comment=comment)
@USSdefect_app.route("/projects/modify/<did>/",method="POST")
@login_required
def projects_modify_post(did):
name = request.forms.get('name',"")
total_num = request.forms.get('total_num',"")
current_attempt = request.forms.get('current_attempt',"")
current_succ = request.forms.get('current_succ',"")
draw_flag = request.forms.get('draw_flag',"")
start_date = request.forms.get('start_date',"")
end_date = request.forms.get('end_date',"")
comment = request.forms.get('comment',"")
name = name.replace("'","''")
total_num = total_num.replace("'","''")
current_attempt = current_attempt.replace("'","''")
current_succ = current_succ.replace("'","''")
draw_flag = draw_flag.replace("'","''")
start_date = start_date.replace("'","''")
end_date = end_date.replace("'","''")
comment = comment.replace("'","''")
if name == "":
return template("myerror.htm", user=request.user, msg="Error,name is null!")
if total_num == "":
return template("myerror.htm", user=request.user, msg="Error, total_num is null!")
if current_attempt == "":
return template("myerror.htm", user=request.user, msg="Error,current_attempt is null!")
if current_succ == "":
return template("myerror.htm", user=request.user, msg="Error,current_succ is null!")
if draw_flag == "":
return template("myerror.htm", user=request.user, msg="Error,draw_flag is null!")
m = date_p.match(start_date)
if not m:
return template("myerror.htm", user=request.user, msg="Error, start_date is invalid -- " + open_date)
m = date_p.match(end_date)
if end_date and not m:
return template("myerror.htm", user=request.user, msg="Error, end_date is invalid -- " + end_date)
if end_date < start_date:
return template("myerror.htm", user=request.user, msg="Error, end_date < start_date -- " + end_date+" , "+start_date)
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "update ussprojects set name='%s',total_num='%s',current_attempt='%s',current_succ='%s',draw_flag='%s',start_date='%s',end_date='%s',comment='%s' where id='%s';" % (name, total_num, current_attempt, current_succ, draw_flag, start_date, end_date, comment, did)
cu.execute(command)
cx.commit()
cu.close()
redirect("/USSdefect/projects/")
@USSdefect_app.route("/projects/delete/<did:int>/") #delete a user
@login_required
def projects_delete(did):
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
#if MessageBox(None, 'Do you confirm to delete this user?', 'Delete the user', 1)==1:
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "delete from ussprojects where id= '%s' " % did
cu.execute(command)
command = "delete from ussproject where projectid= '%s' " % did
cu.execute(command)
cx.commit()
#print "redirect hahaha"
redirect("/USSdefect/projects/")
@USSdefect_app.route("/projects/project/list/<did>/")
@login_required
def projects_project_list(did):
cx = sqlite.connect('branding.db')
cu = cx.cursor()
cu.execute('select * from ussprojects where id='+did)
rs = cu.fetchall()
if len(rs) != 1:
return template("myerror.htm", user=request.user, msg="Error, id is not existed in ussprojects -- " + did)
r = rs[0]
name = r[1]
totalnum = r[2]
cu.execute('select * from ussproject where projectid='+did)
rs = cu.fetchall()
rs2 = [ [x,float(x[5])/totalnum*100, float(x[6])/totalnum*100] for x in rs]
return template("ussdefect/projects_project_list.htm",user=request.user, records=rs2, project=r)
@USSdefect_app.route("/projects/project/new/<did>/")
@login_required
def projects_project_new(did):
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
return template("ussdefect/projects_project_new.htm", user=request.user, did=did)
@USSdefect_app.route("/projects/project/new/<did>/",method="POST")
@login_required
def projects_project_new_post(did):
datepoint = request.forms.get('datepoint',"")
plan_attempt = request.forms.get('plan_attempt',"")
plan_succ = request.forms.get('plan_succ',"")
actual_attempt = request.forms.get('actual_attempt',"")
actual_succ = request.forms.get('actual_succ',"")
comment = request.forms.get('comment',"")
datepoint = datepoint.replace("'","''")
plan_attempt = plan_attempt.replace("'","''")
plan_succ = plan_succ.replace("'","''")
actual_attempt = actual_attempt.replace("'","''")
actual_succ = actual_succ.replace("'","''")
comment = comment.replace("'","''")
if datepoint == "":
return template("myerror.htm", user=request.user, msg="Error,datepoint is null!")
if plan_attempt == "":
plan_attempt = "0"
#return template("myerror.htm", user=request.user, msg="Error, plan_attempt is null!")
if plan_succ == "":
plan_succ = "0"
#return template("myerror.htm", user=request.user, msg="Error,plan_succ is null!")
if actual_attempt == "":
actual_attempt = "0"
#return template("myerror.htm", user=request.user, msg="Error,actual_attempt is null!")
if actual_succ == "":
actual_succ = "0"
#return template("myerror.htm", user=request.user, msg="Error,actual_succ is null!")
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "insert into ussproject values(NULL,'%s','%s','%s','%s','%s','%s','%s');" % (did, datepoint, plan_attempt, plan_succ, actual_attempt, actual_succ, comment)
cu.execute(command)
cx.commit()
cu.close()
redirect("/USSdefect/projects/project/list/"+did+"/")
@USSdefect_app.route("/projects/project/modify/<ddid>/")
@login_required
def projects_project_modify(ddid):
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "select * from ussproject where id=%s" %ddid
cu.execute(command)
rs = cu.fetchone()
if rs == None:
return template("myerror.htm", user=request.user, msg="Error,invalid project id -- "+ddid)
cu.close()
ddid = rs[0]
projectid = rs[1]
datepoint = rs[2]
plan_attempt = rs[3]
plan_succ = rs[4]
actual_attempt = rs[5]
actual_succ = rs[6]
comment = rs[7]
return template("ussdefect/projects_project_modify.htm", user=request.user, ddid=ddid, projectid=projectid, datepoint=datepoint, plan_attempt=plan_attempt, plan_succ=plan_succ,
actual_attempt=actual_attempt, actual_succ=actual_succ, comment=comment)
@USSdefect_app.route("/projects/project/modify/<ddid>/",method="POST")
@login_required
def projects_project_modify_post(ddid):
projectid = request.forms.get('projectid',"")
datepoint = request.forms.get('datepoint',"")
plan_attempt = request.forms.get('plan_attempt',"")
plan_succ = request.forms.get('plan_succ',"")
actual_attempt = request.forms.get('actual_attempt',"")
actual_succ = request.forms.get('actual_succ',"")
comment = request.forms.get('comment',"")
current_flag = request.forms.get('current_flag',"")
projectid = projectid.replace("'","''")
datepoint = datepoint.replace("'","''")
plan_attempt = plan_attempt.replace("'","''")
plan_succ = plan_succ.replace("'","''")
actual_attempt = actual_attempt.replace("'","''")
actual_succ = actual_succ.replace("'","''")
comment = comment.replace("'","''")
current_flag = current_flag.replace("'","''")
if projectid == "":
return template("myerror.htm", user=request.user, msg="Error,projectid is null!")
if datepoint == "":
return template("myerror.htm", user=request.user, msg="Error,datepoint is null!")
if plan_attempt == "":
plan_attempt = "0"
if not plan_attempt.isdigit():
return template("myerror.htm", user=request.user, msg="Error, plan_attempt is not number!")
if plan_succ == "":
plan_succ = "0"
if not plan_succ.isdigit():
return template("myerror.htm", user=request.user, msg="Error,plan_succ is not number!")
if actual_attempt == "":
actual_attempt = "0"
if not actual_attempt.isdigit():
return template("myerror.htm", user=request.user, msg="Error,actual_attempt is not number!")
if actual_succ == "":
actual_succ = "0"
if not actual_succ.isdigit():
return template("myerror.htm", user=request.user, msg="Error,actual_succ is not number!")
if int(actual_attempt) < int(actual_succ):
return template("myerror.htm", user=request.user, msg="Error,actual_attempt(" +actual_attempt +") < actual_succ(" + actual_succ +")")
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "select * from ussprojects where id=%s" % projectid
cu.execute(command)
rs = cu.fetchone()
if rs == None:
return template("myerror.htm", user=request.user, msg="Error,invalid projects id -- "+projectid)
cu.close()
total_num = rs[2]
if int(actual_attempt) > total_num:
return template("myerror.htm", user=request.user, msg="Error, actual_attempt(" +actual_attempt +") > total_num(" + str(total_num) +")")
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "update ussproject set datepoint='%s',plan_attempt='%s',plan_succ='%s',actual_attempt='%s',actual_succ='%s',comment='%s' where id='%s';" % (datepoint,
plan_attempt, plan_succ, actual_attempt, actual_succ, comment, ddid)
cu.execute(command)
if current_flag == "on":
command = "update ussprojects set current_attempt='%s',current_succ='%s' where id='%s';" % (actual_attempt, actual_succ, projectid)
cu.execute(command)
cx.commit()
cu.close()
redirect("/USSdefect/projects/project/list/"+projectid+"/")
@USSdefect_app.route("/projects/project/delete/<ddid>/<projectid>/") #delete a user
@login_required
def projects_project_delete(ddid,projectid):
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
#if MessageBox(None, 'Do you confirm to delete this user?', 'Delete the user', 1)==1:
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "delete from ussproject where id= '%s' " % ddid
cu.execute(command)
cx.commit()
#print "redirect hahaha"
redirect("/USSdefect/projects/project/list/"+projectid+"/")
@USSdefect_app.route("/projects/project/generate/<did>/")
@login_required
def projects_project_generate(did):
if request.user.username not in ussfvt_users:
return template("myerror.htm", user=request.user, msg="Error,Only uss fvt members can visit here!")
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "select * from ussprojects where id=%s" %did
cu.execute(command)
rs = cu.fetchone()
if rs == None:
return template("myerror.htm", user=request.user, msg="Error,invalid projects id -- "+did)
start_date = rs[6]
end_date = rs[7]
m = date_p.match(start_date)
if not m:
return template("myerror.htm", user=request.user, msg="Error, start_date is invalid -- " + start_date)
#print map(lambda x:int(x),m.groups())
#start_day = datetime.date(*map(lambda x:int(x),m.groups()))
start_day = datetime.date(int(m.group("year")),int(m.group("month")),int(m.group("day")))
m = date_p.match(end_date)
if not m:
return template("myerror.htm", user=request.user, msg="Error, end_date is invalid -- " + end_date)
end_day = datetime.date(int(m.group("year")),int(m.group("month")),int(m.group("day")))
print start_day, end_day
weeks = []
months = []
day1 = start_day
while day1 <= end_day:
weeks.append("%d-%02d-%02d" % (day1.year, day1.month, day1.day) )
day1 = day1 + datetime.timedelta(7)
day1 = start_day
while day1 <= end_day:
months.append(day1)
day1 = day1 + datetime.timedelta(30)
#print weeks, months
return template("ussdefect/projects_project_generate.htm", user=request.user, did=did, project=rs, weeks=weeks, months=months)
@USSdefect_app.route("/projects/project/generate/<did>/", method="post")
@login_required
def projects_project_generate_post(did):
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "select * from ussprojects where id=%s" %did
cu.execute(command)
rs = cu.fetchone()
if rs == None:
return template("myerror.htm", user=request.user, msg="Error,invalid projects id -- "+did)
name = rs[1]
total_num = rs[2]
current_attempt = rs[3]
current_succ = rs[4]
draw_flag = rs[5]
start_date = rs[6]
end_date = rs[7]
comment = rs[8]
m = date_p.match(start_date)
if not m:
return template("myerror.htm", user=request.user, msg="Error, start_date is invalid -- " + start_date)
start_day = datetime.date(int(m.group("year")),int(m.group("month")),int(m.group("day")))
m = date_p.match(end_date)
if not m:
return template("myerror.htm", user=request.user, msg="Error, end_date is invalid -- " + end_date)
end_day = datetime.date(int(m.group("year")),int(m.group("month")),int(m.group("day")))
#print start_day, end_day
gflag = request.forms.get('gflag',"weekly")
datepoints = []
day1 = start_day
if gflag == "weekly":
while day1 <= end_day:
datepoints.append("%d-%02d-%02d" % (day1.year, day1.month, day1.day) )
day1 = day1 + datetime.timedelta(7)
elif gflag == "monthly":
while day1 <= end_day:
datepoints.append("%d-%02d-%02d" % (day1.year, day1.month, day1.day) )
day1 = day1 + datetime.timedelta(30)
else:
return template("myerror.htm", user=request.user, msg="Error, gflag is invalid -- " + gflag)
###############################################################
# generate data with different algorithem #
# (1) gtype == "empty" #
# only generate datepoints #
# (2) gtype == "algorithm1" #
# generate datepoints, plan_attempts, plan_succs #
# (3) gtype == "algorithm2" #
# generate datepoints, plan_attempts, plan_succs, #
# actual_attempts, actual_succs #
###############################################################
lens = len(datepoints)
gtype = request.forms.get('gtype',"empty")
if gtype == "empty":
plan_attempts = [ 0 for x in xrange(lens)]
plan_succs = [ 0 for x in xrange(lens)]
actual_attempts = [ 0 for x in xrange(lens)]
actual_succs = [ 0 for x in xrange(lens)]
elif gtype == "algorithm1":
if lens < 3:
plan_attempts = random.sample(range(total_num),lens)
plan_succs = random.sample(range(total_num),lens)
else:
plan_attempts = []
plan_succs = []
for i in range(1,lens+1):
plan_attempts.append(total_num*i/lens)
plan_succs.append(total_num*i/lens - (lens-i))
actual_attempts = [ 0 for x in xrange(lens)]
actual_succs = [ 0 for x in xrange(lens)]
elif gtype == "algorithm2":
if lens < 3:
plan_attempts = random.sample(range(total_num),lens)
plan_succs = random.sample(range(total_num),lens)
actual_attempts = random.sample(range(total_num),lens)
actual_succs = random.sample(range(total_num),lens)
else:
plan_attempts = random.sample(range(total_num),lens-2)
plan_succs = random.sample(range(total_num),lens-2)
actual_attempts = random.sample(range(total_num),lens-2)
actual_succs = random.sample(range(total_num),lens-2)
plan_attempts.append(total_num)
plan_attempts.append(total_num)
plan_succs.append(total_num)
plan_succs.append(total_num)
actual_attempts.append(total_num)
actual_attempts.append(total_num)
actual_succs.append(total_num)
actual_succs.append(total_num)
plan_attempts.sort()
plan_succs.sort()
actual_attempts.sort()
actual_succs.sort()
# make sure attempt >= succ
for ii in range(lens):
if plan_attempts[ii] < plan_succs[ii]:
plan_attempts[ii], plan_succs[ii] = plan_succs[ii], plan_attempts[ii]
if actual_attempts[ii] < actual_succs[ii]:
actual_attempts[ii], actual_succs[ii] = actual_succs[ii], actual_attempts[ii]
#print "plan_attempts=", plan_attempts
command = "delete from ussproject where projectid=%s" % did
#print "command1=",command
cu.execute(command)
command = "update ussprojects set current_attempt='%s',current_succ='%s' where id='%s';" % ("0", "0", did)
cu.execute(command)
for dd in range(lens):
command = "insert into ussproject values(NULL,'%s','%s','%s','%s','%s','%s','%s');" % (did, datepoints[dd], plan_attempts[dd], plan_succs[dd], actual_attempts[dd], actual_succs[dd], "")
#print "command2=",command
cu.execute(command)
cx.commit()
redirect("/USSdefect/projects/project/list/"+did+"/")
def create_project_csv0():
result = []
cx = sqlite.connect('branding.db')
cu = cx.cursor()
command = "select * from ussprojects where draw_flag=1"
cu.execute(command)
rs = cu.fetchall()
data = []
for r in rs: