Пример #1
0
def get_flow_meter(request, flow_meter_id):
    meter = get_object_or_404(models.FlowMeter, id=flow_meter_id)

    if request.method == "DELETE":
        meter.delete()
        return RESULT_OK

    elif request.method == "POST":
        form = UpdateFlowMeterForm(request.POST, instance=meter)
        if form.is_valid():
            meter = form.save()
        else:
            errors = _form_errors(form)
            raise kbapi.BadRequestError(errors)

    return protolib.ToProto(meter, full=True)
Пример #2
0
def get_controller(request, controller_id):
    controller = get_object_or_404(models.Controller, id=controller_id)

    if request.method == "DELETE":
        controller.delete()
        return RESULT_OK

    elif request.method == "POST":
        form = ControllerForm(request.POST, instance=controller)
        if form.is_valid():
            controller = form.save()
        else:
            errors = _form_errors(form)
            raise kbapi.BadRequestError(errors)

    return protolib.ToProto(controller, full=True)
Пример #3
0
def get_flow_toggle(request, flow_toggle_id):
    toggle = get_object_or_404(models.FlowToggle, id=flow_toggle_id)

    if request.method == "DELETE":
        toggle.delete()
        return RESULT_OK

    elif request.method == "POST":
        form = FlowToggleForm(request.POST, instance=toggle)
        if form.is_valid():
            toggle = form.save()
        else:
            errors = _form_errors(form)
            raise kbapi.BadRequestError(errors)

    return protolib.ToProto(toggle, full=True)
Пример #4
0
def tap_calibrate(request, meter_name_or_id):
    # TODO(mikey): This would make more semantic sense as PATCH /taps/tap-name/,
    # but Django's support for non-POST verbs is poor (specifically wrt request
    # body/form handling).
    tap = get_tap_from_meter_name_or_404(meter_name_or_id)
    meter = tap.current_meter()
    if not meter:
        raise kbapi.BadRequestError('Tap does not have a meter!')

    form = forms.CalibrateTapForm(request.POST)
    if form.is_valid():
        meter.ticks_per_ml = 1.0 / form.cleaned_data['ml_per_tick']
        meter.save()
        tap = get_tap_from_meter_name_or_404(meter_name_or_id)
    else:
        raise kbapi.BadRequestError, _form_errors(form)
    return protolib.ToProto(tap, full=True)
Пример #5
0
    def CancelDrink(self, seqn, spilled=False):
        try:
            d = self._site.drinks.get(seqn=seqn)
        except models.Drink.DoesNotExist:
            return

        keg = d.keg
        user = d.user
        session = d.session

        # Transfer volume to spillage if requested.
        if spilled and d.volume_ml and d.keg:
            d.keg.spilled_ml += d.volume_ml
            d.keg.save()

        d.status = 'deleted'
        d.save()

        # Invalidate all statistics.
        models.SystemStats.objects.filter(site=self._site).delete()
        models.KegStats.objects.filter(site=self._site, keg=d.keg).delete()
        models.UserStats.objects.filter(site=self._site, user=d.user).delete()
        models.SessionStats.objects.filter(site=self._site,
                                           session=d.session).delete()

        # Delete any SystemEvents for this drink.
        models.SystemEvent.objects.filter(site=self._site, drink=d).delete()

        # Regenerate new statistics, based on the most recent drink
        # post-cancellation.
        last_qs = self._site.drinks.valid().order_by('-seqn')
        if last_qs:
            last_drink = last_qs[0]
            last_drink._UpdateSystemStats()

        if keg:
            keg.RecomputeStats()
        if user and user.get_profile():
            user.get_profile().RecomputeStats()
        if session:
            session.Rebuild()
            session.RecomputeStats()

        # TODO(mikey): recompute session.
        return protolib.ToProto(d)
Пример #6
0
def register(request):
    if not request.POST:
        raise kbapi.BadRequestError('POST required.')
    form = forms.RegisterForm(request.POST)
    errors = {}
    if not form.is_valid():
        errors = _form_errors(form)
    else:
        username = form.cleaned_data['username']
        email = form.cleaned_data.get('email', None)
        password = form.cleaned_data.get('password', None)
        photo = request.FILES.get('photo', None)
        try:
            user = request.backend.create_new_user(username, email=email,
                                                   password=password, photo=photo)
            return protolib.ToProto(user, full=True)
        except backend.exceptions.UserExistsError:
            user_errs = errors.get('username', [])
            user_errs.append('Username not available.')
            errors['username'] = user_errs
    raise kbapi.BadRequestError(errors)
Пример #7
0
    def LogSensorReading(self, sensor_name, temperature, when=None):
        if not when:
            when = datetime.datetime.now()

        # The maximum resolution of ThermoSensor records is 1 minute.  Round the
        # time down to the nearest minute; if a record already exists for this time,
        # replace it.
        when = when.replace(second=0, microsecond=0)

        # If the temperature is out of bounds, reject it.
        min_val = kb_common.THERMO_SENSOR_RANGE[0]
        max_val = kb_common.THERMO_SENSOR_RANGE[1]
        if temperature < min_val or temperature > max_val:
            raise ValueError, 'Temperature out of bounds'

        sensor = self._GetSensorFromName(sensor_name)
        defaults = {
            'temp': temperature,
        }
        record, created = models.Thermolog.objects.get_or_create(
            site=self._site, sensor=sensor, time=when, defaults=defaults)
        record.temp = temperature
        record.save()
        return protolib.ToProto(record)
Пример #8
0
def end_keg(request, keg_id):
    keg = get_object_or_404(models.Keg, id=keg_id)
    keg = request.backend.end_keg(keg)
    return protolib.ToProto(keg, full=True)
Пример #9
0
def get_keg(request, keg_id):
    keg = get_object_or_404(models.Keg, id=keg_id)
    return protolib.ToProto(keg, full=True)
Пример #10
0
def get_session(request, session_id):
    session = get_object_or_404(models.DrinkingSession, id=session_id)
    return protolib.ToProto(session, full=True)
Пример #11
0
def add_drink_photo(request, drink_id):
    if request.method != 'POST':
        raise Http404('Method not supported')
    drink = get_object_or_404(models.Drink, id=drink_id)
    pic = _save_pour_pic(request, drink)
    return protolib.ToProto(pic, full=True)
Пример #12
0
def get_drink(request, drink_id):
    drink = get_object_or_404(models.Drink, id=drink_id)
    return protolib.ToProto(drink, full=True)
Пример #13
0
def get_session(request, session_id):
    session = get_object_or_404(models.DrinkingSession,
                                seqn=session_id,
                                site=request.kbsite)
    return protolib.ToProto(session, full=True)
Пример #14
0
def _tap_detail_get(request, tap):
    return protolib.ToProto(tap, full=True)
Пример #15
0
def to_dict(data):
    if not isinstance(data, Message):
        data = protolib.ToProto(data, full=True)
    return Dict(protoutil.ProtoMessageToDict(data))
Пример #16
0
def tap_disconnect_toggle(request, meter_name_or_id):
    tap = get_tap_from_meter_name_or_404(meter_name_or_id)
    tap = request.backend.connect_toggle(tap, None)
    return protolib.ToProto(tap, full=True)
Пример #17
0
def _tap_detail_get(request, tap_id):
    tap = get_object_or_404(models.KegTap,
                            meter_name=tap_id,
                            site=request.kbsite)
    return protolib.ToProto(tap, full=True)
Пример #18
0
def all_events(request):
    events = request.kbsite.events.all().order_by('-seqn')
    events = apply_since(request, events)
    events = events[:10]
    return [protolib.ToProto(e, full=True) for e in events]
Пример #19
0
def get_keg(request, keg_id):
    keg = get_object_or_404(models.Keg, seqn=keg_id, site=request.kbsite)
    return protolib.ToProto(keg, full=True)
Пример #20
0
def all_events(request):
    events = models.SystemEvent.objects.all().order_by('-id')
    events = apply_since(request, events)
    events = events[:10]
    return [protolib.ToProto(e, full=True) for e in events]
Пример #21
0
def pictures(request):
    if request.method != "POST":
        raise Http404("Method not supported")
    pic = models.Picture.objects.create(image=request.FILES["photo"], )
    return protolib.ToProto(pic, full=True)
Пример #22
0
def get_user(request, username):
    user = get_object_or_404(models.User, username=username)
    return protolib.ToProto(user, full=True)
Пример #23
0
def last_drink(request):
    drinks = models.Drink.objects.all().order_by('-id')
    if not drinks:
        raise Http404
    return protolib.ToProto(drinks[0], full=True)
Пример #24
0
def pictures(request):
    if request.method != 'POST':
        raise Http404('Method not supported')
    pic = models.Picture.objects.create(image=request.FILES['photo'], )
    return protolib.ToProto(pic, full=True)
Пример #25
0
def get_drink(request, drink_id):
    drink = get_object_or_404(models.Drink, seqn=drink_id, site=request.kbsite)
    return protolib.ToProto(drink, full=True)
Пример #26
0
def end_keg(request, keg_id):
    keg = get_object_or_404(models.Keg, id=keg_id)
    tap = keg.current_tap
    keg = request.backend.end_keg(tap)
    return protolib.ToProto(keg, full=True)
Пример #27
0
 def GetAllTaps(self):
     return protolib.ToProto(list(models.KegTap.objects.all()))