def fuel_data(request, pos_id): try: params = extract_datatable_params(request) pos_id = int(pos_id) except: return HttpResponseBadRequest() pos = get_object_or_404(POS, item_id=pos_id) fuelTypeIDs = ( pos.fuel_type_id, constants.STRONTIUM_CLATHRATES_TYPEID, ) fuelTable = [] for type_id in fuelTypeIDs: try: fuel = pos.fuel_levels.filter(type_id=type_id).latest() quantity = fuel.current_fuel() consumption = fuel.consumption except FuelLevel.DoesNotExist: quantity = 0 consumption = 0 if consumption == 0: timeLeft = '-' else: hoursLeft = int(quantity / consumption) timeLeft = print_duration(seconds=hoursLeft * 3600, verbose=False) fuelTable.append([ type_id, Type.objects.get(typeID=type_id).typeName, print_fuel_quantity(quantity), '%d / hour' % consumption, '%d / day' % (consumption * 24), timeLeft, ]) json_data = { "sEcho": params.sEcho, "iTotalRecords": len(fuelTypeIDs), "iTotalDisplayRecords": len(fuelTypeIDs), "aaData": fuelTable } return HttpResponse(json.dumps(json_data))
def fuel_data(request, pos_id): try: params = extract_datatable_params(request) pos_id = int(pos_id) except: return HttpResponseBadRequest() pos = get_object_or_404(POS, item_id=pos_id) fuelTypeIDs = ( pos.fuel_type_id, constants.STRONTIUM_CLATHRATES_TYPEID, ) fuelTable = [] for type_id in fuelTypeIDs: try: fuel = pos.fuel_levels.filter(type_id=type_id).latest() quantity = fuel.current_fuel() consumption = fuel.consumption except FuelLevel.DoesNotExist: quantity = 0 consumption = 0 if consumption == 0: timeLeft = '-' else: hoursLeft = int(quantity / consumption) timeLeft = print_duration(seconds=hoursLeft * 3600, verbose=False) fuelTable.append([ type_id, Type.objects.get(typeID=type_id).typeName, print_fuel_quantity(quantity), '%d / hour' % consumption, '%d / day' % (consumption * 24), timeLeft, ]) json_data = { "sEcho" : params.sEcho, "iTotalRecords" : len(fuelTypeIDs), "iTotalDisplayRecords" : len(fuelTypeIDs), "aaData" : fuelTable } return HttpResponse(json.dumps(json_data))
def getFuelValue(pos, fuelTypeID, displayMode): try: fuel = pos.fuel_levels.filter(type_id=fuelTypeID).latest() quantity = fuel.current_fuel() consumption = fuel.consumption except FuelLevel.DoesNotExist: quantity = 0 consumption = 0 if displayMode == 'quantities': value = print_fuel_quantity(quantity) else: if consumption == 0: value = '-' else: hoursLeft = int(quantity / consumption) if displayMode == 'hours': value = '%dh' % hoursLeft elif displayMode == 'hours_int': value = hoursLeft else: value = print_duration(seconds=hoursLeft * 3600, verbose=False) return value