class XLS_Load TemplateView template_name xls_load html def get_contex

 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
class XLS_Load(TemplateView):
template_name = 'xls_load.html'
def get_context_data(self, **kwargs):
context = super(XLS_Load, self).get_context_data(**kwargs)
directors = []
stageurs_values = []
seniors_values = []
for i in Results.objects.all():
for j in User.objects.all():
if j.id == i.director_id:
directors.append(j)
stageurs_values.append(i.avg_stageur)
seniors_values.append(i.avg_senior)
data = map(None, directors, stageurs_values, seniors_values)
directors = [x[0] for x in data]
stageurs_values = [x[1] for x in data]
seniors_values = [x[2] for x in data]
context['stat'] = []
for i in xrange(len(directors)):
count = directors.count(directors[0])
print count
context['stat'].append({'director_name': directors[0],
'stageurs_values': sum(stageurs_values[:count])/count,
'seniors_values': sum(seniors_values[:count])/count,
})
directors = directors[count:]
stageurs_values = stageurs_values[count:]
seniors_values = seniors_values[count:]
if not directors:
break
print context['stat']
return context
def post(self, request):
file = dict(request.FILES)
new_file = file['file'][0].read()
local = open('new.xlsx', 'wb')
local.write(new_file)
local.close()
read_book = xlrd.open_workbook('new.xlsx', on_demand=True)
read_sheet = read_book.sheet_by_index(sheetx=0)
for rownum in range(read_sheet.nrows):
row = read_sheet.row_values(rownum)
new_record = Results.objects.create(director_id=row[0],
question_id=row[1],
avg=row[2],
avg_stageur=row[3],
avg_senior=row[4])
new_record.save()
return super(XLS_Load, self).get(self, request)