From c821491cd454d1400449844c0c3428fb4f7d1b38 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sat, 21 Jul 2018 09:18:01 -0500 Subject: [PATCH] Added ORM relations for deleting objects --- DatabaseModels.py | 33 ++++++++++++++++++++------------- Geoffrey.py | 10 +++++----- test_geoffreyDatabase.py | 13 +++++++++++++ 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/DatabaseModels.py b/DatabaseModels.py index cafce08..5e8f1f7 100644 --- a/DatabaseModels.py +++ b/DatabaseModels.py @@ -165,21 +165,26 @@ class DatabaseInterface: def search_all_fields(self, search): loc_string = '' + count = 0 expr = Location.owner.has(Player.name.ilike('%{}%'.format(search))) | Location.name.ilike('%{}%'.format(search)) for loc in self.database.query_by_filter(Location, expr): - loc_string = "{}\n\t{}".format(loc_string, loc) + loc_string = "{}\n{}".format(loc_string, loc) + count += 1 expr = Tunnel.owner.has(Player.name.ilike('%{}%'.format(search))) & Tunnel.location is None for loc in self.database.query_by_filter(Tunnel, expr): - loc_string = "{}\n\t{}".format(loc_string, loc) + loc_string = "{}\n{}".format(loc_string, loc) + count += 1 - return loc_string + if count == 0: + raise LocationLookUpError + else: + return loc_string def delete_location(self, owner, name): expr = (Location.owner == owner) & (Location.name == name) - - self.database.delete_entry(Location, expr) + self.database.delete_entry(Shop, expr) class DiscordDatabaseInterface(DatabaseInterface): @@ -319,8 +324,10 @@ class Player(SQL_Base): mc_uuid = Column(String) discord_uuid = Column(String) name = Column(String) - locations = relationship("Location", back_populates="owner", lazy='dynamic') - tunnels = relationship("Tunnel", back_populates="owner", lazy='dynamic') + locations = relationship("Location", back_populates="owner", lazy='dynamic', + cascade="save-update, merge, delete, delete-orphan") + tunnels = relationship("Tunnel", back_populates="owner", lazy='dynamic', + cascade="save-update, merge, delete, delete-orphan") def __init__(self, name, discord_id=None): self.mc_uuid = grab_UUID(name) @@ -334,9 +341,9 @@ class Tunnel(SQL_Base): tunnel_number = Column(Integer) tunnel_direction = Column(Enum(TunnelDirection)) owner_id = Column(Integer, ForeignKey('Players.id')) - owner = relationship("Player", back_populates="tunnels") + owner = relationship("Player", back_populates="tunnels", cascade="save-update, merge, delete") location_id = Column(Integer, ForeignKey('Locations.id')) - location = relationship("Location", back_populates="tunnel") + location = relationship("Location", back_populates="tunnel", cascade="save-update, merge, delete") def __init__(self, owner, tunnel_color, tunnel_number, location=None): try: @@ -360,11 +367,12 @@ class Location(SQL_Base): y = Column(Integer) z = Column(Integer) - tunnel = relationship("Tunnel", back_populates="location", uselist=False) + tunnel = relationship("Tunnel", back_populates="location", uselist=False, + cascade="save-update, merge, delete, delete-orphan") dimension = Column(Enum(Dimension)) owner_id = Column(Integer, ForeignKey('Players.id')) - owner = relationship("Player", back_populates="locations") + owner = relationship("Player", back_populates="locations", cascade="save-update, merge, delete") type = Column(String) __mapper_args__ = { @@ -403,12 +411,11 @@ class Location(SQL_Base): return self.info_str() - class Shop(Location): __tablename__ = 'Shops' shop_id = Column(Integer, ForeignKey('Locations.id'), primary_key=True) name = column_property(Column(String), Location.name) - inventory = relationship('ItemListing', back_populates='shop', lazy='dynamic') + inventory = relationship('ItemListing', back_populates='shop', cascade='all, delete-orphan') __mapper_args__ = { 'polymorphic_identity': 'Shop', } diff --git a/Geoffrey.py b/Geoffrey.py index 4dba414..08ca296 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -122,7 +122,7 @@ async def addshop(ctx, x_pos: int, z_pos: int, *args): name = '{}\'s_Shop'.format(database_interface.find_player_by_discord_uuid(ctx.message.author.id).name) try: - shop = database_interface.add_shop(ctx.message.author.id, name, x_pos, y_pos, z_pos) + shop = database_interface.add_shop(ctx.message.author.id, name, x_pos, z_pos) except LocationInitError: raise commands.UserInputError except EntryNameNotUniqueError: @@ -174,9 +174,9 @@ async def find(ctx, search: str): try: result = database_interface.search_all_fields(search) - await bot.say('{}, The following entires match **{}**: {}'.format(ctx.message.author.mention, search, result)) - except PlayerNotFound: - await bot.say('{}, no matches **{}** were found in the database'.format(ctx.message.author.mention, name)) + await bot.say('{}, The following entries match **{}**:\n{}'.format(ctx.message.author.mention, search, result)) + except LocationLookUpError: + await bot.say('{}, no matches **{}** were found in the database'.format(ctx.message.author.mention, search)) @bot.command(pass_context=True) @@ -315,7 +315,7 @@ def get_args_dict(args): def create_config(): config['Discord'] = {'Token': ''} - config['SQL'] = {'Dialect+Driver': 'test', 'username': '', 'password':'', 'host': '', 'port': '', 'database':''} + config['SQL'] = {'Dialect+Driver': 'Test', 'username': '', 'password':'', 'host': '', 'port': '', 'database':''} with open('GeoffreyConfig.ini', 'w') as configfile: config.write(configfile) diff --git a/test_geoffreyDatabase.py b/test_geoffreyDatabase.py index 437ea03..e66e3ae 100644 --- a/test_geoffreyDatabase.py +++ b/test_geoffreyDatabase.py @@ -198,6 +198,19 @@ class TestGeoffreyDatabase(TestCase): self.assertRaises(EntryNameNotUniqueError, self.interface.add_location, '143072699567177728', 'test', 0, 0, 0) + def test_delete_parent(self): + owner = self.add_player() + loc = self.add_shop() + + self.interface.add_item('143072699567177728', 'test', 'dirt', 1, 15) + + self.interface.delete_location('143072699567177728', 'test') + + shops = self.interface.find_shop_selling_item('dirt') + self.assertGreater(len(shops), 0) + + +