pacho_the_python

Untitled

Aug 3rd, 2024
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. def decrease_spacecrafts_weight():
  2.     affected_spacecrafts = (Spacecraft.objects
  3.                             .filter(mission__status="Planned", weight__gte=200.0)
  4.                             .distinct())
  5.  
  6.     if not affected_spacecrafts.exists():
  7.         return "No changes in weight."
  8.  
  9.     num_of_spacecrafts_affected = affected_spacecrafts.count()
  10.  
  11.     affected_spacecrafts.update(weight=F('weight') - 200.0)
  12.  
  13.     avg_weight = Spacecraft.objects.aggregate(avg_weight=Avg('weight'))['avg_weight']
  14.     if avg_weight is None:
  15.         avg_weight = 0.0
  16.     avg_weight = round(avg_weight, 1)
  17.  
  18.     return (f"The weight of {num_of_spacecrafts_affected} spacecrafts has been decreased. "
  19.             f"The new average weight of all spacecrafts is {avg_weight}kg.")
Advertisement
Add Comment
Please, Sign In to add comment