theLaborInVain / kdm-manager-api

The API used by https://kdm-manager.com and related Kingdom Death: Monster utilities.
Other
3 stars 0 forks source link

Settlement: get_summary endpoint throws a missing key error for settlements without a version #58

Closed toconnell closed 2 years ago

toconnell commented 2 years ago

Seems like we need to update the baseline() method?

User OID: 5a2e5fa98740d904ca10e728 Method: GET URL: http://api.kdm-manager.com/settlement/get_summary/5a4eb8848740d94f1435b314 JSON: None

Traceback (most recent call last):
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
 rv = self.dispatch_request()
File "/home/toconnell/kdm-manager-api/venv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
 return self.view_functions[rule.endpoint](**req.view_args)
File "/home/toconnell/kdm-manager-api/app/utils/crossdomain.py", line 56, in wrapped_function
 resp = flask.make_response(func(*args, **kwargs))
File "/home/toconnell/kdm-manager-api/app/routes.py", line 469, in collection_action
 asset_object = assets.get_user_asset(collection, asset_id)
File "/home/toconnell/kdm-manager-api/app/assets/__init__.py", line 87, in get_user_asset
 return settlements.Settlement(_id=asset_id)
File "/home/toconnell/kdm-manager-api/app/utils/__init__.py", line 285, in not_timed
 return method(*args, **kwargs)
File "/home/toconnell/kdm-manager-api/app/models/settlements.py", line 135, in __init__
 self.init_asset_collections()
File "/home/toconnell/kdm-manager-api/app/models/settlements.py", line 167, in init_asset_collections
 self.Endeavors = endeavors.Assets(self.settlement['version'])
KeyError: 'version'
toconnell commented 2 years ago

Yeah, so _init_assetcollections() gets called before normalize() and we're feeding in a raw settlement attribute, which is a bad pattern, so the solution here is probably going to swap in a _getversion() call...?

toconnell commented 2 years ago

Alright, this is fixed in dev, so I'm closing the ticket, but this needs more regression testing before it goes out the door.

diff --git a/app/models/settlements.py b/app/models/settlements.py
index 2935bef..14e8e2a 100644
--- a/app/models/settlements.py
+++ b/app/models/settlements.py
@@ -163,15 +163,19 @@ class Settlement(models.UserAsset):
             err = "Settlement object must be loaded before assets can init!"
             raise AttributeError(err)

+        # set versions first, since this is used by the get_versions() method
+        #   that gets called to get assets at the settlement's version
+        self.Versions = versions.Assets()
+
         self.Campaigns = campaigns.Assets()
-        self.Endeavors = endeavors.Assets(self.settlement['version'])
+        self.Endeavors = endeavors.Assets(self.get_version(str))
         self.Events = events.Assets()
         self.Expansions = expansions.Assets()
         self.FightingArts = fighting_arts.Assets()
-        self.Gear = gear.Assets(self.settlement['version'])
-        self.Resources = resources.Assets(self.settlement['version'])
+        self.Gear = gear.Assets(self.get_version(str))
+        self.Resources = resources.Assets(self.get_version(str))
         self.Innovations = innovations.Assets()
-        self.Locations = locations.Assets(self.settlement['version'])
+        self.Locations = locations.Assets(self.get_version(str))
         self.Macros = macros.Assets()
         self.Milestones = milestone_story_events.Assets()
         self.Monsters = monsters.Assets()
@@ -182,7 +186,6 @@ class Settlement(models.UserAsset):
         self.Survivors = survivors.Assets()
         self.SurvivorColorSchemes = color_schemes.Assets()
         self.SurvivorStatusFlags = status_flags.Assets()
-        self.Versions = versions.Assets()
         self.WeaponMasteries = weapon_masteries.Assets()
         self.StrainMilestones = strain_milestones.Assets()

@@ -3752,9 +3755,20 @@ class Settlement(models.UserAsset):
         return TL

-    def get_version(self):
+    def get_version(self, return_type=object):
         """ Returns the version object. """
-        return self.Versions.get_asset(self.settlement['version'])
+
+        # set a default:
+        version_object = self.Versions.get_asset('core_1_3')
+
+        # override the default if the value exists
+        if hasattr(self.settlement, 'version'):
+            version_object = self.Versions.get_asset(self.settlement['version'])
+
+        # handle 'return_type' kwarg
+        if return_type == str:
+            return version_object['handle']
+        return version_object