Fixed a bunch of small command bugs

doc_update
Joey Hines 2019-02-16 21:13:53 -06:00
parent 2513661003
commit 890b78140c
4 changed files with 34 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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