def test_notifications_hook(self): sensor_value = SensorValue( sensor_id=1, value=56, timestamp=datetime.now().replace(tzinfo=utc)) sensor_value.save() threshold = Threshold(sensor_id=1) threshold.save() response = self.client.get('/api/notifications/') self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertEqual(data['total'], 0) self.assertEqual(len(data['notifications']), 0) notification = Notification(threshold=threshold, sensor_value=sensor_value, target=50) notification.save() response = self.client.get('/api/notifications/') self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertEqual(data['total'], 1) self.assertEqual(len(data['notifications']), 1) self.assertEqual(data['notifications'][0]['threshold']['id'], threshold.id)
def test_notifications_hook(self): sensor_value = SensorValue(sensor_id=1, value=56, timestamp=datetime.now().replace(tzinfo=utc)) sensor_value.save() threshold = Threshold(sensor_id=1) threshold.save() response = self.client.get('/api/notifications/') self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertEqual(data['total'], 0) self.assertEqual(len(data['notifications']), 0) notification = Notification(threshold=threshold, sensor_value=sensor_value, target=50) notification.save() response = self.client.get('/api/notifications/') self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertEqual(data['total'], 1) self.assertEqual(len(data['notifications']), 1) self.assertEqual( data['notifications'][0]['threshold']['id'], threshold.id)
def handle_threshold(request): if not request.user.is_superuser: raise PermissionDenied try: data = json.loads(request.body) except ValueError: return create_json_response({"status": "failed"}, request) if 'id' in data: threshold = Threshold.objects.get(id=data['id']) if threshold is not None: if 'delete' in data: threshold.delete() else: if 'name' in data: threshold.name = data['name'] if 'sensor_id' in data: threshold.sensor_id = int(data['sensor_id']) if 'min_value' in data: if data['min_value'] == '': threshold.min_value = None else: try: threshold.min_value = float(data['min_value']) except ValueError: pass if 'max_value' in data and data['max_value'] != '': if data['max_value'] == '': threshold.max_value = None else: try: threshold.max_value = float(data['max_value']) except ValueError: pass if 'category' in data: threshold.category = int(data['category']) if 'show_manager' in data: threshold.show_manager = True if data[ 'show_manager'] == '1' else False threshold.save() return create_json_response({"status": "success"}, request) else: if all(x in data for x in [ 'name', 'sensor_id', 'min_value', 'max_value', 'category', 'show_manager' ]): threshold = Threshold(name=data['name'], sensor_id=int(data['sensor_id']), category=int(data['category'])) try: threshold.min_value = float(data['min_value']) except ValueError: pass try: threshold.max_value = float(data['max_value']) except ValueError: pass threshold.show_manager = True if data[ 'show_manager'] == '1' else False threshold.save() return create_json_response({"status": "success"}, request) return create_json_response({"status": "failed"}, request)
def handle_threshold(request): if not request.user.is_superuser: raise PermissionDenied try: data = json.loads(request.body) except ValueError: return create_json_response({"status": "failed"}, request) if 'id' in data: threshold = Threshold.objects.get(id=data['id']) if threshold is not None: if 'delete' in data: threshold.delete() else: if 'name' in data: threshold.name = data['name'] if 'sensor_id' in data: threshold.sensor_id = int(data['sensor_id']) if 'min_value' in data: if data['min_value'] == '': threshold.min_value = None else: try: threshold.min_value = float(data['min_value']) except ValueError: pass if 'max_value' in data and data['max_value'] != '': if data['max_value'] == '': threshold.max_value = None else: try: threshold.max_value = float(data['max_value']) except ValueError: pass if 'category' in data: threshold.category = int(data['category']) if 'show_manager' in data: threshold.show_manager = True if data[ 'show_manager'] == '1' else False threshold.save() return create_json_response({"status": "success"}, request) else: if all(x in data for x in ['name', 'sensor_id', 'min_value', 'max_value', 'category', 'show_manager']): threshold = Threshold(name=data['name'], sensor_id=int( data['sensor_id']), category=int(data['category'])) try: threshold.min_value = float(data['min_value']) except ValueError: pass try: threshold.max_value = float(data['max_value']) except ValueError: pass threshold.show_manager = True if data[ 'show_manager'] == '1' else False threshold.save() return create_json_response({"status": "success"}, request) return create_json_response({"status": "failed"}, request)