django_coreprotect/views.py

216 lines
7.4 KiB
Python

from django.shortcuts import render
from django.views.generic import View
from django.http.response import JsonResponse
from django_coreprotect.models import *
class Home(View):
def get(self, request):
form = form_data(request.GET)
return render(request, "coreprotect/coreprotect.html", {"form": form})
def post(self, request):
pass
class Query(View):
def get(self, request):
form = form_data(request.GET)
results = result_data(form)
if "format" in request.GET and request.GET["format"] == "json":
return JsonResponse(results)
return render(request, "coreprotect/table.html", {"results": results})
def post(self, request):
pass
class Form:
def __init__(self):
self.block_break = False
self.block_place = False
self.chat = False
self.chest_use = False
self.command = False
self.interact = False
self.login_logout = False
self.sign_place = False
self.worlds = []
self.ignore_environment = False
self.start = ""
self.limit_results = ""
self.players = ""
self.x = ""
self.y = ""
self.z = ""
self.radius = ""
self.blocks = ""
self.date_from = ""
self.date_to = ""
def form_data(request_data):
form = Form()
worlds = CoWorld.objects.all()
# Actions
form.block_break = checkbox(request_data["block_break"]) if "block_break" in request_data else False
form.block_place = checkbox(request_data["block_place"]) if "block_place" in request_data else False
form.chat = checkbox(request_data["chat"]) if "chat" in request_data else False
form.chest_use = checkbox(request_data["chest_use"]) if "chest_use" in request_data else False
form.command = checkbox(request_data["command"]) if "command" in request_data else False
form.interact = checkbox(request_data["interact"]) if "interact" in request_data else False
form.login_logout = checkbox(request_data["login_logout"]) if "login_logout" in request_data else False
form.sign_place = checkbox(request_data["sign_place"]) if "sign_place" in request_data else False
# Worlds
request_worlds = request_data.getlist("world")
for world in worlds:
world_id = "world_{}".format(world.id)
w = {
"id": str(world.id),
"world_id": world_id,
"checked": True if str(world.id) in request_worlds else False,
"name": world.world
}
form.worlds.append(w)
# Options
form.ignore_environment = checkbox(request_data["ignore_environment"]) if "ignore_environment" in request_data else False
# Limit Results
form.start = request_data["start"] if "start" in request_data else "0"
form.limit_results = request_data["limit_results"] if "limit_results" in request_data else "200"
# Players
form.players = request_data["players"] if "players" in request_data else ""
# Coordinates
form.x = request_data["x"] if "x" in request_data else ""
form.y = request_data["y"] if "y" in request_data else ""
form.z = request_data["z"] if "z" in request_data else ""
form.radius = request_data["radius"] if "radius" in request_data else ""
# Blocks
form.blocks = request_data["blocks"] if "blocks" in request_data else ""
# Date and Time
form.date_from = request_data["date_from"] if "date_from" in request_data else ""
form.date_to = request_data["date_to"] if "date_to" in request_data else ""
if form.date_from and not form.date_to:
form.date_to = datetime.now().timestamp()
if form.date_to and not form.date_from:
form.date_from = datetime.now().timestamp()
return form
def checkbox(value):
return True if value == "on" else False
def result_data(form):
queries = []
ignore_environment = " AND player NOT LIKE '#%' " if form.ignore_environment else ""
coords = []
radius = form.radius if form.radius else "0"
if form.x:
coords.append(" AND x BETWEEN {0} - {1} AND {0} + {1}".format(form.x, radius))
if form.y:
coords.append(" AND y BETWEEN {0} - {1} AND {0} + {1}".format(form.y, radius))
if form.z:
coords.append(" AND z BETWEEN {0} - {1} AND {0} + {1}".format(form.z, radius))
coords_clause = " AND ".join(coords)
players = []
players_clause = ""
if form.players:
for player in form.players.split(","):
players.append(player.strip())
players_clause = " AND ({})".format(" OR ".join(["player LIKE '%{}%'".format(p) for p in players]))
worlds_clause = ""
worlds = [world["id"] for world in form.worlds if world["checked"]]
if len(worlds):
worlds_clause = " AND cw.id IN ({})".format(",".join(worlds))
time_clause = ""
if form.date_from and form.date_to:
time_clause = " AND unix BETWEEN {} AND {} ".format(form.date_from, form.date_to)
# Block Break, Block Place, and Interact
block_actions = []
if form.block_break:
block_actions.append("0")
if form.block_place:
block_actions.append("1")
if form.interact:
block_actions.append("2")
if len(block_actions):
queries.append('''SELECT
0 AS id, "block" AS type, cb.time AS unix, cu.user AS player, cb.action, cmm.material AS data, cb.x, cb.y, cb.z, cw.world
FROM co_block cb
JOIN co_user cu ON cb.user = cu.id
JOIN co_material_map cmm ON cb.type = cmm.id
JOIN co_world cw ON cb.wid = cw.id
WHERE cb.action IN ({action})
{ignore_environment}
{coords}
{players}
{worlds}
{time}
'''.format(action=",".join(block_actions), ignore_environment=ignore_environment, coords=coords_clause,
players=players_clause, worlds=worlds_clause, time=time_clause))
# Chat
if form.chat:
queries.append('''SELECT
0 AS id, "chat" AS type, cc.time AS unix, cu.user AS player, "" AS action, cc.message AS data, "" AS x, "" AS y, "" AS z, "" AS world
FROM co_chat cc
JOIN co_user cu ON cch.user = cu.id
WHERE 1 = 1
{players}
{time}
'''.format(players=players_clause, time=time_clause))
# Commands
if form.command:
queries.append('''SELECT
0 AS id, "command" AS type, cc.time AS unix, cu.user AS player, "" AS action, cc.message AS data, "" AS x, "" AS y, "" AS z, "" AS world
FROM co_chat cc
JOIN co_user cu ON cco.user = cu.id
WHERE 1 = 1
{players}
{time}
'''.format(players=players_clause, time=time_clause))
# Chest Use
if form.chest_use:
queries.append('''SELECT
0 AS id, "container" AS type, cc.time AS unix, cu.user AS player, cc.action, cc.amount || " " || cmm.material AS data, cc.x, cc.y, cc.z, cw.world
FROM co_container cc
JOIN co_user cu ON cc.user = cu.id
JOIN co_material_map cmm ON cc.type = cmm.id
JOIN co_world cw ON cc.wid = cw.id
WHERE 1 = 1
{coords}
{players}
{worlds}
{time}
'''.format(coords=coords_clause,
players=players_clause, worlds=worlds_clause, time=time_clause))
query = " UNION ".join(queries)
if query and form.limit_results != "-1":
query += " LIMIT {}, {}".format(form.start, form.limit_results)
print(query)
if query:
return CoResult.objects.raw(query)
return []