From 890b78140cb833eaa5a734025f38c10dd5d05d7d Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sat, 16 Feb 2019 21:13:53 -0600 Subject: [PATCH] Fixed a bunch of small command bugs --- api/commands.py | 48 ++++++++++++++++++++++++++++--------------- api/views.py | 1 - models.py | 2 +- test/test_commands.py | 2 +- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/api/commands.py b/api/commands.py index 0543667..c0f379c 100644 --- a/api/commands.py +++ b/api/commands.py @@ -84,11 +84,17 @@ def get_location(owner, name=None, loc_type=Location): def add_location(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None, loc_type=Location): player = get_player(discord_uuid, mc_uuid) + + name_was_none = False if name is None: name = "{}'s {}".format(player.name, loc_type.__name__) + name_was_none = True if Location.objects.filter(name__iexact=name).all().count() > 0: - raise EntryNameNotUniqueError + if name_was_none: + raise LocationLookUpError + else: + raise EntryNameNotUniqueError else: location = loc_type.objects.create(name=name, x_coord=x_pos, z_coord=z_pos) location.owner.add(player) @@ -147,7 +153,7 @@ def add_shop(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None): :param mc_uuid: Minecraft UUID :return: JSON representation of the new shop :raises: EntryNameNotUniqueError, PlayerNotFound, LocationLookupError - :help: Adds your shop to the database. The name is optional if this is your first shop + :help: Adds your shop to the database. ''' return add_location(x_pos, z_pos, name=name, discord_uuid=discord_uuid, mc_uuid=mc_uuid, loc_type=Shop) @@ -164,7 +170,7 @@ def add_town(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None): :param mc_uuid: Minecraft UUID :return: JSON representation of the new town :raises: EntryNameNotUniqueError, PlayerNotFound, LocationLookupError - :help: Adds your town to the database. The name is optional if this is your first town + :help: Adds your town to the database. ''' return add_location(x_pos, z_pos, name=name, discord_uuid=discord_uuid, mc_uuid=mc_uuid, loc_type=Town) @@ -277,7 +283,7 @@ def add_item(item_name, quantity, diamond_price, shop_name=None, discord_uuid=No :param mc_uuid: Minecraft UUID :return: Item Listing :raises: PlayerNotFound, LocationLookupError, EntryNameNotUniqueError, NoLocationsInDatabase - :help: Adds an item to a shop's inventory. If you have one shop, the shop name is not required + :help: Adds an item to a shop's inventory. ''' player = get_player(discord_uuid, mc_uuid) @@ -295,7 +301,6 @@ def selling(item_name): ''' :request: GET :param item_name: Item name to search for - :param sort: Field to sort shop results by, default is date_restocked :return: List of top matching shops, sorted by the :raises: ItemNotFound :help: Lists shops selling an item. Sorted by when they were last restocked. @@ -305,35 +310,38 @@ def selling(item_name): @command("GET") -def selling(item_name): +def selling_price(item_name): ''' :request: GET :param item_name: Item name to search for - :param sort: Field to sort shop results by, default is date_restocked :return: List of top matching shops, sorted by the :raises: ItemNotFound :help: Lists shops selling an item. Sorted lowest price to highest price. ''' - return get_selling(item_name, sort="-normalized_price") + return get_selling(item_name, sort="normalized_price") -def get_selling(item_name, sort="-date_restocked"): +def get_selling(item_name, sort): items = [] if len(item_name) == 0: raise EmptryString - shops = ItemListing.objects.annotate(normalized_price=F('price') / F('amount')) \ + all_shop_ids = ItemListing.objects.annotate(normalized_price=F('price') / F('amount')) \ .filter(item_name__icontains=item_name).order_by(sort).values('shop_id').all() - if len(shops) == 0: + if len(all_shop_ids) == 0: raise ItemNotFound # Removes duplicates - shops = [i for n, i in enumerate(shops) if i not in shops[n + 1:]] + shop_ids = [] - for shop_id in shops: + for shop_id in all_shop_ids: + if shop_id not in shop_ids: + shop_ids.append(shop_id) + + for shop_id in shop_ids: shop = Shop.objects.get(pk=shop_id['shop_id']).json item_query = ItemListing.objects.annotate(normalized_price=F('price') / F('amount')) \ @@ -367,7 +375,15 @@ def info(location_name): location = Location.objects.filter(name__iregex=".*{}.*".format(location_name)).first() if location is not None: - return location.loc_child_obj.json + loc = location.loc_child_obj + loc_json = loc.json + + if type(loc) == Shop: + loc_json["items"] = [] + for item in loc.shop_selling.all(): + loc_json["items"].append(item.json) + + return loc_json else: raise LocationLookUpError @@ -601,7 +617,7 @@ def add_resident(new_resident_name, town_name, discord_uuid=None, mc_uuid=None): town = get_location(owner, town_name, Town) if town.residents.filter(Q(town__owner__name__iexact=new_resident_name) | - Q(town__residents__name__iexact=new_resident_name)).all().count(): + Q(name__iexact=new_resident_name)).all().count(): raise IsResidentError town.residents.add(new_resident) @@ -627,7 +643,7 @@ def remove_resident(resident_name, town_name, discord_uuid=None, mc_uuid=None): owner = get_player(discord_uuid, mc_uuid) town = get_location(owner, town_name, Town) try: - resident = town.residents.get(town__residents__name__iexact=resident_name) + resident = town.residents.get(name__iexact=resident_name) except Player.DoesNotExist: raise ResidentNotFoundError diff --git a/api/views.py b/api/views.py index 9255c08..f185c06 100644 --- a/api/views.py +++ b/api/views.py @@ -49,7 +49,6 @@ def get_commands(): class CommandAPI(View): def get(self, request, command): get = request.GET - if check_token(get, commands_perm=True): if command.lower() == "commands": return JsonResponse(get_commands(), safe=False) diff --git a/models.py b/models.py index 01dbe16..3412cf1 100644 --- a/models.py +++ b/models.py @@ -137,7 +137,7 @@ class Location(models.Model): @property def link(self): - return self.loc_child_obj.link() + return self.loc_child_obj.link @property def loc_child_obj(self): diff --git a/test/test_commands.py b/test/test_commands.py index f97bed6..1ef84af 100644 --- a/test/test_commands.py +++ b/test/test_commands.py @@ -54,7 +54,7 @@ class CommandsAPITestCase(TestCase): self.assertEqual(base.owner.all()[0].name, "ZeroHD") - self.assertRaises(EntryNameNotUniqueError, add_base, x_pos=0, z_pos=0, name=None, discord_uuid=DISCORD_UUID) + self.assertRaises(LocationLookUpError, add_base, x_pos=0, z_pos=0, name=None, discord_uuid=DISCORD_UUID) def test_add_shop(self): add_shop(x_pos=0, z_pos=0, name=None, discord_uuid=DISCORD_UUID)