From 65a594e876a008220f650dadcbfb1a0d05524e8b Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Fri, 4 Jan 2019 15:09:44 -0600 Subject: [PATCH] Added restock command +Updates the restock time of an item at a shop +Should be easier than deleting and then adding the item --- api/commands.py | 38 ++++++++++++++++++++++++++++---------- test/test_commands.py | 24 +++++++++++++++++++----- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/api/commands.py b/api/commands.py index 6cbd219..8fea7d6 100644 --- a/api/commands.py +++ b/api/commands.py @@ -3,6 +3,7 @@ from GeoffreyApp.models import * from GeoffreyApp.errors import * from GeoffreyApp.minecraft_api import * import inspect +import datetime command_dict = {"GET": {}, "POST": {}, "DELETE": {}} @@ -46,10 +47,8 @@ def get_player(discord_uuid=None, mc_uuid=None): def get_location(owner, name=None, loc_type=Location): if name is None: - if loc_type == Location: - loc_list = Location.objects.all().filter(owner=owner) - else: - loc_list = Location.objects.all().select_related(loc_type.__name__.lower()).filter(owner=owner) + loc_list = loc_type.objects.filter(owner=owner).all() + if len(loc_list) == 1: loc = loc_list[0] elif len(loc_list) == 0: @@ -57,10 +56,8 @@ def get_location(owner, name=None, loc_type=Location): else: raise EntryNameNotUniqueError else: - if loc_type == Location: - loc_list = Location.objects.all().filter(owner=owner) - else: - loc_list = Location.objects.all().select_related(loc_type.__name__.lower()).filter(owner=owner) + loc_list = loc_type.objects.filter(owner=owner, name__iexact=name).all() + if len(loc_list) == 1: loc = loc_list[0] else: @@ -232,7 +229,7 @@ def selling(item_name, sort="normalized_price"): ''' :request: GET :param item_name: Item name to search for - :param sort: Field to sort shop results by, default is price + :param sort: Field to sort shop results by, default is normalized_price :return: List of top matching shops, sorted by the :help: Lists shops selling an item. ''' @@ -247,6 +244,7 @@ def selling(item_name, sort="normalized_price"): if len(shops) == 0: raise ItemNotFound + # Removes duplicates shops = [i for n, i in enumerate(shops) if i not in shops[n + 1:]] for shop_id in shops: @@ -375,7 +373,7 @@ def edit_name(new_name, loc_name, discord_uuid=None, mc_uuid=None): @command("DELETE") -def delete_item(item, shop_name, discord_uuid=None, mc_uuid=None): +def delete_item(item, shop_name=None, discord_uuid=None, mc_uuid=None): ''' :request: DELETE :param item: Item name to delete @@ -412,3 +410,23 @@ def me(discord_uuid=None, mc_uuid=None): raise PlayerNotFound return objects_list_to_json(locations) + + +@command("POST") +def restock(item_name, shop_name=None, discord_uuid=None, mc_uuid=None): + ''' + :request: POST + :param item_name: Item to restock + :param shop_name: Shop the item is in, can be none if the only one location is owned by the user + :param discord_uuid: Discord UUID + :param mc_uuid: Minecraft UUID + :return: + ''' + owner = get_player(discord_uuid, mc_uuid) + shop = get_location(owner, shop_name, Shop) + + items = ItemListing.objects.filter(item_name__iexact=item_name, shop=shop).all() + + for item in items: + item.date_restocked = datetime.datetime.now() + item.save() diff --git a/test/test_commands.py b/test/test_commands.py index d83f23a..817f6c9 100644 --- a/test/test_commands.py +++ b/test/test_commands.py @@ -1,6 +1,7 @@ from django.test import TestCase from GeoffreyApp.api.commands import * from GeoffreyApp.models import * +from time import sleep # Create your tests here. @@ -10,6 +11,7 @@ USERNAME = "ZeroHD" class CommandsAPITestCase(TestCase): + def setUp(self): self.player = Player.objects.create(name=USERNAME, mc_uuid=MC_UUID, discord_uuid=DISCORD_UUID) @@ -22,13 +24,13 @@ class CommandsAPITestCase(TestCase): Tunnel.objects.all().delete() def populate(self): - base = Base.objects.create(owner=self.player, name="test", x_coord=0, z_coord=0, dimension="O") - shop = Shop.objects.create(owner=self.player, name="test shop", x_coord=500, z_coord=500, + self.base = Base.objects.create(owner=self.player, name="test", x_coord=0, z_coord=0, dimension="O") + self.shop = Shop.objects.create(owner=self.player, name="test shop", x_coord=500, z_coord=500, dimension="O") - item = ItemListing.objects.create(shop=shop, price=1, amount=5, item_name="sed") + self.item = ItemListing.objects.create(shop=self.shop, price=1, amount=5, item_name="sed") - tunnel = Tunnel.objects.create(tunnel_number="42", tunnel_direction="S", location=base) + self.tunnel = Tunnel.objects.create(tunnel_number="42", tunnel_direction="S", location=self.base) def test_register(self): register(player_name="Vakky", discord_uuid="229423434256351233") @@ -126,7 +128,7 @@ class CommandsAPITestCase(TestCase): self.assertEqual(len(locations), 1) - def delete_item(self): + def test_delete_item(self): self.populate() delete_item(item="sed", shop_name="test shop", discord_uuid=DISCORD_UUID) @@ -134,3 +136,15 @@ class CommandsAPITestCase(TestCase): items = ItemListing.objects.filter(item_name__iexact="sed").all() self.assertEqual(len(items), 0) + + def test_restock(self): + self.populate() + sleep(1) + item = ItemListing.objects.create(shop=self.shop, price=1, amount=5, item_name="sed2") + + restock(item_name="sed", shop_name=self.shop.name, discord_uuid=DISCORD_UUID) + + new_item = ItemListing.objects.get(item_name="sed2") + old_item = ItemListing.objects.get(item_name="sed") + + self.assertGreater(old_item.date_restocked, new_item.date_restocked)