Geoffrey-Django/api/views.py

53 lines
1.5 KiB
Python
Raw Normal View History

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-12-08 21:37:00 +00:00
import inspect
import GeoffreyApp.api.commands as commands
2018-12-17 16:02:43 +00:00
from GeoffreyApp.errors import *
def run_command(request, command, req_type):
command = command.lower()
try:
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:
2018-12-08 21:37:00 +00:00
response = {"error": e.__class__.__name__}
return JsonResponse(response, safe=False)
class CommandAPI(View):
def get(self, request, command):
get = request.GET
if command.lower() == "commands":
return JsonResponse(command.commands_dict)
2018-12-08 21:37:00 +00:00
else:
return run_command(get, command, "GET")
def post(self, request, command):
post = request.POST
return run_command(post, command, "POST")
def delete(self, request, command):
delete = request.DELETE
return run_command(delete, command, "DELETE")
2018-12-02 02:17:19 +00:00
class SettingsAPI(View):
2018-12-08 21:37:00 +00:00
def get(self, request):
2018-12-02 02:17:19 +00:00
response = {
"BOT_PREFIX": getattr(settings, 'BOT_PREFIX', '?'),
"ERROR_USERS": getattr(settings, 'ERROR_USERS', []),
"MOD_RANK": getattr(settings, 'MOD_RANK', []),
"DEFAULT_STATUS": getattr(settings, 'DEFAULT_STATUS', 'sed')
}
return JsonResponse(response)