Пример #1
0
    def post(self):
        current_reader = self.require_current_reader()
        meter_key = self.post_str("meter_key")
        meter = Meter.get_meter(meter_key)
        if current_reader.key() != meter.reader.key():
            self.return_forbidden()

        try:
            read_date = self.post_datetime("read", meter.get_tzinfo())
            value = self.post_float("value")
            read = Read(meter=meter, read_date=read_date, value=value)
            read.put()
            fields = self.page_fields(meter, current_reader)
            fields['read'] = read
            self.return_ok(fields)
        except HTTPBadRequest as e:
            self.return_bad_request(self.page_fields(meter, current_reader, e))
Пример #2
0
    def post(self):
        try:
            current_reader = self.require_current_reader()
            meter_key = self.post_str("meter_key")
            meter = Meter.get_meter(meter_key)
            if current_reader.key() != meter.reader.key():
                self.return_forbidden()

            file_item = self.post_file("spreadsheet")
            if file_item.filename.endswith(".csv"):
                rdr = csv.reader(file_item.file)
                for row in rdr:
                    if len(row) < 2:
                        raise HTTPBadRequest("""Expecting 2 fields per row, the
                                date in the format yyyy-MM-dd HH:mm followed by
                                the reading.""")
                    try:
                        read_date = datetime.datetime.strptime(
                            row[0].strip(), '%Y-%m-%d %H:%M')
                    except ValueError as e:
                        raise HTTPBadRequest(
                            "Problem at line number " + str(rdr.line_num) +
                            " of the file. The first field (the read date "
                            "field) isn't formatted correctly, it should be "
                            "of the form 2010-02-23T21:46. " + str(e))
                    value = float(row[1].strip())
                    read = Read(meter=meter, read_date=read_date, value=value)
                    read.put()
                fields = self.page_fields(meter, current_reader)
                fields['message'] = 'File imported successfully.'
                self.return_ok(fields)
            else:
                raise HTTPBadRequest("The file name must end with '.csv.'")
        except HTTPBadRequest as e:
            e.values = self.page_fields(meter, current_reader)
            raise e