def utility_track(request, pk): try: home = Home.get(pk) item = Track.objects.filter(home=home).order_by('-created')[:1] except Home.DoesNotExist: item = Track() except Track.DoesNotExist: item = Track() return json_write_items(item)
def utility_daily(request, pk): try: home = Home.get(pk) item = Daily.objects.filter(home=home).order_by('-created')[:30] except Home.DoesNotExist: item = Daily() except Daily.DoesNotExist: item = Daily() return json_write_items(item)
def get_home_devices(request): try: home = Home.get(request.GET['home']) hardwares = Hardware.objects.filter(home=home) devices = Device.objects.filter(hardware=hardwares) except KeyError: devices = None items = [] for device in devices: try: status = Status.objects.get(device=device) items.append(status) except Status.DoesNotExist: continue return json_write_items(items)
def get(self, request, *args, **kwargs): try: home_id = request.GET['home'] home = Home.get(home_id) request.session['home'] = home.id messages.add_message(request, messages.INFO, message='Now viewing Home %s' % str(home)) try: utility = Track.objects.get(home=home) except Track.DoesNotExist: utility = Track(home=home) return render(request, self.template_name, { 'home': home, 'utility': utility }) except KeyError: homes = Home.objects.all() return render(request, 'homes-index.html', {'homes': homes})
def home_track_chart(request, pk): today = date.today() try: home = Home.get(pk) items = Track.objects.filter(home=home).filter( created__contains=today).order_by('-created').values_list( 'created', 'power_main_w') count = len(items) items = reversed(items) chart = [["Time", "Energy Main"]] if count > 0: for item in items: t = timezone.localtime(item[0]).strftime('%H:%M') chart.append((t, item[1])) else: raise Track.DoesNotExist except Home.DoesNotExist: chart = [["Time", "Energy Main"], ["00:00", 0]] except Track.DoesNotExist: chart = [["Time", "Energy Main"], ["00:00", 0]] return HttpResponse(json.dumps(chart), content_type='text/json')
def get(self, request, *args, **kwargs): try: home = Home.get(request.session['home']) except KeyError: return reverse_lazy('index')