minecraft_manager/external/views.py

168 lines
6.6 KiB
Python
Raw Permalink Normal View History

2018-09-20 02:56:17 +00:00
from django.views.generic import View
from django.shortcuts import render, reverse
2018-09-20 02:56:17 +00:00
from django.conf import settings
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from minecraft_manager.forms import ApplicationForm, TicketForm
import minecraft_manager.api.api as mcm_api
import minecraft_manager.utils as mcm_utils
import minecraft_manager.external.stats as mcm_stats
from minecraft_manager.models import Player
import random, yaml, os
2018-09-20 02:56:17 +00:00
def config():
data = {}
data['invite_link'] = getattr(settings, "INVITE_LINK", "#")
data['invite_label'] = getattr(settings, "INVITE_LABEL", "community")
2018-09-20 02:56:17 +00:00
dynmap_url = getattr(settings, "DYNMAP_URL", "")
data['dynmap_url'] = dynmap_url
dynmap_static_url = getattr(settings, "DYNMAP_STATIC_URL", dynmap_url)
data['dynmap_static_url'] = dynmap_static_url
dynmap_world = getattr(settings, "WORLD", "world")
data['dynmap_world'] = dynmap_world
world_border = getattr(settings, "WORLD_BORDER", 100)
data['world_border'] = world_border
data['target'] = "_blank" if dynmap_url else "_self"
x = random.randint(-world_border, world_border)
data['x'] = x
z = random.randint(-world_border, world_border)
data['z'] = z
uri = "?worldname={}&mapname=surface&zoom=5&x={}&y=64&z={}".format(dynmap_world, x, z)
data['map_url'] = "{}{}".format(dynmap_url, uri)
data['static_url'] = "{}{}".format(dynmap_static_url, uri)
return data
def rules():
path = os.path.join(settings.MINECRAFT_BASE_DIR, "plugins/MinecraftManager/config.yml")
with open(path) as config_file:
cfg = yaml.safe_load(config_file)
data = cfg['rules']['rules']
if cfg['rules']['application']['validate']:
data.append("The answer to the final question is \"{}\"".format(cfg['rules']['application']['answer']))
2018-09-20 02:56:17 +00:00
return {
"rules": data,
"validate": cfg['rules']['application']['validate'],
"answer": cfg['rules']['application']['answer']
}
2018-09-20 02:56:17 +00:00
@method_decorator(csrf_exempt, name='dispatch')
class Apply(View):
def get(self, request):
form = ApplicationForm()
return render(request, 'minecraft_manager/external/apply.html',
{'form': form.as_p(), 'rules': rules()["rules"], 'valid': False, 'map': config(),
'captcha': getattr(settings, "CAPTCHA_SITE", "")})
2018-09-20 02:56:17 +00:00
def post(self, request):
form = ApplicationForm(request.POST)
valid_username = mcm_utils.validate_username(form.data['username'])
captcha = mcm_utils.Captcha(request.POST)
valid = form.is_valid()
r = rules()
attachments (#2) Add migration for longer timezone Signed-off-by: Etzelia <etzelia@hotmail.com> Merge branch 'master' of birbmc.com:BirbMC/minecraft_manager into birb # Conflicts: # external/views.py Fix col Signed-off-by: Etzelia <etzelia@hotmail.com> Add attachments, clean up UI, etc. Signed-off-by: Etzelia <etzelia@hotmail.com> Fix Discord embeds (#59) Fix Discord embeds Reviewed-on: https://git.etztech.xyz/MMS/MinecraftManagerDjango/pulls/59 Reviewed-by: ZeroHD <joey@ahines.net> Add requirements (not txt) (#57) Add requirements (not txt) Signed-off-by: Etzelia <etzelia@hotmail.com> Reviewed-on: https://git.etztech.xyz/MMS/MinecraftManagerDjango/pulls/57 Reviewed-by: ZeroHD <joey@ahines.net> Interim Patch (#54) Birb Patches (#1) Birb Patches Signed-off-by: Etzelia <etzelia@hotmail.com> Co-authored-by: Etzelia <etzelia@hotmail.com> Reviewed-by: ZeroHD <joey@ahines.net> Add 'LICENSE' (#53) Add 'LICENSE' Reviewed-by: ZeroHD <joey@ahines.net> Fix captcha and generify community invite (#52) Add validation for final question and some minor CSS Signed-off-by: Etzelia <etzelia@hotmail.com> Change some docs Signed-off-by: Etzelia <etzelia@hotmail.com> Fix captcha and generify community invite Signed-off-by: Etzelia <etzelia@hotmail.com> Co-authored-by: Etzelia <etzelia@hotmail.com> Reviewed-by: ZeroHD <joey@ahines.net> Co-authored-by: Etz Elia <etzelia@hotmail.com> Reviewed-on: https://git.birbmc.com/BirbMC/minecraft_manager/pulls/2 Co-Authored-By: Etzelia <etzelia@hotmail.com> Co-Committed-By: Etzelia <etzelia@hotmail.com>
2021-05-06 17:50:18 +00:00
valid_answer = not r["validate"] or r["answer"].casefold() == form.data['read_rules'].casefold()
if valid and valid_username and valid_answer and captcha.success:
2018-09-20 02:56:17 +00:00
app = form.save()
msg = mcm_utils.build_application(app)
2019-08-30 18:03:12 +00:00
mcm_api.discord_mcm(message='New Application!', embed=msg)
2018-09-20 02:56:17 +00:00
mcm_api.plugin("application", "{0} {1}".format(form.data['username'], app.id))
else:
for error in captcha.errors:
form.add_error(None, error)
if not valid_username:
form.add_error(None, "That username is not a premium Minecraft account")
if not valid_answer:
form.add_error(None, "Please read the rules again")
2018-09-20 02:56:17 +00:00
return render(request, 'minecraft_manager/external/apply.html',
{'form': form.as_p(), 'rules': r["rules"], 'valid': valid and valid_username and valid_answer and captcha.success, 'map': config(),
'captcha': getattr(settings, "CAPTCHA_SITE", "")})
2018-09-20 02:56:17 +00:00
@method_decorator(csrf_exempt, name='dispatch')
class Ticket(View):
def get(self, request):
form = TicketForm()
return render(request, 'minecraft_manager/external/ticket.html',
{'form': form.as_p(), 'valid': False, 'map': config(),
'captcha': getattr(settings, "CAPTCHA_SITE", "")})
2018-09-20 02:56:17 +00:00
def post(self, request):
post = request.POST.copy()
username = post['player']
if 'player' in post and Player.objects.filter(username__iexact=post['player']).exists():
player = Player.objects.get(username__iexact=post['player'])
post.update({'player': player.id})
form = TicketForm(post)
captcha = mcm_utils.Captcha(request.POST)
valid = form.is_valid()
if 'player' not in post:
# Why did I make Player nullable???
valid = False
form.add_error(None, "You must enter your IGN")
if valid and captcha.success:
ticket = form.save()
# Create the message to send to Discord
link = mcm_utils.full_reverse('ticket_info', ticket.id)
2018-09-20 02:56:17 +00:00
msg = mcm_utils.build_ticket(ticket, link)
2019-08-30 18:03:12 +00:00
mcm_api.discord_mcm(message="New Ticket", embed=msg, ping=True)
2018-09-20 02:56:17 +00:00
mcm_api.plugin("ticket", "{0} {1} {2}".format(username, ticket.id, link))
else:
for error in captcha.errors:
form.add_error(None, error)
if 'player' in form.errors:
form.errors['player'][0] = 'You must enter your IGN.'
form.data = form.data.copy()
form.data['player'] = username
return render(request, 'minecraft_manager/external/ticket.html',
{'form': form.as_p(), 'valid': valid and captcha.success, 'map': config(),
'captcha': getattr(settings, "CAPTCHA_SITE", "")})
2018-09-20 02:56:17 +00:00
@method_decorator(csrf_exempt, name='dispatch')
class Stats(View):
def get(self, request):
get = request.GET
results = None
if 'stat' in get and get['stat']:
results = mcm_stats.top_ten_stat(get['stat'])
return render(request, 'minecraft_manager/external/stats_all.html', {'map': config(), 'stats': mcm_stats.get_names(sort=True), 'results': results})
def post(self, request):
pass
@method_decorator(csrf_exempt, name='dispatch')
class StatsPlayer(View):
def get(self, request):
get = request.GET
results = None
if 'username' in get and get['username']:
if Player.objects.filter(username__iexact=get['username']).exists():
uuid = Player.objects.get(username__iexact=get['username']).uuid
results = mcm_stats.top_ten_player(uuid)
if 'uuid' in get and get['uuid']:
results = mcm_stats.top_ten_player(get['uuid'])
return render(request, 'minecraft_manager/external/stats_single.html', {'map': config(), 'results': results})
def post(self, request):
pass