So I am running into the weirdest problem right now... My code only works once. After that, I need to change a print statement within the code or it does not work anymore.
I'm calling perform_city_actions_in_order
from an outside class and from there, as you can see I am calling get_city_action_queue
that should return a list of database objects. When it works, it then performs an action on the object and changes the status so it will not match the queries in get_city_action_queue
anymore. But when a row in the database table is changed so that it should match the queries, get_city_action_queue
does not return it anymore if I don't change a print statement within the code.
Also, if I change a print statement within perform_city_actions_in_order
it also works but only once.
class CityHelper:
def get_city_action_queue(city, actions_until_time=datetime.now()):
queue = []
fields = city.resource_fields.all().filter(status="Under Construction", action_done_time__lte=actions_until_time) | city.resource_fields.all().filter(status="Upgrading")
buildings = city.buildings.all().filter(status="Under Construction", action_done_time__lte=actions_until_time) | city.buildings.all().filter(status="Upgrading")
queue.extend(fields)
queue.extend(buildings)
print("this needs to change between executions for the above code to work")
return queue
def perform_city_actions_in_order(city, actions_until_time=datetime.now()):
print("city", city)
queue = CityHelper.get_city_action_queue(city, actions_until_time)
print("before", queue)
queue.sort(key = lambda x:x.action_done_time)
print("after", queue)
for action in queue:
print("perfofming ", action)
if(action.__class__.__name__ == "ResourceFieldModel"):
ResourceHelper.upgradeResourceField(action)
elif(action.__class__.__name__ == "BuildingModel"):
BuildingHelper.upgradeBuilding(action)
And here are my models but these should not be the problem as the logic in the code seems to work.
class BuildingModel(models.Model):
position = models.IntegerField(default=0)
building_type = models.CharField(max_length=32)
status = models.CharField(max_length=32)
action_done_time = models.DateTimeField(default=None, null=True)
level = models.IntegerField(default=0)
city = models.ForeignKey(CityModel, on_delete=models.CASCADE, null=True, related_name='buildings')
objects = BuildingManager()
def __str__(self):
return self.building_type + " in " + self.city.city_name + " at position: " + str(self.position)
class ResourceFieldModel(models.Model):
position = models.IntegerField(default=0)
field_type = models.CharField(max_length=10)
status = models.CharField(max_length=32)
action_done_time = models.DateTimeField(default=None, null=True, blank=True)
level = models.IntegerField(default=0, verbose_name='level')
city = models.ForeignKey(CityModel, on_delete=models.CASCADE, null=True, related_name='resource_fields')
objects = ResourceFieldManager()
def __str__(self):
return self.field_type + " at: " + str(self.position) + " in " + self.city.city_name