diff --git a/admin.py b/admin.py index 6cdea8d..dd42891 100644 --- a/admin.py +++ b/admin.py @@ -1,8 +1,53 @@ from django.contrib import admin +from django.contrib.admin import widgets as admin_widgets +from django import forms +import ast +from django_coreprotect.utils import time_to_form, form_to_time from django_coreprotect.models import CoArtMap, CoBlock, CoBlockdataMap, CoChat, CoCommand, CoContainer, CoDatabaseLock, \ CoEntity, CoEntityMap, CoMaterialMap, CoSession, CoSign, CoSkull, CoUser, CoUsernameLog, CoVersion, CoWorld, Preset + +class PresetAdminForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super(PresetAdminForm, self).__init__(*args, **kwargs) + + worlds = tuple([(world.id, world.world) for world in CoWorld.objects.all()]) + self.fields['worlds'].widget = forms.SelectMultiple(choices=worlds) + self.fields['date_from'].widget = admin_widgets.AdminSplitDateTime() + self.fields['date_to'].widget = admin_widgets.AdminSplitDateTime() + + if self.instance: + if "worlds" in self.initial and self.initial["worlds"]: + self.initial["worlds"] = tuple(self.initial["worlds"]) + if "date_from" in self.initial and self.initial["date_from"]: + self.initial["date_from"] = time_to_form(self.initial["date_from"]) + if "date_to" in self.initial and self.initial["date_to"]: + self.initial["date_to"] = time_to_form(self.initial["date_to"]) + + def clean(self): + cleaned = super(PresetAdminForm, self).clean() + worlds = cleaned.get("worlds", "") + if worlds: + worlds = ast.literal_eval(worlds) + cleaned.update({"worlds": ",".join(worlds)}) + date_from = cleaned.get("date_from", "") + if date_from: + cleaned.update({"date_from": form_to_time(date_from)}) + date_to = cleaned.get("date_to", "") + if date_to: + cleaned.update({"date_to": form_to_time(date_to)}) + return cleaned + + class Meta: + model = Preset + fields = "__all__" + + +class PresetAdmin(admin.ModelAdmin): + form = PresetAdminForm + + try: pass # admin.site.register(CoArtMap) @@ -22,6 +67,6 @@ try: # admin.site.register(CoUsernameLog) # admin.site.register(CoVersion) # admin.site.register(CoWorld) - admin.site.register(Preset) + admin.site.register(Preset, PresetAdmin) except admin.sites.AlreadyRegistered: pass diff --git a/gui.py b/gui.py index c17178e..0ef5f1c 100644 --- a/gui.py +++ b/gui.py @@ -1,5 +1,5 @@ from django_coreprotect.models import CoWorld, GUIResult -from django_coreprotect.utils import safe_int, checkbox +from django_coreprotect.utils import safe_int, checkbox, time_to_form from datetime import datetime @@ -86,8 +86,12 @@ def gui_data(request): 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 "" + form.date_from = time_to_form(request_data["date_from"]) if "date_from" in request_data else "" + if form.date_from: + form.date_from = form.date_from.timestamp() + form.date_to = time_to_form(request_data["date_to"]) if "date_to" in request_data else "" + if form.date_to: + form.date_to = form.date_to.timestamp() return form @@ -116,10 +120,14 @@ def gui_results(form): blocks = [] blocks_clause = "" + chat_clause = "" + command_clause = "" if form.blocks: for block in form.blocks.split(","): blocks.append(block.strip()) blocks_clause = " AND ({})".format(" OR ".join(["cmm.material LIKE 'minecraft:{}%%'".format(b) for b in blocks])) + chat_clause = " AND ({})".format(" OR ".join(["cc.message LIKE '%%{}%%'".format(b) for b in blocks])) + command_clause = " AND ({})".format(" OR ".join(["cc.message LIKE '%%{}%%'".format(b) for b in blocks])) worlds_clause = "" worlds = [world["id"] for world in form.worlds if world["checked"]] @@ -133,7 +141,7 @@ def gui_results(form): dt = datetime.now().timestamp() if form.date_to and not form.date_from: df = datetime.now().timestamp() - time_clause = " HAVING unix BETWEEN {} AND {} ".format(df, dt) + time_clause = " GROUP BY unix HAVING unix BETWEEN {} AND {} ".format(df, dt) # Block Break, Block Place, and Interact block_actions = [] @@ -168,8 +176,9 @@ def gui_results(form): JOIN co_user cu ON cc.user = cu.rowid WHERE 1 = 1 {players} + {chat} {time} - '''.format(players=players_clause, time=time_clause)) + '''.format(players=players_clause, chat=chat_clause, time=time_clause)) # Chest Use if form.chest_use: @@ -195,9 +204,10 @@ def gui_results(form): JOIN co_user cu ON cc.user = cu.rowid WHERE 1 = 1 {players} + {command} {coords} {time} - '''.format(players=players_clause, coords=coords_clause, time=time_clause)) + '''.format(players=players_clause, command=command_clause, coords=coords_clause, time=time_clause)) # Login/Logout if form.login_logout: diff --git a/migrations/0002_preset.py b/migrations/0002_preset.py index 8dd2cdc..fe15e02 100644 --- a/migrations/0002_preset.py +++ b/migrations/0002_preset.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.3 on 2019-09-30 21:59 +# Generated by Django 2.2.3 on 2019-10-01 04:28 from django.db import migrations, models @@ -23,17 +23,17 @@ class Migration(migrations.Migration): ('interact', models.BooleanField(default=False)), ('login_logout', models.BooleanField(default=False)), ('sign_place', models.BooleanField(default=False)), - ('worlds', models.CharField(default='', max_length=20)), + ('worlds', models.CharField(blank=True, default='', max_length=20)), ('ignore_environment', models.BooleanField(default=False)), ('oldest_first', models.BooleanField(default=False)), - ('players', models.CharField(max_length=50)), - ('x', models.CharField(default='', max_length=10)), - ('y', models.CharField(default='', max_length=10)), - ('z', models.CharField(default='', max_length=10)), - ('radius', models.CharField(default='', max_length=10)), - ('blocks', models.CharField(default='', max_length=50)), - ('date_from', models.CharField(default='', max_length=10)), - ('date_to', models.CharField(default='', max_length=10)), + ('players', models.CharField(blank=True, max_length=50)), + ('x', models.CharField(blank=True, default='', max_length=10)), + ('y', models.CharField(blank=True, default='', max_length=10)), + ('z', models.CharField(blank=True, default='', max_length=10)), + ('radius', models.CharField(blank=True, default='', max_length=10)), + ('blocks', models.CharField(blank=True, default='', max_length=50)), + ('date_from', models.CharField(blank=True, default='', max_length=50)), + ('date_to', models.CharField(blank=True, default='', max_length=50)), ('enabled', models.BooleanField(default=True)), ], ), diff --git a/models.py b/models.py index 0702fdf..f611f71 100644 --- a/models.py +++ b/models.py @@ -312,8 +312,8 @@ class Preset(models.Model): z = models.CharField(max_length=10, default="", blank=True) radius = models.CharField(max_length=10, default="", blank=True) blocks = models.CharField(max_length=50, default="", blank=True) - date_from = models.CharField(max_length=10, default="", blank=True) - date_to = models.CharField(max_length=10, default="", blank=True) + date_from = models.CharField(max_length=50, default="", blank=True) + date_to = models.CharField(max_length=50, default="", blank=True) enabled = models.BooleanField(default=True) @property @@ -337,7 +337,8 @@ class Preset(models.Model): if self.sign_place: params.append("sign_place=on") if self.worlds: - params.append("world=" + self.worlds) + for world in self.worlds.split(","): + params.append("world=" + world) if self.ignore_environment: params.append("ignore_environment=on") if self.oldest_first: @@ -360,6 +361,9 @@ class Preset(models.Model): params.append("date_to=" + self.date_to) return url + "&".join(params) + def __str__(self): + return self.name + class GUIResult(models.Model): type = models.TextField() diff --git a/templates/coreprotect/coreprotect.html b/templates/coreprotect/coreprotect.html index 3d1326d..f40b546 100644 --- a/templates/coreprotect/coreprotect.html +++ b/templates/coreprotect/coreprotect.html @@ -75,30 +75,25 @@ -
+ {% if presets %} +
-

- Page Size -

-
+

+ Presets +

+

- + + {% for preset in presets %} + + {% endfor %}

-
- - - {% for preset in presets %} - {{ preset.name }} - {% endfor %} +
+ {% endif %} @@ -159,6 +154,27 @@
+

+ Page Size +

+
+

+ + + +

+
+ + +
+

@@ -282,5 +298,13 @@ document.getElementById("export").addEventListener("click", function() { exportTableToCSV("coreprotect.csv", "results"); }); + + {% if presets %} + document.getElementById("preset").addEventListener("change", function() { + if (this.value !== "") { + location.href = this.value; + } + }); + {% endif %} {% endblock %} \ No newline at end of file diff --git a/utils.py b/utils.py index 3dab566..77762c6 100644 --- a/utils.py +++ b/utils.py @@ -1,3 +1,6 @@ +from datetime import datetime, timedelta +import ast + def checkbox(value): return True if value == "on" else False @@ -6,4 +9,27 @@ def safe_int(value): try: return int(value) except: - return 0 \ No newline at end of file + return 0 + + +def time_to_form(date_time): + if date_time == "~T": + return datetime.today() + elif date_time == "~N": + return datetime.now() + elif date_time == "~Y": + return datetime.today() - timedelta(days=1) + elif date_time == "": + return "" + else: + return datetime.utcfromtimestamp(float(date_time)) + + +def form_to_time(date_time): + dt = ast.literal_eval(date_time) + if dt[0] in ["~T", "~N", "~Y"]: + return dt[0] + else: + dt[0] = dt[0] if dt[0] else datetime.now().strftime("%Y-%m-%d") + dt[1] = dt[1] if dt[1] else "00:00:00" + return datetime.strptime(" ".join(dt), "%Y-%m-%d %H:%M:%S").timestamp()