From 02ab21aafcace3417be00b7b1e5f6b45a66bd347 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Thu, 13 Dec 2018 15:18:29 -0600 Subject: [PATCH] Migration of Warnings to Notes Notes to TicketNotes Fixes #21 --- admin.py | 16 +- api/views.py | 6 +- .../001-whitelist.sql} | 0 forms.py | 19 ++- migrations/0011_token.py | 32 ++++ migrations/0012_token_description.py | 18 +++ migrations/0013_warnings_to_notes.py | 48 ++++++ models.py | 112 ++++---------- overview.py | 4 +- .../{warning.html => note.html} | 22 +-- .../{warning_add.html => note_add.html} | 4 +- .../{warning_info.html => note_info.html} | 28 ++-- templates/minecraft_manager/overview.html | 2 +- templates/minecraft_manager/player_info.html | 18 +-- templates/minecraft_manager/ticket_info.html | 22 +-- templatetags/sidebar.py | 2 +- urls.py | 6 +- views.py | 146 +++++++++--------- 18 files changed, 278 insertions(+), 227 deletions(-) rename assets/{migrate.sql => migrations/001-whitelist.sql} (100%) create mode 100644 migrations/0011_token.py create mode 100644 migrations/0012_token_description.py create mode 100644 migrations/0013_warnings_to_notes.py rename templates/minecraft_manager/{warning.html => note.html} (55%) rename templates/minecraft_manager/{warning_add.html => note_add.html} (90%) rename templates/minecraft_manager/{warning_info.html => note_info.html} (52%) diff --git a/admin.py b/admin.py index e01ee22..6cc4532 100644 --- a/admin.py +++ b/admin.py @@ -4,7 +4,7 @@ from django.contrib import admin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ -from minecraft_manager.models import Application, Warning, Ticket, Player, IP, UserSettings, Alert, Note +from minecraft_manager.models import Application, Note, Ticket, TicketNote, Player, IP, UserSettings, Alert from minecraft_manager.api.admin import register as api_register @@ -61,9 +61,9 @@ class TicketAdmin(admin.ModelAdmin): list_filter = (TicketPriorityFilter,) -class WarningSeverityFilter(admin.SimpleListFilter): - title = _('Severity') - parameter_name = 'severity' +class NoteImportanceFilter(admin.SimpleListFilter): + title = _('Importance') + parameter_name = 'importance' def lookups(self, request, model_admin): return ( @@ -81,9 +81,9 @@ class WarningSeverityFilter(admin.SimpleListFilter): return queryset.filter(priority='H') -class WarningAdmin(admin.ModelAdmin): +class NoteAdmin(admin.ModelAdmin): search_fields = ["player__username", "staff__username"] - list_filter = (WarningSeverityFilter,) + list_filter = (NoteImportanceFilter,) class IPAdmin(admin.ModelAdmin): @@ -95,12 +95,12 @@ try: admin.site.register(User, UserAdmin) admin.site.register(UserSettings) admin.site.register(Application, ApplicationAdmin) - admin.site.register(Warning, WarningAdmin) admin.site.register(Ticket, TicketAdmin) + admin.site.register(TicketNote) + admin.site.register(Note, NoteAdmin) admin.site.register(Player, PlayerAdmin) admin.site.register(IP, IPAdmin) admin.site.register(Alert) - admin.site.register(Note) api_register() except admin.sites.AlreadyRegistered: pass diff --git a/api/views.py b/api/views.py index 59c1657..5b702e4 100644 --- a/api/views.py +++ b/api/views.py @@ -12,7 +12,7 @@ from django.views.generic import View from django.forms import modelform_factory import minecraft_manager.forms as mcm_forms -from minecraft_manager.models import Player, UserSettings, Application, IP, Ticket, Warning +from minecraft_manager.models import Player, UserSettings, Application, IP, Ticket, Note import minecraft_manager.api.api as mcm_api from minecraft_manager.api.models import Token import minecraft_manager.utils as mcm_utils @@ -306,10 +306,10 @@ class PluginAPI(View): player = Player.objects.get(uuid=post['player']) staff = Player.objects.get(uuid=post['staff']) try: - warning = Warning(player=player, message=post['message'], severity=post['severity'], staff=staff.auth_user) + warning = Note(player=player, message=post['message'], importance=post['severity'], staff=staff.auth_user) warning.save() json['message'] = "Warning issued." - link = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard/warning', warning.id)) + link = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard/note', warning.id)) msg = mcm_utils.build_warning(warning, link) mcm_api.discord_mcm(embeds=msg) except Exception as ex: diff --git a/assets/migrate.sql b/assets/migrations/001-whitelist.sql similarity index 100% rename from assets/migrate.sql rename to assets/migrations/001-whitelist.sql diff --git a/forms.py b/forms.py index b9003e4..d0a37b3 100644 --- a/forms.py +++ b/forms.py @@ -1,9 +1,9 @@ from django.forms import ModelForm, Textarea, HiddenInput, TextInput -from minecraft_manager.models import UserSettings, Application, Alert, Ticket, Warning, Note +from minecraft_manager.models import UserSettings, Application, Alert, Ticket, TicketNote, Note def __all__(): - return [UserSettingsForm, ApplicationForm, AlertForm, TicketForm, WarningForm, NoteForm] + return [UserSettingsForm, ApplicationForm, AlertForm, TicketForm, TicketNoteForm, NoteForm] class UserSettingsForm(ModelForm): @@ -34,20 +34,19 @@ class TicketForm(ModelForm): } -class WarningForm(ModelForm): +class NoteForm(ModelForm): class Meta: - model = Warning - fields = ['player', 'message', 'severity'] + model = Note + fields = ['player', 'message', 'importance'] widgets = { 'message': Textarea } -class NoteForm(ModelForm): +class TicketNoteForm(ModelForm): class Meta: - model = Note - fields = ['ref_id', 'ref_table', 'message'] + model = TicketNote + fields = ['ticket', 'message'] widgets = { - 'ref_id': HiddenInput, - 'ref_table': HiddenInput + 'ticket': HiddenInput } diff --git a/migrations/0011_token.py b/migrations/0011_token.py new file mode 100644 index 0000000..8e28c8c --- /dev/null +++ b/migrations/0011_token.py @@ -0,0 +1,32 @@ +# Generated by Django 2.0.7 on 2018-11-23 15:20 + +from django.db import migrations, models +import minecraft_manager.api.api + + +class Migration(migrations.Migration): + + dependencies = [ + ('minecraft_manager', '0010_auto_20180921_2306'), + ] + + operations = [ + migrations.CreateModel( + name='Token', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('key', models.CharField(default=minecraft_manager.api.api.generate_password, max_length=50, unique=True, verbose_name='Key')), + ('active', models.BooleanField(default=True, verbose_name='Active')), + ('web_get_permission', models.BooleanField(default=False, verbose_name='Web GET')), + ('web_post_permission', models.BooleanField(default=False, verbose_name='Web POST')), + ('plugin_get_permission', models.BooleanField(default=False, verbose_name='Plugin GET')), + ('plugin_post_permission', models.BooleanField(default=False, verbose_name='Plugin POST')), + ('form_get_permission', models.BooleanField(default=False, verbose_name='Form GET')), + ('form_post_permission', models.BooleanField(default=False, verbose_name='Form POST')), + ('model_get_permission', models.BooleanField(default=False, verbose_name='Model GET')), + ('model_post_permission', models.BooleanField(default=False, verbose_name='Model POST')), + ('stats_get_permission', models.BooleanField(default=False, verbose_name='Stats GET')), + ('stats_post_permission', models.BooleanField(default=False, verbose_name='Stats POST')), + ], + ), + ] diff --git a/migrations/0012_token_description.py b/migrations/0012_token_description.py new file mode 100644 index 0000000..0752851 --- /dev/null +++ b/migrations/0012_token_description.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.7 on 2018-11-23 15:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('minecraft_manager', '0011_token'), + ] + + operations = [ + migrations.AddField( + model_name='token', + name='description', + field=models.CharField(blank=True, max_length=200, verbose_name='Description'), + ), + ] diff --git a/migrations/0013_warnings_to_notes.py b/migrations/0013_warnings_to_notes.py new file mode 100644 index 0000000..74a8397 --- /dev/null +++ b/migrations/0013_warnings_to_notes.py @@ -0,0 +1,48 @@ +# Generated by Django 2.0.5 on 2018-12-13 20:20 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +def migrate_notes(apps, schema_editor): + Note = apps.get_model('minecraft_manager', 'Note') + TicketNote = apps.get_model('minecraft_manager', 'TicketNote') + for note in Note.objects.all(): + ticket_note = TicketNote(author=note.author, ticket_id=note.ref_id, message=note.message, last_update=note.last_update, date=note.date) + ticket_note.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('minecraft_manager', '0012_token_description'), + ] + + operations = [ + migrations.CreateModel( + name='TicketNote', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('message', models.TextField(max_length=1000)), + ('last_update', models.DateTimeField(auto_now_add=True, null=True)), + ('date', models.DateTimeField(auto_now_add=True, null=True)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ('ticket', + models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='minecraft_manager.Ticket')), + ], + ), + migrations.RunPython(migrate_notes), + migrations.DeleteModel( + name='Note', + ), + migrations.RenameModel( + old_name='Warning', + new_name='Note' + ), + migrations.RenameField( + model_name='Note', + old_name='severity', + new_name='importance' + ) + ] diff --git a/models.py b/models.py index 27e8bb1..ea324de 100644 --- a/models.py +++ b/models.py @@ -52,7 +52,7 @@ class UserSettings(models.Model): verbose_name_plural = "User Settings" def __str__(self): - return "User Settings : %s" % self.auth_user.username + return self.auth_user.username def __repr__(self): return self.auth_user.username @@ -86,22 +86,8 @@ class Application(models.Model): def date_display(self): return str(self.date).split(".")[0] - @property - def json(self): - return {'id': self.id, 'username': self.username, 'age': self.age, 'player_type': self.player_type, - 'ever_banned': self.ever_banned, 'ever_banned_explanation': self.ever_banned_explanation, - 'reference': self.reference, 'read_rules': self.read_rules, 'status': self.status, - 'date': self.date_display} - - @property - def skript(self): - return "{0}<:>{1}<:>{2}<:>{3}<:>{4}<:>{5}<:>{6}<:>{7}<:>{8}<:>{9}<:>{10}".format( - "success", self.id, self.username, self.age, self.player_type, self.ever_banned, - self.ever_banned_explanation, self.reference, self.read_rules, self.status, self.date_display - ) - def __str__(self): - return "Application: %s" % self.username + return self.username def __repr__(self): return self.username @@ -177,7 +163,7 @@ class Player(models.Model): return " ".join(ips) def __str__(self): - return self.username + " (" + self.uuid + ")" + return "{} ({})".format(self.username, self.uuid) class Ticket(models.Model): @@ -270,25 +256,34 @@ class Ticket(models.Model): return str(self.date).split(".")[0] def __str__(self): - username = "Unknown" - try: - pl = Player.objects.get(id=self.player_id) - username = pl.username - except: - pass - return "Ticket from %s" % username + return "{}: {}".format(self.issuer, self.snippet) -class Warning(models.Model): - SEVERITY = ( +class TicketNote(models.Model): + author = models.ForeignKey(User, on_delete=models.CASCADE) + ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE) + message = models.TextField(max_length=1000) + last_update = models.DateTimeField(auto_now_add=True, blank=True, null=True) + date = models.DateTimeField(auto_now_add=True, blank=True, null=True) + + class Meta: + verbose_name = "Ticket Note" + verbose_name_plural = "Ticket Notes" + + def __str__(self): + return "Ticket: {0}".format(self.ticket.snippet) + + +class Note(models.Model): + IMPORTANCE = ( ('L', 'Low'), ('M', 'Medium'), ('H', 'High') ) - player = models.ForeignKey(Player, related_name="warning_player", on_delete=models.CASCADE) + player = models.ForeignKey(Player, related_name="note_player", on_delete=models.CASCADE) message = models.CharField(max_length=200) - severity = models.CharField(max_length=1, choices=SEVERITY) - staff = models.ForeignKey(User, related_name="warning_staff", on_delete=models.CASCADE, null=True, blank=True, limit_choices_to=Q(is_active=True)) + importance = models.CharField(max_length=1, choices=IMPORTANCE) + staff = models.ForeignKey(User, related_name="note_staff", on_delete=models.CASCADE, null=True, blank=True, limit_choices_to=Q(is_active=True)) date = models.DateTimeField(auto_now_add=True, blank=True, null=True) @property @@ -299,26 +294,19 @@ class Warning(models.Model): return self.message @property - def severity_display(self): - for S in self.SEVERITY: - if S[0] == self.severity: + def importance_display(self): + for S in self.IMPORTANCE: + if S[0] == self.importance: return S[1] return "Unknown" @staticmethod - def severity_code_to_display(severity_code): - for S in Warning.SEVERITY: - if S[0] == severity_code: + def importance_code_to_display(importance_code): + for S in Note.IMPORTANCE: + if S[0] == importance_code: return S[1] return "Unknown" - @staticmethod - def resolve(severity): - for S in Warning.SEVERITY: - if severity.lower() in (S[0].lower(), S[1].lower()): - return S[0] - return 'L' - @property def issuer(self): if self.staff: @@ -340,13 +328,7 @@ class Warning(models.Model): return str(self.date).split(".")[0] def __str__(self): - username = "Unknown" - try: - pl = Player.objects.get(id=self.player_id) - username = pl.username - except: - pass - return "Warning for %s" % username + return "{}: {}".format(self.issuee, self.snippet) class IPManager(models.Manager): @@ -393,7 +375,7 @@ class IP(models.Model): def __str__(self): player = Player.objects.get(id=self.player_id) - return player.username + " - " + self.ip + return "{}: {}".format(player.username, self.ip) class Alert(models.Model): @@ -410,34 +392,8 @@ class Alert(models.Model): return self.message def __str__(self): - return "Alert for %s" % self.user.username - - -class Note(models.Model): - NOTABLE = ( - ('PL', 'Player'), - ('TI', 'Ticket'), - ('WA', 'Warning') - ) - author = models.ForeignKey(User, on_delete=models.CASCADE) - ref_table = models.CharField(max_length=2, choices=NOTABLE) - ref_id = models.CharField(max_length=4) - message = models.TextField(max_length=1000) - last_update = models.DateTimeField(auto_now_add=True, blank=True, null=True) - date = models.DateTimeField(auto_now_add=True, blank=True, null=True) - - @property - def ref_model(self): - if self.ref_table == 'PL': - return Player - elif self.ref_table == 'TI': - return Ticket - elif self.ref_table == 'WA': - return Warning - - def __str__(self): - ref = self.ref_model.objects.get(id=self.ref_id) - return "Note: {0}".format(ref) + return "Alert for {}".format(self.user.username) + diff --git a/overview.py b/overview.py index 3508606..4f1ffe4 100644 --- a/overview.py +++ b/overview.py @@ -2,7 +2,7 @@ import os import json from datetime import datetime, timedelta from django.conf import settings -from minecraft_manager.models import Application, Player, Ticket, Warning, IP +from minecraft_manager.models import Application, Player, Ticket, Note, IP from django.contrib.auth.models import User @@ -33,7 +33,7 @@ def overview_data(): 'unresolved': Ticket.objects.filter(resolved=False).count(), 'all': Ticket.objects.count() }, - 'warning': Warning.objects.count(), + 'note': Note.objects.count(), 'ip': IP.objects.count() } diff --git a/templates/minecraft_manager/warning.html b/templates/minecraft_manager/note.html similarity index 55% rename from templates/minecraft_manager/warning.html rename to templates/minecraft_manager/note.html index 4ee299f..fd350cd 100644 --- a/templates/minecraft_manager/warning.html +++ b/templates/minecraft_manager/note.html @@ -1,5 +1,5 @@ {% extends "minecraft_manager/dashboard.html" %} -{% block title %}Warnings{% endblock %} +{% block title %}Notes{% endblock %} {% block section %}