Skip to content
Permalink
 
 
Cannot retrieve contributors at this time
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:",
},
)