Skip to content

Commit

Permalink
Merge pull request #409 from johnnyruz/add_thermo_sensor_api_endpoints
Browse files Browse the repository at this point in the history
Add API endpoints to add remove tap temp sensors
  • Loading branch information
mik3y authored May 13, 2020
2 parents c9a8f17 + 6c62e93 commit d02f559
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pykeg/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,17 @@ def connect_toggle(self, tap, new_toggle):

return tap

@transaction.atomic
def connect_thermo(self, tap, new_thermo):
tap = self._get_tap(tap)
if new_thermo:
tap.temperature_sensor = new_thermo
else:
tap.temperature_sensor = None
tap.save()

return tap

def build_stats(self, drink_id):
"""Build statistics for the given drink (or drink), without adjusting
any future statistics.
Expand Down
5 changes: 5 additions & 0 deletions pykeg/web/api/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

ALL_METERS = models.FlowMeter.objects.all()
ALL_TOGGLES = models.FlowToggle.objects.all()
ALL_THERMOS = models.ThermoSensor.objects.all()


class DrinkPostForm(forms.Form):
Expand Down Expand Up @@ -76,3 +77,7 @@ class ConnectMeterForm(forms.Form):

class ConnectToggleForm(forms.Form):
toggle = forms.ModelChoiceField(queryset=ALL_TOGGLES, required=True)


class ConnectThermoForm(forms.Form):
thermo = forms.ModelChoiceField(queryset=ALL_THERMOS, required=True)
2 changes: 2 additions & 0 deletions pykeg/web/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
url(r"^thermo-sensors/?$", views.all_thermo_sensors),
url(r"^thermo-sensors/(?P<sensor_name>[^/]+)/?$", views.get_thermo_sensor),
url(r"^thermo-sensors/(?P<sensor_name>[^/]+)/logs/?$", views.get_thermo_sensor_logs),
url(r"^taps/(?P<meter_name_or_id>[\w\.-]+)/connect-thermo/?$", views.tap_connect_thermo),
url(r"^taps/(?P<meter_name_or_id>[\w\.-]+)/disconnect-thermo/?$", views.tap_disconnect_thermo),
url(r"^users/?$", views.user_list),
url(r"^users/(?P<username>[\w@.+-_]+)/drinks/?$", views.get_user_drinks),
url(r"^users/(?P<username>[\w@.+-_]+)/events/?$", views.get_user_events),
Expand Down
22 changes: 22 additions & 0 deletions pykeg/web/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,28 @@ def tap_disconnect_toggle(request, meter_name_or_id):
return protolib.ToProto(tap, full=True)


@require_http_methods(["POST"])
@csrf_exempt
@auth_required
def tap_connect_thermo(request, meter_name_or_id):
tap = get_tap_from_meter_name_or_404(meter_name_or_id)
form = forms.ConnectThermoForm(request.POST)
if form.is_valid():
tap = request.backend.connect_thermo(tap, form.cleaned_data["thermo"])
else:
raise kbapi.BadRequestError(_form_errors(form))
return protolib.ToProto(tap, full=True)


@require_http_methods(["POST"])
@csrf_exempt
@auth_required
def tap_disconnect_thermo(request, meter_name_or_id):
tap = get_tap_from_meter_name_or_404(meter_name_or_id)
tap = request.backend.connect_thermo(tap, None)
return protolib.ToProto(tap, full=True)


@auth_required
def _tap_detail_post(request, tap):
form = forms.DrinkPostForm(request.POST)
Expand Down

0 comments on commit d02f559

Please sign in to comment.