def generateTestFile(id, option='csv', fname=None): """ Generates a test file & uploads it to Document OPTIONS: (csv|not-csv|bad_data) :return file object """ file_name = None output = io.BytesIO() wr = csv.writer(output, delimiter=",") if option == 'csv': file_name = "test1.csv" wr.writerow(['a', 'b', 'c', 'd', 'e']) for _ in range(1, 5): wr.writerow([1, 2, 3, 4, 5]) elif option == 'not-csv': file_name = 'not-csv' wr.writerow('hello world\n1\n2.5\nlol\n'); elif option == 'bad_data': file_name = 'bad_data.csv' for _ in range(1, 4): wr.writerow([1, 2, 3, 4, 5]) wr.writerow([6.0, 6.0, 6.0, 6.0, 6.0]) if fname: # overrides any default file name set file_name = fname user_file = SimpleUploadedFile(file_name, output.getvalue()) Document.upload_file(id, file_name, user_file, FilesTest.username)
def generateBadDoc(id): """ Generates a 'bad' document (causes data-processing to crash) """ new_files = [] file_name = "test1" new_files.append(file_name) with open(os.path.join(settings.MEDIA_ROOT, file_name), 'wb') as csvfile: wr = csv.writer(csvfile, delimiter=",") wr.writerow(['c{}'.format(k) for k in range(4)]) # uneven row/column lengths! wr.writerow([k for k in range(4)]) wr.writerow([k for k in range(3)]) wr.writerow([k for k in range(2)]) Document.upload_file(id, file_name, file_name, PlotTest.username) # not sure about path correctness return new_files
def list_panes(request): """ Entry handler for user's home page! """ # redirect to login page if not signed in if not request.user.is_authenticated(): return redirect("top") if request.method == "POST": form = DocumentForm(request.POST, request.FILES) if form.is_valid(): owner = Account.objects.get(id=request.user.id).username Document.upload_file(request.user.id, request.FILES["csv_file"].name, request.FILES["csv_file"], owner) # Redirect to the document list after POST return HttpResponseRedirect(reverse("app.views.list_panes")) elif request.method == "GET": return render_to_response( "home.html", get_panes_data(request.user.id), context_instance=RequestContext(request) )
def generateDocs(id, n, r, c, name=None): """ Helper to create test docs according to the parameters below :param id: account id :param n: number of files to generate :param r: number of rows per file :param c: number of cols per file :return: [files names of new files generated] """ new_files = [] for i in range(1, n+1): if not name: file_name = "test{}".format(i) else: file_name = name + str(i) new_files.append(file_name) with open(os.path.join(settings.MEDIA_ROOT, file_name), 'wb') as csvfile: wr = csv.writer(csvfile, delimiter=",") wr.writerow(['c{}'.format(k) for k in range(c)]) for j in range(r): wr.writerow([i*k for k in range(1, c+1)]) Document.upload_file(id, file_name, file_name, PlotTest.username) # not sure about path correctness return new_files