diff --git a/api/views.py b/api/views.py index fdba1eb..d1798b5 100644 --- a/api/views.py +++ b/api/views.py @@ -1,23 +1,22 @@ from __future__ import absolute_import -import datetime -import logging - -from django.apps import apps -from django.contrib.auth import update_session_auth_hash +import logging, datetime from django.contrib.auth.forms import PasswordChangeForm +from django.contrib.auth import update_session_auth_hash +from django.apps import apps +from django.conf import settings from django.contrib.auth.models import User -from django.forms import modelform_factory from django.http import JsonResponse, HttpResponse from django.utils import timezone from django.views.generic import View +from django.forms import modelform_factory -import minecraft_manager.api.api as mcm_api -import minecraft_manager.external.stats as mcm_stats import minecraft_manager.forms as mcm_forms -import minecraft_manager.utils as mcm_utils -from minecraft_manager.api.models import Token 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 +import minecraft_manager.external.stats as mcm_stats logger = logging.getLogger(__name__) @@ -206,11 +205,10 @@ class PluginAPI(View): application.save() json['message'] = "Application was successfully {0}.".format( "accepted" if post['action'] == "True" else "denied") - link = mcm_utils.full_reverse('application_info', application.id) - mcm_api.discord_mcm("{0}'s application ([#{1}]({4})) was {2} by {3}".format(application.username, + mcm_api.discord_mcm("{0}'s application (#{1}) was {2} by {3}".format(application.username, application.id, "accepted" if post['action'] == "True" else "denied", - post['username'], link)) + post['username'])) mcm_api.plugin("accept" if post['action'] == "True" else "deny", application.username) else: json['status'] = False @@ -299,7 +297,7 @@ class PluginAPI(View): ticket = Ticket(player=player, message=post['message'], x=post['x'], y=post['y'], z=post['z'], world=post['world']) ticket.save() json['message'] = "Ticket submitted." - link = mcm_utils.full_reverse('ticket_info', ticket.id) + link = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard/ticket', ticket.id)) msg = mcm_utils.build_ticket(ticket, link) json['extra'] = {'id': ticket.id, 'link': link} mcm_api.discord_mcm(embed=msg, ping=True) @@ -313,7 +311,7 @@ class PluginAPI(View): warning = Note(player=player, message=post['message'], importance=post['severity'], staff=staff.auth_user) warning.save() json['message'] = "Warning issued." - link = mcm_utils.full_reverse('note_info', 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(embed=msg) except Exception as ex: @@ -392,7 +390,7 @@ class ModelAPI(View): json = [] for value in objects: try: - link = mcm_utils.full_reverse(f"{request_model}_info", value['id']) + link = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard', request_model, value['id'])) value['link'] = link except: pass diff --git a/apps.py b/apps.py index cc3695e..a5fc31c 100644 --- a/apps.py +++ b/apps.py @@ -1,7 +1,12 @@ from django.apps import AppConfig +from django.db.models.signals import pre_delete +from minecraft_manager.signals.pre_delete import attachment_delete class MinecraftManagerAppConfig(AppConfig): name = 'minecraft_manager' verbose_name = "Minecraft Manager" + def ready(self): + pre_delete.connect(attachment_delete) + diff --git a/bot/commands.py b/bot/commands.py index 693d496..dd7c2ec 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -1,13 +1,12 @@ import discord -from io import StringIO from discord.ext import commands from django.contrib.auth.models import User from django.db import close_old_connections from minecraft_manager.api import api from minecraft_manager.bot.utils import get_application +from minecraft_manager.utils import build_application from minecraft_manager.models import Application, Player -from minecraft_manager.utils import build_application, full_static class Commands(commands.Cog): @@ -45,8 +44,8 @@ class Commands(commands.Cog): async def help(self, ctx): embed = discord.Embed(colour=discord.Colour(0x417505)) embed.set_thumbnail( - url=full_static('favicon.png')) - embed.title = "Minecraft Manager Help" + url="https://cdn.discordapp.com/avatars/454457830918062081/b5792489bc43d9e17b8f657880a17dd4.png") + embed.add_field(name="Minecraft Manager Help", value="-----------------------------") embed.add_field(name="{}app search ".format(self.bot.prefix), value="Search for applications by partial or exact username.") embed.add_field(name="{}app info ".format(self.bot.prefix), @@ -55,7 +54,6 @@ class Commands(commands.Cog): embed.add_field(name="{}demote ".format(self.bot.prefix), value="Demote a player to the role given to accepted applications.") embed.add_field(name="{}compare".format(self.bot.prefix), value="Compare Discord users to the Whitelist.") - embed.add_field(name="{}sync".format(self.bot.prefix), value="Sync Discord users with Player records.") await self.bot.discord_message(ctx.message.channel, embed) @commands.group("app", aliases=["application"]) @@ -260,31 +258,6 @@ class Commands(commands.Cog): header = "**The following users do not have an application or player match on the whitelist:**\n" await self.bot.discord_message(ctx.author, "{}```{}```".format(header, "\n".join(no_application))) - @commands.command() - async def sync(self, ctx): - def sync_player(player): - for member in ctx.guild.members: - name = member.nick if member.nick else member.name - if player.username.lower() == name.lower(): - player.discord_id = member.id - player.save() - return True - return False - # Attempt to sync users - need_sync = Player.objects.filter(discord_id__isnull=True) - need_sync_count = need_sync.count() - synced = [] - not_synced = [] - if need_sync_count > 0: - for ns in need_sync: - if sync_player(ns): - synced.append(ns.username) - else: - not_synced.append(ns.username) - txt = "Synced\n\t{}\n\nNot Synced\n\t{}".format('\n\t'.join(synced), '\n\t'.join(not_synced)) - attach = discord.File(fp=StringIO(txt), filename="sync.txt") - await ctx.channel.send(content="Successfully synced {}/{} players.".format(len(synced), need_sync_count), file=attach) - def setup(bot): bot.add_cog(Commands(bot)) diff --git a/bot/discord.py b/bot/discord.py index 4895dc1..2b57345 100644 --- a/bot/discord.py +++ b/bot/discord.py @@ -7,7 +7,7 @@ from discord.ext import commands from django.conf import settings from minecraft_manager.models import Application, Ticket -from minecraft_manager.utils import full_reverse +from minecraft_manager.utils import url_path logger = logging.getLogger(__name__) @@ -24,10 +24,8 @@ class Discord(commands.Bot): error_users = getattr(settings, 'DISCORD_ERROR_USERS', []) def __init__(self, token): - intents = discord.Intents.default() - intents.members = True super().__init__(command_prefix=self.prefix, description=description, case_insensitive=True, help_command=None, - activity=discord.Game(name=self.discord_game), intents=intents) + activity=discord.Game(name=self.discord_game)) self.token = token self.load_extension("minecraft_manager.bot.commands") @@ -48,11 +46,11 @@ class Discord(commands.Bot): content = "" unanswered_applications = Application.objects.filter(accepted=None) if len(unanswered_applications) > 0: - link = full_reverse('application') + link = url_path(settings.MCM_BASE_LINK, 'dashboard/application') content += "[Unanswered Applications: {}]({})".format(len(unanswered_applications), link) unclaimed_tickets = Ticket.objects.filter(staff=None, resolved=False) if len(unclaimed_tickets) > 0: - link = full_reverse('ticket') + link = url_path(settings.MCM_BASE_LINK, 'dashboard/ticket') if content: content += "\n\n" content += "[Unclaimed Tickets: {}]({})".format(len(unclaimed_tickets), link) @@ -93,5 +91,5 @@ class Discord(commands.Bot): print(e) logger.info('Bot encountered the following unhandled exception %s', e) finally: - loop.run_until_complete(self.close()) + loop.run_until_complete(self.logout()) logger.info("Bot shutting down...") diff --git a/external/views.py b/external/views.py index 1a657f6..6b5623a 100644 --- a/external/views.py +++ b/external/views.py @@ -1,5 +1,5 @@ from django.views.generic import View -from django.shortcuts import render, reverse +from django.shortcuts import render from django.conf import settings from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt @@ -116,7 +116,7 @@ class Ticket(View): if valid and captcha.success: ticket = form.save() # Create the message to send to Discord - link = mcm_utils.full_reverse('ticket_info', ticket.id) + link = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard/ticket', ticket.id)) msg = mcm_utils.build_ticket(ticket, link) mcm_api.discord_mcm(message="New Ticket", embed=msg, ping=True) mcm_api.plugin("ticket", "{0} {1} {2}".format(username, ticket.id, link)) diff --git a/migrations/0017_player_discord_id.py b/migrations/0017_player_discord_id.py deleted file mode 100644 index 0cbc657..0000000 --- a/migrations/0017_player_discord_id.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2.3 on 2021-07-20 23:29 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('minecraft_manager', '0016_auto_20210328_0131'), - ] - - operations = [ - migrations.AddField( - model_name='player', - name='discord_id', - field=models.CharField(blank=True, max_length=30, null=True, unique=True), - ), - ] diff --git a/models.py b/models.py index 4fc573a..c447198 100644 --- a/models.py +++ b/models.py @@ -1,17 +1,10 @@ -import json -import logging -import os -from datetime import datetime -from os.path import basename - -import pytz -import yaml -from django.conf import settings -from django.contrib.auth.models import User from django.db import models +from django.contrib.auth.models import User from django.db.models import Q -from django.db.models.signals import pre_delete -from django.dispatch import receiver +from os.path import basename +import logging, yaml, pytz, json, os +from django.conf import settings +from datetime import datetime logger = logging.getLogger(__name__) @@ -105,7 +98,7 @@ class Player(models.Model): application = models.ForeignKey(Application, on_delete=models.SET_NULL, null=True, blank=True) first_seen = models.DateField(null=True, blank=True) last_seen = models.DateField(null=True, blank=True) - discord_id = models.CharField(max_length=30, unique=True, null=True, blank=True) + @property def is_banned(self): @@ -383,11 +376,6 @@ class Attachment(models.Model): return self.file.name -@receiver(pre_delete, sender=Attachment, dispatch_uid="delete_attachments") -def attachment_delete(sender, instance, **kwargs): - instance.file.delete(False) - - class IPManager(models.Manager): def get_queryset(self): users = User.objects.filter(is_active=True) diff --git a/signals/__init__.py b/signals/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/signals/pre_delete.py b/signals/pre_delete.py new file mode 100644 index 0000000..cb46ea4 --- /dev/null +++ b/signals/pre_delete.py @@ -0,0 +1,2 @@ +def attachment_delete(sender, instance, **kwargs): + instance.file.delete(False) diff --git a/templates/minecraft_manager/application.html b/templates/minecraft_manager/application.html index e60b729..3509399 100644 --- a/templates/minecraft_manager/application.html +++ b/templates/minecraft_manager/application.html @@ -34,7 +34,7 @@ {% endfor %} - Reference Report + Reference Report