2018-11-22 02:09:30 +00:00
|
|
|
from django.views.generic import View
|
|
|
|
from django.http import JsonResponse
|
2018-12-02 02:17:19 +00:00
|
|
|
from django.conf import settings
|
2018-11-22 02:09:30 +00:00
|
|
|
|
|
|
|
import GeoffreyApp.api.commands as commands
|
2018-12-17 16:02:43 +00:00
|
|
|
from GeoffreyApp.errors import *
|
2018-12-30 01:41:18 +00:00
|
|
|
from GeoffreyApp.models import APIToken
|
|
|
|
|
|
|
|
|
|
|
|
def check_token(request, **kwargs):
|
|
|
|
if "api" in request:
|
|
|
|
key = request["api"]
|
|
|
|
|
|
|
|
if APIToken.objects.filter(key=key, **kwargs).exists():
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2018-11-22 02:09:30 +00:00
|
|
|
|
|
|
|
|
2018-12-17 21:16:21 +00:00
|
|
|
def run_command(request, command, req_type):
|
2018-11-22 02:09:30 +00:00
|
|
|
command = command.lower()
|
|
|
|
try:
|
2018-12-17 21:16:21 +00:00
|
|
|
if command in commands.command_dict[req_type]:
|
2018-12-30 01:41:18 +00:00
|
|
|
params = request.dict()
|
|
|
|
params.pop("api")
|
|
|
|
response = commands.command_dict[req_type][command]["func"](**params)
|
2018-12-17 21:16:21 +00:00
|
|
|
else:
|
|
|
|
raise CommandNotFound
|
2018-11-22 02:09:30 +00:00
|
|
|
except Exception as e:
|
2018-12-30 01:41:18 +00:00
|
|
|
response = {"error": e.__class__.__name__, "error_message": str(e)}
|
2018-11-22 02:09:30 +00:00
|
|
|
|
|
|
|
return JsonResponse(response, safe=False)
|
|
|
|
|
|
|
|
|
|
|
|
class CommandAPI(View):
|
|
|
|
def get(self, request, command):
|
|
|
|
get = request.GET
|
2018-12-30 01:41:18 +00:00
|
|
|
|
|
|
|
if check_token(get, commands_perm=True):
|
|
|
|
if command.lower() == "commands":
|
2019-01-01 16:18:15 +00:00
|
|
|
return JsonResponse(commands.command_dict)
|
2018-12-30 01:41:18 +00:00
|
|
|
else:
|
|
|
|
return run_command(get, command, "GET")
|
2018-12-08 21:37:00 +00:00
|
|
|
else:
|
2018-12-30 01:41:18 +00:00
|
|
|
return JsonResponse({})
|
2018-11-22 02:09:30 +00:00
|
|
|
|
|
|
|
def post(self, request, command):
|
|
|
|
post = request.POST
|
2018-12-30 01:41:18 +00:00
|
|
|
if check_token(post, commands_perm=True):
|
|
|
|
return run_command(post, command, "POST")
|
|
|
|
else:
|
|
|
|
return JsonResponse({})
|
2018-11-22 02:09:30 +00:00
|
|
|
|
|
|
|
def delete(self, request, command):
|
|
|
|
delete = request.DELETE
|
2018-12-30 01:41:18 +00:00
|
|
|
if check_token(delete, commands_perm=True):
|
|
|
|
return run_command(delete, command, "DELETE")
|
|
|
|
else:
|
|
|
|
return JsonResponse({})
|
2018-12-02 02:17:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SettingsAPI(View):
|
2018-12-08 21:37:00 +00:00
|
|
|
def get(self, request):
|
2019-01-01 16:18:15 +00:00
|
|
|
if check_token(request, command_perm=True):
|
|
|
|
response = {
|
|
|
|
"BOT_PREFIX": getattr(settings, 'GEOFFREY_BOT_PREFIX', '?'),
|
|
|
|
"ERROR_USERS": getattr(settings, 'GEOFFREY__BOT_ERROR_USERS', []),
|
|
|
|
"MOD_RANKS": getattr(settings, 'GEOFFREY_BOT_MOD_RANK', []),
|
|
|
|
"STATUS": getattr(settings, 'GEOFFREY_BOT_STATUS', 'sed')
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
response = {}
|
2018-12-02 02:17:19 +00:00
|
|
|
|
|
|
|
return JsonResponse(response)
|
|
|
|
|
|
|
|
|
|
|
|
|