Пример #1
0
def csv_export(request):
	logger.debug("csv export requested.")
	tmpfile = NamedTemporaryFile(suffix='.csv')
	csvwriter = csv.writer(tmpfile)
	# write csv headers
	csvwriter.writerow(['sensor_id', 'sensor_name', 'timestamp', 'value', 'value_name', 'value_units'])
	weather_data = WeatherData.objects.all()
	for d in weather_data:
		csvwriter.writerow([d.sensor.id, d.sensor.name, d.timestamp, d.value, d.type.name, d.type.units])
	wrapper = FileWrapper(tmpfile)
	logger.debug("Serving CSV file name=%s length=%s", tmpfile.name, tmpfile.tell())
	response = HttpResponse(wrapper, content_type='text/plain')
	response['Content-Disposition'] = 'attachment; filename=weather.csv'
	response['Content-Length'] = tmpfile.tell()
	tmpfile.seek(0)
	return response
Пример #2
0
    def process_images(self, sent_files, key_name):
        # name_of_container is the name='...' attribute of the HTML form/input tag
        files = [
            sent_files.get('%s[%d]' % (key_name, i))
            for i in range(0, len(sent_files))
        ]

        # iterate every file
        for file in files:

            # check image format using endswith with a tuple of possible extensions
            if not file.name.lower().endswith(('.jpg', '.jpeg', '.png')):
                raise TypeError('Format of the image is not valid')

            # store the image in a NamedTemporaryFile
            img_temp = NamedTemporaryFile()
            img_temp.write(file.read())
            img_temp.flush()

            # check image dimension, files cannot be larger than 10mb
            if img_temp.tell() > 10000000:
                raise TypeError('Image file is too big')

            # recognize image and add recognition result to a list
            self.recognition_output[
                file.name] = self.image_recognizer.recognize_image(img_temp)

        # Create JSON FILE
        json_file_str = "{"
        for key, value in self.recognition_output.items():
            json_file_str += '"' + key + '":"' + value + '",'

        # remove last comma
        json_file_str = json_file_str[:-1]
        json_file_str += "}"

        # return formatted JSON file
        return json_file_str