Finalise the crumbs collections objects to operate as inventories

This commit is contained in:
Ben 2019-05-29 23:00:05 +01:00
parent da8fa75586
commit 1127cedb21
8 changed files with 78 additions and 16 deletions

View File

@ -12,25 +12,44 @@ class BaseCrumbsCollection(dict):
self._model = kwargs.get('model') self._model = kwargs.get('model')
self._key = kwargs.get('key') self._key = kwargs.get('key')
self._inventory_model = kwargs.get('inventory_model') self._inventory_model = kwargs.get('inventory_model')
self._inventory_key = kwargs.get('inventory_key')
self._inventory_value = kwargs.get('inventory_value')
self._inventory_id = kwargs.get('inventory_id') self._inventory_id = kwargs.get('inventory_id')
self._model_key_column = getattr(self._model, self._key)
self._is_inventory = self._inventory_model is not None and self._inventory_id is not None self._is_inventory = self._inventory_model is not None and self._inventory_id is not None
async def get(self, k):
if self._is_inventory: if self._is_inventory:
return self[k] self._inventory_key_column = getattr(self._inventory_model, self._inventory_key)
self._inventory_value_column = getattr(self._inventory_model, self._inventory_value)
async def get(self, k):
try: try:
return self[k] return self[k]
except KeyError as e: except KeyError as e:
result = await self._model.query.where(getattr(self._model, self._key) == k).gino.first() query = self._model.load(parent=self._inventory_model).where(
(self._inventory_key_column == self._inventory_id) & (self._inventory_value_column == k)
) if self._is_inventory else self._model.query.where(self._model_key_column == k)
result = await query.gino.first()
if result: if result:
self[k] = result self[k] = result
return result return result
raise e raise e
async def set(self, k=None, **kwargs):
if self._is_inventory and k:
kwargs = {self._inventory_key: self._inventory_id, self._inventory_value: k}
self[k] = await self._inventory_model.create(**kwargs)
else:
model_instance = await self._model.create(**kwargs)
k = getattr(model_instance, self._key)
self[k] = model_instance
return self[k]
async def __collect(self): async def __collect(self):
query = self._model.load(parent=self._inventory_model).where( query = self._model.load(parent=self._inventory_model).where(
self._inventory_model.PenguinID == self._inventory_id self._inventory_key_column == self._inventory_id
) if self._is_inventory else self._model.query ) if self._is_inventory else self._model.query
async with db.transaction(): async with db.transaction():

View File

@ -72,25 +72,42 @@ class Location(db.Model):
class IglooCrumbsCollection(BaseCrumbsCollection): class IglooCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Igloo, key='ID', inventory_model=PenguinIgloo, super().__init__(model=Igloo,
key='id',
inventory_model=PenguinIgloo,
inventory_key='penguin_id',
inventory_value='igloo_id',
inventory_id=inventory_id) inventory_id=inventory_id)
class LocationCrumbsCollection(BaseCrumbsCollection): class LocationCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Location, key='ID', inventory_model=PenguinLocation, super().__init__(model=Location,
key='id',
inventory_model=PenguinLocation,
inventory_key='penguin_id',
inventory_value='location_id',
inventory_id=inventory_id) inventory_id=inventory_id)
class FurnitureCrumbsCollection(BaseCrumbsCollection): class FurnitureCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Furniture, key='ID', inventory_model=PenguinFurniture, super().__init__(model=Furniture,
key='id',
inventory_model=PenguinFurniture,
inventory_key='penguin_id',
inventory_value='furniture_id',
inventory_id=inventory_id) inventory_id=inventory_id)
class FlooringCrumbsCollection(BaseCrumbsCollection): class FlooringCrumbsCollection(BaseCrumbsCollection):
def __init__(self): def __init__(self, inventory_id=None):
super().__init__(model=Flooring, key='ID') super().__init__(model=Flooring,
key='id',
inventory_model=PenguinFlooring,
inventory_key='penguin_id',
inventory_value='flooring_id',
inventory_id=inventory_id)

View File

@ -20,5 +20,9 @@ class Item(db.Model):
class ItemCrumbsCollection(BaseCrumbsCollection): class ItemCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Item, key='ID', inventory_model=PenguinItem, super().__init__(model=Item,
key='id',
inventory_model=PenguinItem,
inventory_key='penguin_id',
inventory_value='item_id',
inventory_id=inventory_id) inventory_id=inventory_id)

View File

@ -14,5 +14,9 @@ class Postcard(db.Model):
class PostcardCrumbsCollection(BaseCrumbsCollection): class PostcardCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Postcard, key='ID', inventory_model=PenguinPostcard, super().__init__(model=Postcard,
key='id',
inventory_model=PenguinPostcard,
inventory_key='recipient_id',
inventory_value='postcard_id',
inventory_id=inventory_id) inventory_id=inventory_id)

View File

@ -18,5 +18,7 @@ class Card(db.Model):
class CardCrumbsCollection(BaseCrumbsCollection): class CardCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Card, key='ID', inventory_model=PenguinCard, super().__init__(model=Card,
key='id',
inventory_model=PenguinCard,
inventory_id=inventory_id) inventory_id=inventory_id)

View File

@ -13,5 +13,9 @@ class Permission(db.Model):
class PermissionCrumbsCollection(BaseCrumbsCollection): class PermissionCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Permission, key='ID', inventory_model=PenguinPermission, super().__init__(model=Permission,
key='name',
inventory_model=PenguinPermission,
inventory_key='penguin_id',
inventory_value='permission_id',
inventory_id=inventory_id) inventory_id=inventory_id)

View File

@ -63,12 +63,20 @@ class PuffleTreasurePuffleItem(db.Model):
class PuffleCrumbsCollection(BaseCrumbsCollection): class PuffleCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Puffle, key='ID', inventory_model=PenguinPuffle, super().__init__(model=Puffle,
key='id',
inventory_model=PenguinPuffle,
inventory_key='penguin_id',
inventory_value='puffle_id',
inventory_id=inventory_id) inventory_id=inventory_id)
class PuffleItemCrumbsCollection(BaseCrumbsCollection): class PuffleItemCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=PuffleItem, key='ID', inventory_model=PenguinPuffleItem, super().__init__(model=PuffleItem,
key='id',
inventory_model=PenguinPuffleItem,
inventory_key='penguin_id',
inventory_value='item_id',
inventory_id=inventory_id) inventory_id=inventory_id)

View File

@ -39,5 +39,9 @@ class CoverStamp(db.Model):
class StampCrumbsCollection(BaseCrumbsCollection): class StampCrumbsCollection(BaseCrumbsCollection):
def __init__(self, inventory_id=None): def __init__(self, inventory_id=None):
super().__init__(model=Stamp, key='ID', inventory_model=PenguinStamp, super().__init__(model=Stamp,
key='id',
inventory_model=PenguinStamp,
inventory_key='penguin_id',
inventory_value='stamp_id',
inventory_id=inventory_id) inventory_id=inventory_id)