def carHistory(request, car_id, fromDate=None, toDate=None): user = request.user user_id = user.id car = Car.objects.filter(owner_id=user_id).filter(id=car_id) if not car: return HttpResponseRedirect('/') fromDateStr = utils.formatDateStr(fromDate) toDateStr = utils.formatDateStr(toDate, zeroHour=False) list_of_locations = LocationLog.objects.filter(car=car).filter(timestamp__range=[fromDateStr,toDateStr]).order_by('-timestamp') context = { 'menuParams' : utils.initMenuParameters(user), 'fromDateStr' : fromDateStr[:-9], # [:-9] truncates the hour 'toDateStr' : toDateStr[:-9], 'route_details':utils.RouteDetails(list_of_locations), 'user' : user, 'car': car[0], 'primary_driver' : car[0].getPrimaryDriversByDateRange(fromDateStr,toDateStr), 'temporary_drivers' : car[0].getTemporaryDriversByDateRange(fromDateStr,toDateStr), 'map_center_lat' : '32.047818', 'map_center_long' : '34.761265', 'activeMenu' : 'cars', } return render(request, 'carHistory/carHistory.html', context)
def get_fuel_data(request,fromDate,toDate): user = request.user message = "" if request.is_ajax(): if request.method == 'POST': all_details = generateCarsRouteContext(request,fromDate,toDate) car_names = [] km_per_liter = [] liter = [] km = [] for carRoutes in all_details['carsRoutes']: if carRoutes.locationList: car = carRoutes.locationList[0].car datestr = utils.formatDateStr(fromDate) fromDateTime = utils.makeDateFromDateStr(datestr) fuel_logs = FuelConsumptionLog.objects.filter(timestamp__year = fromDateTime.year).filter(timestamp__month = fromDateTime.month).filter(car = car) usage = 0 liters = 0 if fuel_logs: for log_line in fuel_logs: liters += log_line.liters usage = round(carRoutes.length() / liters, 3) km.append(carRoutes.length()) car_names.append(str(car)) km_per_liter.append(usage) liter.append(liters) message = json.dumps([car_names, km_per_liter, liter,km ]) return HttpResponse(message)
def generateCarsRouteContext(request, fromDate, toDate): user = request.user user_id = user.id cars = Car.objects.filter(owner_id=user_id) fromDateStr = utils.formatDateStr(fromDate) toDateStr = utils.formatDateStr(toDate, zeroHour=False) totalCarsRoutes = 0 carsRoutes=[] for car in cars: list_of_locations = LocationLog.objects.filter(car=car).filter(timestamp__range=[fromDateStr,toDateStr]).order_by('-timestamp') routeDetails = utils.RouteDetails(list_of_locations) totalCarsRoutes = totalCarsRoutes + routeDetails.length() carsRoutes.append(routeDetails) context = { 'user' : user, 'fromDateStr' : fromDateStr[:-9], # [:-9] truncates the hour 'toDateStr' : toDateStr[:-9], 'carsRoutes':carsRoutes, 'totalCarsRoutes':totalCarsRoutes, } return context
def generateDriverContext(user, driver_id, fromDate=None, toDate=None): user_id = user.id driver = Driver.objects.filter(owner_id=user_id).filter(id=driver_id) if not driver: return HttpResponseRedirect('/') fromDateStr = utils.formatDateStr(fromDate) toDateStr = utils.formatDateStr(toDate, zeroHour=False) #Need to get all primary\temporary of the driver in this dates temporary = TemporaryDriver.objects.filter(driver = driver).filter(Q(end__gte = fromDateStr) | Q(start__lte = toDateStr)) temporaryPeriodsLocations = utils.getLocationsOfPeriod(fromDateStr, toDateStr, temporary, isTemporaryDriver=True) primary = PrimaryDriver.objects.filter(driver = driver).filter(Q(end__gte = fromDateStr) | Q(end = None) ) primaryPeriodsLocations = utils.getLocationsOfPeriod(fromDateStr, toDateStr, primary) periodsLocations = temporaryPeriodsLocations + primaryPeriodsLocations total_length = 0 for period in periodsLocations: total_length += period.get_length() alerts = AlertLog.objects.filter(location_log__driver = driver[0]).filter(Q(location_log__timestamp__gte = fromDateStr) & Q(location_log__timestamp__lte = toDateStr)).filter(notification_sent = True) context = { 'menuParams' : utils.initMenuParameters(user), 'fromDateStr' : fromDateStr[:-9], # [:-9] truncates the hour 'toDateStr' : toDateStr[:-9], 'fromDate' : fromDate, 'toDate' : toDate, 'periodsLocations' : periodsLocations, 'total_length':total_length, 'alerts_count':len(alerts), 'user' : user, 'driver' : driver[0], 'map_center_lat' : '32.047818', 'map_center_long' : '34.761265', 'activeMenu' : 'drivers', } return context