Added test file for commands and redid command decorators
+ Command decorator now puts functions to to one dict + Params, name, and the function object are in this dict.doc_update
parent
59bac55fd1
commit
83a1b7a175
|
@ -2,31 +2,25 @@ from django.db.models import Q, F
|
|||
from GeoffreyApp.models import *
|
||||
from GeoffreyApp.errors import *
|
||||
from GeoffreyApp.minecraft_api import *
|
||||
import inspect
|
||||
|
||||
post_list = []
|
||||
get_list = []
|
||||
delete_list = []
|
||||
command_dict = {"GET": {}, "POST": {}, "DELETE": {}}
|
||||
|
||||
|
||||
def post(func):
|
||||
def command():
|
||||
post_list.append(func)
|
||||
def getRequiredArgs(func):
|
||||
args = inspect.getfullargspec(func)
|
||||
|
||||
return command()
|
||||
return args.args + args.kwonlyargs
|
||||
|
||||
|
||||
def delete(func):
|
||||
def command():
|
||||
delete_list.append(func)
|
||||
def command(type):
|
||||
def command_dec(func):
|
||||
def add_command():
|
||||
command_dict[type][func.__name__] = {"func": func, "params": getRequiredArgs(func)}
|
||||
|
||||
return command()
|
||||
return add_command()
|
||||
|
||||
|
||||
def get(func):
|
||||
def command():
|
||||
get_list.append(func)
|
||||
|
||||
return command()
|
||||
return command_dec
|
||||
|
||||
|
||||
def get_player(discord_uuid=None, mc_uuid=None):
|
||||
|
@ -59,7 +53,7 @@ def get_location(owner, name=None, loc_type=Location):
|
|||
return loc
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def register(player_name, discord_uuid):
|
||||
mc_uuid = grab_UUID(player_name)
|
||||
player = Player.objects.create(name=player_name, mc_uuid=mc_uuid, discord_uuid=discord_uuid)
|
||||
|
@ -67,16 +61,16 @@ def register(player_name, discord_uuid):
|
|||
return player.json
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def add_base(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None):
|
||||
return add_location(x_pos, z_pos, name=name, discord_uuid=discord_uuid, mc_uuid=mc_uuid, loc_type=Base)
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def add_shop(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None):
|
||||
return add_location(x_pos, z_pos, name=name, discord_uuid=discord_uuid, mc_uuid=mc_uuid, loc_type=Shop)
|
||||
|
||||
|
||||
@command("POST")
|
||||
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)
|
||||
try:
|
||||
|
@ -96,7 +90,7 @@ def add_location(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None, loc_t
|
|||
return location
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def add_tunnel(tunnel_direction, tunnel_number, location_name=None, discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid, mc_uuid)
|
||||
if location_name is None:
|
||||
|
@ -108,7 +102,7 @@ def add_tunnel(tunnel_direction, tunnel_number, location_name=None, discord_uuid
|
|||
return tunnel
|
||||
|
||||
|
||||
@get
|
||||
@command("GET")
|
||||
def find_location(search):
|
||||
limit = 25
|
||||
|
||||
|
@ -120,13 +114,13 @@ def find_location(search):
|
|||
return locations
|
||||
|
||||
|
||||
@delete
|
||||
@command("DELETE")
|
||||
def delete(name, discord_uuid=None, mc_uuid=None):
|
||||
owner = get_player(discord_uuid, mc_uuid)
|
||||
Location.objects.get(name__iexact=name, owner=owner)
|
||||
|
||||
|
||||
@get
|
||||
@command("GET")
|
||||
def find_around(x_pos, z_pos, radius=200):
|
||||
locations = Location.objects.get(x_coord__range=(x_pos - radius, x_pos + radius),
|
||||
z_coord__range=(z_pos - radius, z_pos + radius), dimension='O')
|
||||
|
@ -134,7 +128,7 @@ def find_around(x_pos, z_pos, radius=200):
|
|||
return locations
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def add_item(item_name, quantity, diamond_price, shop_name=None, discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid, mc_uuid)
|
||||
|
||||
|
@ -146,7 +140,7 @@ def add_item(item_name, quantity, diamond_price, shop_name=None, discord_uuid=No
|
|||
|
||||
|
||||
# TODO Re-implement selling shop search
|
||||
@get
|
||||
@command("GET")
|
||||
def selling(item_name):
|
||||
items = []
|
||||
if len(item_name) == 0:
|
||||
|
@ -172,13 +166,13 @@ def selling(item_name):
|
|||
return items
|
||||
|
||||
|
||||
@get
|
||||
@command("GET")
|
||||
def info(location_name):
|
||||
loc = Location.objects.get(name__iexact=location_name)
|
||||
return loc
|
||||
return loc.json
|
||||
|
||||
|
||||
@get
|
||||
@command("GET")
|
||||
def tunnel(player_name):
|
||||
tunnels = Tunnel.objects.get(location__owner__name__icontains=player_name)
|
||||
|
||||
|
@ -188,7 +182,7 @@ def tunnel(player_name):
|
|||
return tunnel
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def edit_pos(x, z, loc_name, discord_uuid=None, mc_uuid=None):
|
||||
|
||||
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
||||
|
@ -201,7 +195,7 @@ def edit_pos(x, z, loc_name, discord_uuid=None, mc_uuid=None):
|
|||
return location
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def edit_tunnel(tunnel_direction, tunnel_number, loc_name, discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
||||
location = get_location(player, loc_name)
|
||||
|
@ -215,7 +209,7 @@ def edit_tunnel(tunnel_direction, tunnel_number, loc_name, discord_uuid=None, mc
|
|||
return location
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def edit_name(new_name, loc_name, discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
||||
location = get_location(player, loc_name)
|
||||
|
@ -226,7 +220,7 @@ def edit_name(new_name, loc_name, discord_uuid=None, mc_uuid=None):
|
|||
return location
|
||||
|
||||
|
||||
@post
|
||||
@command("POST")
|
||||
def delete_item(item, shop_name, discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
||||
|
||||
|
@ -237,7 +231,7 @@ def delete_item(item, shop_name, discord_uuid=None, mc_uuid=None):
|
|||
return shop
|
||||
|
||||
|
||||
@get
|
||||
@command("GET")
|
||||
def me(discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
||||
|
||||
|
|
44
api/views.py
44
api/views.py
|
@ -7,37 +7,13 @@ import GeoffreyApp.api.commands as commands
|
|||
from GeoffreyApp.errors import *
|
||||
|
||||
|
||||
def getRequiredArgs(func):
|
||||
args = inspect.getfullargspec(func)
|
||||
'''
|
||||
if defaults:
|
||||
args = args[:-len(kwonlyargs)]
|
||||
'''
|
||||
return args.args + args.kwonlyargs
|
||||
|
||||
|
||||
GET = []
|
||||
POST = []
|
||||
DELETE = []
|
||||
|
||||
for command in commands.get_list:
|
||||
GET.append({"name": command.__name__, "params": getRequiredArgs(command)})
|
||||
for command in commands.post_list:
|
||||
POST.append({"name": command.__name__, "params": getRequiredArgs(command)})
|
||||
for command in commands.delete_list:
|
||||
DELETE.append({"name": command.__name__, "params": getRequiredArgs(command)})
|
||||
command_response = {"GET": GET, "POST": POST, "DELETE": DELETE}
|
||||
|
||||
|
||||
def run_command(request, command, command_list):
|
||||
def run_command(request, command, req_type):
|
||||
command = command.lower()
|
||||
try:
|
||||
for c in command_list:
|
||||
if command == c.__name__:
|
||||
response = c(**request.dict())
|
||||
return JsonResponse(response, safe=False)
|
||||
|
||||
raise CommandNotFound
|
||||
if command in commands.command_dict[req_type]:
|
||||
response = commands.command_dict[req_type][command]["func"](**request.dict())
|
||||
else:
|
||||
raise CommandNotFound
|
||||
except Exception as e:
|
||||
response = {"error": e.__class__.__name__}
|
||||
|
||||
|
@ -47,18 +23,18 @@ def run_command(request, command, command_list):
|
|||
class CommandAPI(View):
|
||||
def get(self, request, command):
|
||||
get = request.GET
|
||||
if command.lower() != "commands":
|
||||
return run_command(get, command, commands.get_list)
|
||||
if command.lower() == "commands":
|
||||
return JsonResponse(command.commands_dict)
|
||||
else:
|
||||
return JsonResponse(command_response)
|
||||
return run_command(get, command, "GET")
|
||||
|
||||
def post(self, request, command):
|
||||
post = request.POST
|
||||
return run_command(post, command, commands.post_list)
|
||||
return run_command(post, command, "POST")
|
||||
|
||||
def delete(self, request, command):
|
||||
delete = request.DELETE
|
||||
return run_command(delete, command, commands.delete_list)
|
||||
return run_command(delete, command, "DELETE")
|
||||
|
||||
|
||||
class SettingsAPI(View):
|
||||
|
|
|
@ -35,7 +35,7 @@ class Location(models.Model):
|
|||
|
||||
@property
|
||||
def json(self):
|
||||
return {"Type": str(type(self)), "Name": self.name, "x_coord": self.x_coord, "z_coord": self.z_coord,
|
||||
return {"Type": self.__class__.__name__, "Name": self.name, "x_coord": self.x_coord, "z_coord": self.z_coord,
|
||||
"dimension": self.dimension, "Owner": self.owner.json}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
from django.test import TestCase
|
||||
from GeoffreyApp.api.commands import *
|
||||
from GeoffreyApp.models import *
|
||||
# Create your tests here.
|
||||
|
||||
MC_UUID = "fe7e84132570458892032b69ff188bc3"
|
||||
DISCORD_UUID = "143072699567177728"
|
||||
USERNAME = "ZeroHD"
|
||||
|
||||
|
||||
class CommandsAPITestCase(TestCase):
|
||||
def setUp(self):
|
||||
Player.objects.create(name=USERNAME, mc_uuid=MC_UUID, discord_uuid=DISCORD_UUID)
|
||||
|
||||
def test_register(self):
|
||||
command_dict["POST"]["register"]["func"](player_name="Vakky", discord_uuid="229423434256351233")
|
||||
|
||||
count = Player.objects.filter(mc_uuid__iexact="7afbf6632bf049ef915f22e81b298d17").count()
|
||||
|
||||
self.assertEqual(count, 1)
|
Loading…
Reference in New Issue