Some changes (#9)

Some changes

Signed-off-by: Etzelia <etzelia@hotmail.com>

Reviewed-on: https://git.canopymc.net/Canopy/minecraft_manager/pulls/9
Co-Authored-By: Etzelia <etzelia@hotmail.com>
Co-Committed-By: Etzelia <etzelia@hotmail.com>
roll
Etzelia 2021-07-01 03:07:45 +00:00
parent 7cb2a14716
commit 9bdacbca75
9 changed files with 92 additions and 76 deletions

View File

@ -1,22 +1,23 @@
from __future__ import absolute_import
import logging, datetime
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
import datetime
import logging
from django.apps import apps
from django.conf import settings
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm
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.forms as mcm_forms
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
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
logger = logging.getLogger(__name__)
@ -205,10 +206,11 @@ class PluginAPI(View):
application.save()
json['message'] = "Application was successfully {0}.".format(
"accepted" if post['action'] == "True" else "denied")
mcm_api.discord_mcm("{0}'s application (#{1}) was {2} by {3}".format(application.username,
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,
application.id,
"accepted" if post['action'] == "True" else "denied",
post['username']))
post['username'], link))
mcm_api.plugin("accept" if post['action'] == "True" else "deny", application.username)
else:
json['status'] = False
@ -297,7 +299,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 = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard/ticket', ticket.id))
link = mcm_utils.full_reverse('ticket_info', ticket.id)
msg = mcm_utils.build_ticket(ticket, link)
json['extra'] = {'id': ticket.id, 'link': link}
mcm_api.discord_mcm(embed=msg, ping=True)
@ -311,7 +313,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 = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard/note', warning.id))
link = mcm_utils.full_reverse('note_info', warning.id)
msg = mcm_utils.build_warning(warning, link)
mcm_api.discord_mcm(embed=msg)
except Exception as ex:
@ -390,7 +392,7 @@ class ModelAPI(View):
json = []
for value in objects:
try:
link = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard', request_model, value['id']))
link = mcm_utils.full_reverse(f"{request_model}_info", value['id'])
value['link'] = link
except:
pass

View File

@ -5,7 +5,7 @@ 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.utils import build_application, full_static
from minecraft_manager.models import Application, Player
@ -44,8 +44,8 @@ class Commands(commands.Cog):
async def help(self, ctx):
embed = discord.Embed(colour=discord.Colour(0x417505))
embed.set_thumbnail(
url="https://cdn.discordapp.com/avatars/454457830918062081/b5792489bc43d9e17b8f657880a17dd4.png")
embed.add_field(name="Minecraft Manager Help", value="-----------------------------")
url=full_static('favicon.png'))
embed.title = "Minecraft Manager Help"
embed.add_field(name="{}app search <username>".format(self.bot.prefix),
value="Search for applications by partial or exact username.")
embed.add_field(name="{}app info <app ID>".format(self.bot.prefix),

View File

@ -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 url_path
from minecraft_manager.utils import full_reverse
logger = logging.getLogger(__name__)
@ -46,11 +46,11 @@ class Discord(commands.Bot):
content = ""
unanswered_applications = Application.objects.filter(accepted=None)
if len(unanswered_applications) > 0:
link = url_path(settings.MCM_BASE_LINK, 'dashboard/application')
link = full_reverse('application')
content += "[Unanswered Applications: {}]({})".format(len(unanswered_applications), link)
unclaimed_tickets = Ticket.objects.filter(staff=None, resolved=False)
if len(unclaimed_tickets) > 0:
link = url_path(settings.MCM_BASE_LINK, 'dashboard/ticket')
link = full_reverse('ticket')
if content:
content += "\n\n"
content += "[Unclaimed Tickets: {}]({})".format(len(unclaimed_tickets), link)

4
external/views.py vendored
View File

@ -1,5 +1,5 @@
from django.views.generic import View
from django.shortcuts import render
from django.shortcuts import render, reverse
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 = "{}".format(mcm_utils.url_path(settings.MCM_BASE_LINK, 'dashboard/ticket', ticket.id))
link = mcm_utils.full_reverse('ticket_info', 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))

View File

@ -34,7 +34,7 @@
{% endfor %}
</tbody>
</table>
<a href="{% url 'reference' %}">Reference Report</a>
<a class="btn btn-primary" href="{% url 'reference' %}">Reference Report</a>
</div>
<script>
$(document).ready(function() {

View File

@ -9,6 +9,7 @@
<th>Player</th>
<th>Message</th>
<th>Priority</th>
<th>Attachments</th>
<th>Claimed</th>
<th>Resolved</th>
<th>Date</th>
@ -25,6 +26,13 @@
{{ ticket.priority_display }}
</span>
</td>
<td>
{% for note in ticket.notes %}
{% for attachment in note.attachments %}
<a href="{% url 'attachment' attachment.id %}">{{ attachment.file_name }}</a><br/>
{% endfor %}
{% endfor %}
</td>
<td>{{ ticket.claimed_by }}</td>
<td>
{% if ticket.resolved %}

View File

@ -12,20 +12,20 @@ urlpatterns = [
# Alerts
url(r'^alert/$', login_required(mcm.Alert.as_view()), name="alert"),
url(r'^alert/(?P<alert_id>[0-9]{1,5})/$', login_required(mcm.AlertInfo.as_view())),
url(r'^alert/(?P<alert_id>[0-9]{1,5})/$', login_required(mcm.AlertInfo.as_view()), name="alert_info"),
# Applications
url(r'^application/$', login_required(mcm.Application.as_view()), name="application"),
url(r'^reference/$', login_required(mcm.Reference.as_view()), name="reference"),
url(r'^application/(?P<application_id>[0-9]{1,5})/$', login_required(mcm.ApplicationInfo.as_view())),
url(r'^application/(?P<application_id>[0-9]{1,5})/$', login_required(mcm.ApplicationInfo.as_view()), name="application_info"),
# Players
url(r'^player/$', login_required(mcm.Player.as_view()), name="player"),
url(r'^player/(?P<player_id>[0-9]{1,5})/$', login_required(mcm.PlayerInfo.as_view())),
url(r'^player/(?P<player_id>[0-9]{1,5})/$', login_required(mcm.PlayerInfo.as_view()), name="player_info"),
# Tickets
url(r'^ticket/$', login_required(mcm.Ticket.as_view()), name="ticket"),
url(r'^ticket/(?P<ticket_id>[0-9]{1,5})/$', login_required(mcm.TicketInfo.as_view())),
url(r'^ticket/(?P<ticket_id>[0-9]{1,5})/$', login_required(mcm.TicketInfo.as_view()), name="ticket_info"),
# Notes
url(r'^note/$', login_required(mcm.Note.as_view()), name="note"),

View File

@ -1,6 +1,9 @@
import discord, requests
import discord
import requests
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.shortcuts import reverse
from django.templatetags.static import static
from minecraft_manager.models import Player, Application
@ -38,18 +41,17 @@ def build_application(application):
if application.reference:
embed.add_field(name="Reference", value=application.reference)
embed.add_field(name="Read the Rules", value=application.read_rules)
embed.add_field(name="Date", value=application.date_display)
embed.timestamp = application.date
embed.add_field(name="Status", value=application.status)
embed.add_field(name="Link", value="{}".format(url_path(settings.MCM_BASE_LINK, 'dashboard/application', application.id)))
embed.add_field(name="Link", value=full_reverse('application_info', application.id))
return embed
def build_ticket(ticket, link):
embed = discord.Embed(colour=discord.Colour(0x417505))
embed.title = "Ticket"
embed.set_thumbnail(
url="https://cdn.discordapp.com/avatars/454457830918062081/b5792489bc43d9e17b8f657880a17dd4.png")
embed.add_field(name="Date", value=ticket.date_display)
embed.set_thumbnail(url=full_static("favicon.png"))
embed.timestamp = ticket.date
embed.add_field(name="Player", value=ticket.player.username.replace("_", "\\_"))
embed.add_field(name="Priority", value=ticket.priority_display)
if ticket.x and ticket.y and ticket.z and ticket.world:
@ -62,9 +64,8 @@ def build_ticket(ticket, link):
def build_warning(warning, link):
embed = discord.Embed(colour=discord.Colour(0x417505))
embed.title = "Warning"
embed.set_thumbnail(
url="https://cdn.discordapp.com/avatars/454457830918062081/b5792489bc43d9e17b8f657880a17dd4.png")
embed.add_field(name="Date", value=warning.date_display)
embed.set_thumbnail(url=full_static("favicon.png"))
embed.timestamp = warning.date
embed.add_field(name="Player", value=warning.player.username.replace("_", "\\_"))
embed.add_field(name="Importance", value=warning.importance_display)
embed.add_field(name="Message", value=warning.message)
@ -72,6 +73,19 @@ def build_warning(warning, link):
return embed
def build_note(note, link):
embed = discord.Embed(colour=discord.Colour(0x417505))
embed.title = "Note"
embed.set_thumbnail(url=full_static("favicon.png"))
embed.timestamp = note.date
embed.add_field(name="Date", value=note.date_display)
embed.add_field(name="Player", value=note.player.username.replace("_", "\\_"))
embed.add_field(name="Importance", value=note.importance_display)
embed.add_field(name="Message", value=note.message)
embed.add_field(name="Link", value=link)
return embed
def validate_username(username):
response = requests.get("https://api.mojang.com/users/profiles/minecraft/{}".format(username))
if response.status_code == 200:
@ -79,16 +93,16 @@ def validate_username(username):
return False
def url_path(*args):
value = []
for arg in args:
arg = str(arg)
if arg.startswith('/'):
arg = arg[1:]
if arg.endswith('/'):
arg = arg[:-1]
value.append(arg)
return '/'.join(value)
def full_reverse(viewname, *args):
base = settings.MCM_DOMAIN.rstrip('/')
view = reverse(viewname, args=args)
return f"{base}{view}"
def full_static(assetname):
base = settings.MCM_DOMAIN.rstrip('/')
asset = static(assetname)
return f"{base}{asset}"
class Captcha:

View File

@ -13,7 +13,7 @@ from django.contrib.auth.models import User
from minecraft_manager.models import Application as AppModel, Player as PlayerModel, Ticket as TicketModel, TicketNote as TicketNoteModel, Note as NoteModel, IP as IPModel, Alert as AlertModel, UserSettings as UserSettingsModel, Attachment as AttachmentModel, RefModels
from minecraft_manager.forms import TicketNoteForm, NoteForm
from minecraft_manager.overview import overview_data
from minecraft_manager.utils import resolve_player
from minecraft_manager.utils import resolve_player, build_note, full_reverse
import minecraft_manager.api.api as API
@ -39,14 +39,12 @@ class Overview(View):
class CoreProtect(View):
def get(self, request):
#http://www.24carrotcraft.com/assets/cp/index.php?username=etzelia
return render(request, 'minecraft_manager/coreprotect.html', {'current_app': 'coreprotect'})
class Activity(View):
def get(self, request):
#http://www.24carrotcraft.com/assets/cp/activity.php?username=etzelia
return render(request, 'minecraft_manager/activity.html', {'current_app': 'activity'})
@ -136,7 +134,6 @@ class Application(View):
class Reference(View):
def get(self, request):
get = request.GET
applications = AppModel.objects.all()
return render(request, 'minecraft_manager/reference.html', {'current_app': 'application', 'applications': applications})
@ -158,7 +155,8 @@ class ApplicationInfo(View):
application.accepted = False
application.save()
API.plugin(post['accept'], application.username)
API.discord_mcm("Application #**{0}** was **{1}** by **{2}**".format(application.id, "Accepted" if application.accepted else "Denied", request.user.player.username))
link = full_reverse('application_info', application_id)
API.discord_mcm("[Application #**{0}**]({3}) was **{1}** by **{2}**".format(application.id, "Accepted" if application.accepted else "Denied", request.user.player.username, link))
return render(request, 'minecraft_manager/application_info.html',
{'current_app': 'application', 'application': application})
@ -187,7 +185,7 @@ class Player(View):
class PlayerInfo(View):
def get(self, request, player_id):
def _info(self, request, player_id):
player = PlayerModel.objects.get(id=player_id)
ips = IPModel.api.filter(player=player)
tickets = TicketModel.objects.filter(player=player)
@ -196,20 +194,16 @@ class PlayerInfo(View):
return render(request, 'minecraft_manager/player_info.html',
{'current_app': 'player', 'player': player, 'form': form})
def get(self, request, player_id):
return self._info(request, player_id)
def post(self, request, player_id):
player = PlayerModel.objects.get(id=player_id)
ips = IPModel.api.filter(player=player)
tickets = TicketModel.objects.filter(player=player)
notes = NoteModel.objects.filter(player=player)
form = {'ips': ips, 'tickets': tickets, 'notes': notes}
return render(request, 'minecraft_manager/player_info.html',
{'current_app': 'player', 'player': player, 'form': form})
return self._info(request, player_id)
class Ticket(View):
def get(self, request):
get = request.GET
tickets1 = TicketModel.objects.filter(resolved=False).order_by('-id')
tickets2 = TicketModel.objects.filter(resolved=True).order_by('-id')
tickets = list(chain(tickets1, tickets2))
@ -240,14 +234,17 @@ class TicketInfo(View):
def post(self, request, ticket_id):
post = request.POST
ticket = TicketModel.objects.get(id=ticket_id)
link = full_reverse('ticket_info', ticket_id)
if 'priority' in post:
if post['priority'] != ticket.priority:
API.discord_mcm(
"Ticket #**{0}**'s priority was changed from **{1}** to **{2}** by **{3}**".format(ticket.id,
"[Ticket #**{0}**]({4})'s priority was changed from **{1}** to **{2}** by **{3}**".format(ticket.id,
ticket.priority_display,
TicketModel.priority_code_to_display(
post['priority']),
request.user.username))
request.user.username,
link)
)
ticket.priority = post['priority']
if 'staff' in post and 'resolved' not in post:
if not ticket.staff or request.user.is_staff:
@ -255,13 +252,13 @@ class TicketInfo(View):
if post['staff'] != str(getattr(ticket.staff, 'id', '-1')):
if post['staff'] == str(request.user.id):
API.discord_mcm(
"Ticket #**{0}** was claimed by **{1}**".format(ticket.id, request.user.username))
"[Ticket #**{0}**]({2}) was claimed by **{1}**".format(ticket.id, request.user.username, link))
else:
API.discord_mcm(
"Ticket #**{0}** was given to **{1}** by **{2}**".format(ticket.id, staff.username, request.user.username))
"[Ticket #**{0}**]({3}) was given to **{1}** by **{2}**".format(ticket.id, staff.username, request.user.username, link))
ticket.staff = staff
if 'resolved' in post:
API.discord_mcm("Ticket #**{0}** was resolved by **{1}**".format(ticket.id, request.user.username))
API.discord_mcm("[Ticket #**{0}**]({2}) was resolved by **{1}**".format(ticket.id, request.user.username, link))
ticket.resolved = True
ticket.save()
@ -327,11 +324,12 @@ class NoteInfo(View):
post = request.POST
note = NoteModel.objects.get(id=note_id)
if 'importance' in post:
API.discord_mcm("Note #**{0}**'s importance was changed from {1} to {2} by {3}".format(
API.discord_mcm("[Note #**{0}**]({4})'s importance was changed from **{1}** to **{2}** by **{3}**".format(
note.id,
note.importance_display,
NoteModel.importance_code_to_display(post['importance']),
request.user.player.username)
request.user.username,
full_reverse('note_info', note_id))
)
note.importance = post['importance']
note.save()
@ -360,17 +358,11 @@ class NoteAdd(View):
note = form.save()
note.staff = request.user
note.save()
API.discord_mcm(
"**{0}** made a **{1}** importance note for **{2}**\nPreview: {3}".format(
note.staff.player.username,
note.importance_display,
note.player.username,
note.snippet)
)
API.discord_mcm(embed=build_note(note, full_reverse('note_info', note.id)))
for file in request.FILES.getlist('attachments', []):
attachment = AttachmentModel(ref_model=RefModels.NOTE[0], ref_id=note.id, file=file)
attachment.save()
return redirect("{0}{1}".format(reverse('note'), note.id))
return redirect("{0}{1}".format(full_reverse('note'), note.id))
else:
return render(request, 'minecraft_manager/note_add.html', context={'current_app': 'note', 'form': form})