def testCsvTuplePrinterMustRecieveOneTupleAndPrintItToTheOutputFile(self):
     csv_tuple_printer = CsvTuplePrinter()
     output_file = Mock()
     csv_tuple_printer.output_file = output_file
     a_tuple = ("field1", "field2", 0)
     
     csv_tuple_printer.put(a_tuple)
     
     stringified_tuple = str(a_tuple[0]) + "," + str(a_tuple[1]) + "," + str(a_tuple[2]) + "\n"
     output_file.write.assert_called_with(stringified_tuple)
Exemple #2
0
def download(request, idcourse):
    if(len(request.user.teacher_set.all()) > 0): # if an authenticated user "accidentally" access this section, he doesn't get an exception
        course = Course.objects.get(pk=idcourse)
        db_data_extractor = DbDeliveriesExtractor()
        db_data_extractor.course = course
        data = db_data_extractor.get_data()
        csv_tuple_printer = CsvTuplePrinter()
        output_file_name = get_output_file_name(course.name)
        csv_tuple_printer.open(output_file_name)
        for line in data:
            csv_tuple_printer.put(line)
        csv_tuple_printer.close()
        file = open(output_file_name)
        response = HttpResponse(file, content_type=TYPECSV)
        response['Content-Disposition'] = 'attachment; filename=%s' % output_file_name
        return response
    else:
        return HTTP_401_UNAUTHORIZED_RESPONSE