diff --git a/README.md b/README.md index 6e7de2ce..215b3489 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,9 @@ # Team Project repo -Heroku Production (master) URI: [https://nyu-mercury-prod.herokuapp.com/](https://nyu-mercury-prod.herokuapp.com/) - -Heroku Staging (develop) URI: [https://nyu-mercury.herokuapp.com](https://nyu-mercury.herokuapp.com) - -Heroku Dashboard: [https://dashboard.heroku.com/pipelines/35c0558f-127e-482b-8cdf-3f4d24464872](https://dashboard.heroku.com/pipelines/35c0558f-127e-482b-8cdf-3f4d24464872) +Heroku (master): [https://spring2020-cs-gy-9223-prod.herokuapp.com/](https://spring2020-cs-gy-9223-prod.herokuapp.com/) +*It's not automaticaly deployed for the time being. Please reach out to Dan Gopstein (dg2514@nyu.edu) or Yonguk Jeong (yj1679@nyu.edu) for access to the heroku instance. # First time repo setup 1. From the root of the repo, run `scripts/setup.sh`. Activate your virtualenv first. @@ -47,9 +44,3 @@ Run `python manage.py runserver` from the root of this Git repo # HOWTO Run tests locally Run `python manage.py test` - -# HOWTO Install SymmetricDS -``` -cd symmetricds -make install -``` diff --git a/mercury/tests/test_measurement.py b/mercury/tests/test_measurement.py index 2d6083a4..e881f165 100644 --- a/mercury/tests/test_measurement.py +++ b/mercury/tests/test_measurement.py @@ -37,9 +37,21 @@ def fake_valid(res, raise_exception=True): class TestMeasurement(TestCase): def setUp(self) -> None: self.post_url = "mercury:measurement" + self.post_url2 = "mercury:measurementWO" self.uuid = "d81cac8d-26e1-4983-a942-1922e54a943d" self.uuid2 = "d81cac8d-26e1-4983-a942-1922e54a943a" + def post_radio_data_wo_event(self): + response = self.client.post( + reverse(self.post_url2), + data={ + "sensor_id": 1, + "values": {"power": "2", "speed": 1}, + "date": datetime.datetime(2020, 2, 2, 20, 21, 22), + }, + ) + return response + def post_radio_data(self): # POST sensor data to the measurement url response = self.client.post( diff --git a/mercury/urls.py b/mercury/urls.py index 20a49b60..2f7a09ee 100644 --- a/mercury/urls.py +++ b/mercury/urls.py @@ -76,4 +76,9 @@ measurement.MeasurementView.as_view(), name="measurement", ), + path( + "measurement/", + measurement.MeasurementWithoutEvent.as_view(), + name="measurementWO", + ), ] diff --git a/mercury/views/measurement.py b/mercury/views/measurement.py index 797d9c7a..d8cf10f6 100644 --- a/mercury/views/measurement.py +++ b/mercury/views/measurement.py @@ -12,12 +12,42 @@ def build_error(str): return json.dumps({"error": str}) +def add_measurement(request, event): + json_data = request.data + if isinstance(json_data, str): + json_data = json.loads(json_data) + + res = {"event_uuid": event.uuid} + key_map = { + "timestamp": "date", + "sensor_id": "sensor_id", + "value": "values", + } + + for key, json_key in key_map.items(): + if json_key not in json_data: + return Response( + build_error("Missing required params " + json_key), + status=status.HTTP_400_BAD_REQUEST, + ) + res[key] = json_data[json_key] + + serializer = AGMeasurementSerializer(data=res) + try: + serializer.is_valid(raise_exception=True) + serializer.save() + except serializers.ValidationError: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + return Response(serializer.data, status=status.HTTP_201_CREATED) + + class MeasurementView(APIView): def post(self, request, event_uuid=None): """ The post receives sensor data through internet Url example: - http://localhost:8000/radioreceiver/d81cac8d-26e1-4983-a942-1922e54a943d + http://localhost:8000/measurement/d81cac8d-26e1-4983-a942-1922e54a943d Post Json Data Example { "sensor_id": 1, @@ -38,26 +68,19 @@ def post(self, request, event_uuid=None): build_error("Event uuid not found"), status=status.HTTP_404_NOT_FOUND ) - json_data = request.data - if isinstance(json_data, str): - json_data = json.loads(json_data) + return add_measurement(request, event) - res = {"event_uuid": event_uuid} - dic = {"timestamp": "date", "sensor_id": "sensor_id", "value": "values"} - for d in dic: - if json_data.get(dic[d]) is None: - return Response( - build_error("Missing required params " + dic[d]), - status=status.HTTP_400_BAD_REQUEST, - ) - res[d] = json_data[dic[d]] - - serializer = AGMeasurementSerializer(data=res) +class MeasurementWithoutEvent(APIView): + def post(self, request): + """ + TODO: fetch the active event + Now we use the first event in the db + """ try: - serializer.is_valid(raise_exception=True) - serializer.save() - except serializers.ValidationError: - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - else: - return Response(serializer.data, status=status.HTTP_201_CREATED) + events = AGEvent.objects.all() + event = events.first() + except AGEvent.DoesNotExist: + event = False + + return add_measurement(request, event)