def testCsvTuplePrinterMustCloseTheOutputFileWhenCommanded(self):
     csv_tuple_printer = CsvTuplePrinter()
     output_file = Mock()
     csv_tuple_printer.output_file = output_file
     
     csv_tuple_printer.close()
     
     output_file.close.assert_called()
 def testCsvTuplePrinterMustCreateTheOutputFileWhenCommanded(self):
     file_name = "/tmp/tmp-output-file.csv"
     shutil.rmtree(file_name, ignore_errors=True) # I make shure the file doesn't exists prevously
     csv_tuple_printer = CsvTuplePrinter()
     
     csv_tuple_printer.open(file_name)
     csv_tuple_printer.close()
     
     self.assertTrue(os.path.exists(file_name))
     shutil.rmtree(file_name, ignore_errors=True)
Beispiel #3
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