Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API endpoints to add remove tap temp sensors #409

Merged
merged 4 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pykeg/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,21 @@ def connect_toggle(self, tap, new_toggle):

return tap

@transaction.atomic
def connect_thermo(self, tap, new_thermo):
tap = self._get_tap(tap)
old_thermo = tap.temperature_sensor

if old_thermo != new_thermo:
if old_thermo:
tap.temperature_sensor = None
tap.save()
if new_thermo:
tap.temperature_sensor = new_thermo
tap.save()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd think something like this would be cleaner:

if new_thermo:
  tap.temperature_sensor = new_thermo
else:
  tap.temperature_sensor = None
tap.save()

Unless the tap needs to be saved without a thermo before it can be saves with a thermo.


return tap

def build_stats(self, drink_id):
"""Build statistics for the given drink (or drink), without adjusting
any future statistics.
Expand Down
4 changes: 4 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,6 @@ 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
21 changes: 21 additions & 0 deletions pykeg/web/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,27 @@ def tap_disconnect_toggle(request, meter_name_or_id):
tap = request.backend.connect_toggle(tap, None)
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):
Expand Down