Added ORM relations for deleting objects

doc_update
Joey Hines 2018-07-21 09:18:01 -05:00
parent ba5da981b4
commit c821491cd4
3 changed files with 38 additions and 18 deletions

View File

@ -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',
}

View File

@ -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)

View File

@ -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)