-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
views.py
296 lines (254 loc) · 8.78 KB
/
views.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
from django.shortcuts import render, redirect
from django.http import HttpResponseBadRequest, HttpResponse
from _compact import JsonResponse
from django import forms
import django_excel as excel
from polls.models import Question, Choice
data = [[1, 2, 3], [4, 5, 6]]
class UploadFileForm(forms.Form):
file = forms.FileField()
# Create your views here.
def upload(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
filehandle = request.FILES["file"]
return excel.make_response(
filehandle.get_sheet(), "csv", file_name="download"
)
else:
form = UploadFileForm()
return render(
request,
"upload_form.html",
{
"form": form,
"title": "Excel file upload and download example",
"header": (
"Please choose any excel file "
+ "from your cloned repository:"
),
},
)
def download(request, file_type):
sheet = excel.pe.Sheet(data)
return excel.make_response(sheet, file_type)
def download_as_attachment(request, file_type, file_name):
return excel.make_response_from_array(data, file_type, file_name=file_name)
def export_data(request, atype):
if atype == "sheet":
return excel.make_response_from_a_table(
Question, "xls", file_name="sheet"
)
elif atype == "book":
return excel.make_response_from_tables(
[Question, Choice], "xls", file_name="book"
)
elif atype == "custom":
question = Question.objects.get(slug="ide")
query_sets = Choice.objects.filter(question=question)
column_names = ["choice_text", "id", "votes"]
return excel.make_response_from_query_sets(
query_sets, column_names, "xls", file_name="custom"
)
else:
return HttpResponseBadRequest(
"Bad request. please put one of these "
+ "in your url suffix: sheet, book or custom"
)
def import_data(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
def choice_func(row):
q = Question.objects.filter(slug=row[0])[0]
row[0] = q
return row
if form.is_valid():
request.FILES["file"].save_book_to_database(
models=[Question, Choice],
initializers=[None, choice_func],
mapdicts=[
["question_text", "pub_date", "slug"],
{"Question": "question", "Choice": "choice_text", "Votes": "votes"},
],
)
return redirect("handson_view")
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(
request,
"upload_form.html",
{
"form": form,
"title": "Import excel data into database example",
"header": "Please upload sample-data.xls:",
},
)
def import_sheet(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
request.FILES["file"].save_to_database(
name_columns_by_row=2,
model=Question,
mapdict=["question_text", "pub_date", "slug"],
)
return HttpResponse("OK")
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(request, "upload_form.html", {"form": form})
def exchange(request, file_type):
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
filehandle = request.FILES["file"]
return excel.make_response(filehandle.get_sheet(), file_type)
else:
return HttpResponseBadRequest()
def parse(request, data_struct_type):
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
filehandle = request.FILES["file"]
if data_struct_type == "array":
return JsonResponse({"result": filehandle.get_array()})
elif data_struct_type == "dict":
return JsonResponse(filehandle.get_dict())
elif data_struct_type == "records":
return JsonResponse({"result": filehandle.get_records()})
elif data_struct_type == "book":
return JsonResponse(filehandle.get_book().to_dict())
elif data_struct_type == "book_dict":
return JsonResponse(filehandle.get_book_dict())
else:
return HttpResponseBadRequest()
else:
return HttpResponseBadRequest()
def handson_table(request):
return excel.make_response_from_tables(
[Question, Choice], "handsontable.html"
)
def embed_handson_table(request):
"""
Renders two table in a handsontable
"""
content = excel.pe.save_book_as(
models=[Question, Choice],
dest_file_type="handsontable.html",
dest_embed=True,
)
content.seek(0)
return render(
request,
"custom-handson-table.html",
{"handsontable_content": content.read()},
)
def embed_handson_table_from_a_single_table(request):
"""
Renders one table in a handsontable
"""
content = excel.pe.save_as(
model=Question, dest_file_type="handsontable.html", dest_embed=True
)
content.seek(0)
return render(
request,
"custom-handson-table.html",
{"handsontable_content": content.read()},
)
def survey_result(request):
question = Question.objects.get(slug="ide")
query_sets = Choice.objects.filter(question=question)
column_names = ["choice_text", "votes"]
# Obtain a pyexcel sheet from the query sets
sheet = excel.pe.get_sheet(
query_sets=query_sets, column_names=column_names
)
sheet.name_columns_by_row(0)
sheet.column.format("votes", int)
# Transform the sheet into an svg chart
svg = excel.pe.save_as(
array=[sheet.column["choice_text"], sheet.column["votes"]],
dest_file_type="svg",
dest_chart_type="pie",
dest_title=question.question_text,
dest_width=600,
dest_height=400,
)
return render(request, "survey_result.html", dict(svg=svg.read()))
def import_sheet_using_isave_to_database(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
request.FILES["file"].isave_to_database(
model=Question, mapdict=["question_text", "pub_date", "slug"]
)
return HttpResponse("OK")
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(request, "upload_form.html", {"form": form})
def import_data_using_isave_book_as(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
def choice_func(row):
q = Question.objects.filter(slug=row[0])[0]
row[0] = q
return row
if form.is_valid():
request.FILES["file"].isave_book_to_database(
models=[Question, Choice],
initializers=[None, choice_func],
mapdicts=[
["question_text", "pub_date", "slug"],
{"Question": "question", "Choice": "choice_text", "Votes": "votes"},
],
)
return redirect("handson_view")
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(
request,
"upload_form.html",
{
"form": form,
"title": "Import excel data into database example",
"header": "Please upload sample-data.xls:",
},
)
def import_without_bulk_save(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
def choice_func(row):
q = Question.objects.filter(slug=row[0])[0]
row[0] = q
return row
if form.is_valid():
request.FILES["file"].save_book_to_database(
models=[Question, Choice],
initializers=[None, choice_func],
mapdicts=[
["question_text", "pub_date", "slug"],
["question", "choice_text", "votes"],
],
bulk_save=False,
)
return redirect("handson_view")
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(
request,
"upload_form.html",
{
"form": form,
"title": "Import excel data into database example",
"header": "Please upload sample-data.xls:",
},
)