def save(self, *args, **kwargs): """if there's an existing checkin for this month/email, delete that one and replace it with this checkin""" existing_checkin = Commutersurvey.objects.filter( wr_day_month=self.wr_day_month, email=self.email) if self.id: # if this instance has already been saved we need to filter out this instance from our results. existing_checkin = existing_checkin.exclude(pk=self.id) if existing_checkin.exists(): existing_checkin.delete() """overwrites the save method in order to calculate all the data!""" changes = self.calculate_difference() self.carbon_change = sanely_rounded(changes["carbon"]) self.calorie_change = sanely_rounded(changes["calories"]) self.change_type = self.change_analysis() self.already_green = self.check_green() self.carbon_savings = sanely_rounded(self.carbon_saved()) self.calories_total = self.calories_totalled() super(Commutersurvey, self).save(*args, **kwargs)
def save(self, *args, **kwargs): """if there's an existing checkin for this month/email, delete that one and replace it with this checkin""" existing_checkin = Commutersurvey.objects.filter(wr_day_month=self.wr_day_month, email=self.email) if self.id: # if this instance has already been saved we need to filter out this instance from our results. existing_checkin = existing_checkin.exclude(pk=self.id) if existing_checkin.exists(): existing_checkin.delete() """overwrites the save method in order to calculate all the data!""" changes = self.calculate_difference() self.carbon_change = sanely_rounded(changes["carbon"]) self.calorie_change = sanely_rounded(changes["calories"]) self.change_type = self.change_analysis() self.already_green = self.check_green() self.carbon_savings = sanely_rounded(self.carbon_saved()) self.calories_total = self.calories_totalled() super(Commutersurvey, self).save(*args, **kwargs)
def calculate_difference(self): """ Calculates the difference in terms of calories burned and carbon emmitted between the commute of this check-in and that of an average (driving) commute. Returns results as a dict, with 'carbon' and 'calories' keyed to their changes """ legs = self.leg_set.only('carbon', 'calories', 'day').all() difference = {'carbon': 0.000, 'calories': 0.000} for leg in legs: if leg.day == 'w': difference["carbon"] += sanely_rounded(leg.carbon) difference["calories"] += sanely_rounded(leg.calories) elif leg.day == 'n': difference["carbon"] -= sanely_rounded(leg.carbon) difference["calories"] -= sanely_rounded(leg.calories) # import pdb; pdb.set_trace() return difference
def calc_metrics(self): """Calculate the carbon output and calories burned for this leg""" calories = 0.0 carbon = 0.0 if self.mode: met = self.mode.met or 0.0 carb = self.mode.carb or 0.0 kcal = float(met) # kcal/(kg*hour) from this mode if kcal > 0.0: #kcal burned by leg using average weight of 81 kg, #based on duration in minutes calories = kcal * (self.duration/60) * 80.7 #grams carbon dioxide per passenger-mile on this mode coo = float(carb) #grn = self.mode.green if coo > 0.0: #and grn<>'f' speed = self.mode.speed or 0.0 s = float(speed) # average speed in mph #kilograms carbon expended in leg based on duration in minutes carbon = (coo/1000) * s * (self.duration/60) return {'carbon': sanely_rounded(carbon), 'calories': sanely_rounded(calories)}
def carbon_saved(self): """returns the total carbon saved from all legs in kg""" normal_car_carbon = 0.0 wr_day_carbon = 0.0 legs = self.leg_set.only('carbon', 'day').all() for leg in legs: if leg.day == 'n': car_speed = Mode.objects.get(name="Driving alone").speed car_carbon = Mode.objects.get(name="Driving alone").carb/1000 carbon = car_carbon * car_speed * leg.duration/60 normal_car_carbon += carbon elif leg.day == 'w': wr_day_carbon += leg.carbon carbon_saved = normal_car_carbon - wr_day_carbon return sanely_rounded(carbon_saved)
def calc_metrics(self): """Calculate the carbon output and calories burned for this leg""" calories = 0.0 carbon = 0.0 if self.mode: met = self.mode.met or 0.0 carb = self.mode.carb or 0.0 kcal = float(met) # kcal/(kg*hour) from this mode if kcal > 0.0: #kcal burned by leg using average weight of 81 kg, #based on duration in minutes calories = kcal * (self.duration / 60) * 80.7 #grams carbon dioxide per passenger-mile on this mode coo = float(carb) #grn = self.mode.green if coo > 0.0: #and grn<>'f' speed = self.mode.speed or 0.0 s = float(speed) # average speed in mph #kilograms carbon expended in leg based on duration in minutes carbon = (coo / 1000) * s * (self.duration / 60) return { 'carbon': sanely_rounded(carbon), 'calories': sanely_rounded(calories) }
def carbon_saved(self): """returns the total carbon saved from all legs in kg""" normal_car_carbon = 0.0 wr_day_carbon = 0.0 legs = self.leg_set.only('carbon', 'day').all() for leg in legs: if leg.day == 'n': car_speed = Mode.objects.get(name="Driving alone").speed car_carbon = Mode.objects.get(name="Driving alone").carb / 1000 carbon = car_carbon * car_speed * leg.duration / 60 normal_car_carbon += carbon elif leg.day == 'w': wr_day_carbon += leg.carbon carbon_saved = normal_car_carbon - wr_day_carbon return sanely_rounded(carbon_saved)
def carbon_saved(self): """returns the total carbon saved from all legs in kg""" normal_car_carbon = 0.0 wr_day_carbon = 0.0 legs = self.leg_set.all() # don't calculate if all the legs are driving alone. # if legs.filter(day='n').count() == legs.filter(day='n', mode__name="Driving alone").count(): # return None for leg in legs: if leg.day == 'n': leg_distance = leg.duration/60 * leg.mode.speed # hr * mph = miles car_carbon = Mode.objects.get(name="Driving alone").carb/1000 # kilograms carbon per passenger-mile leg_car_carbon = car_carbon * leg_distance # kilograms normal_car_carbon += leg_car_carbon elif leg.day == 'w': wr_day_carbon += leg.carbon # kilograms carbon_saved = normal_car_carbon - wr_day_carbon return sanely_rounded(carbon_saved*1000) # grams
def carbon_saved(self): """returns the total carbon saved from all legs in kg""" normal_car_carbon = 0.0 wr_day_carbon = 0.0 legs = self.leg_set.all() # don't calculate if all the legs are driving alone. # if legs.filter(day='n').count() == legs.filter(day='n', mode__name="Driving alone").count(): # return None for leg in legs: if leg.day == 'n': leg_distance = leg.duration / 60 * leg.mode.speed # hr * mph = miles car_carbon = Mode.objects.get( name="Driving alone" ).carb / 1000 # kilograms carbon per passenger-mile leg_car_carbon = car_carbon * leg_distance # kilograms normal_car_carbon += leg_car_carbon elif leg.day == 'w': wr_day_carbon += leg.carbon # kilograms carbon_saved = normal_car_carbon - wr_day_carbon return sanely_rounded(carbon_saved * 1000) # grams