Ejemplo n.º 1
0
 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()
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
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