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>
pull/2/head
Etz Elia 2020-06-06 19:48:46 +02:00
parent faa342933e
commit 47fb48fd06
5 changed files with 30 additions and 14 deletions

View File

@ -43,7 +43,9 @@ Optional
``DISCORD_NOTIFICATION_CHANNEL`` - The ID for the channel used for notifications. ``DISCORD_NOTIFICATION_CHANNEL`` - The ID for the channel used for notifications.
``DISCORD_INVITE`` - The invite code to your Discord, for after a player applies on the web form. ``INVITE_LINK`` - The invite link to your community.
``INVITE_LABEL`` - The invite label for your community.
``DYNMAP_URL`` - The URL to your dynmap if you have one. Leave blank if you'd rather use a static background for web forms. ``DYNMAP_URL`` - The URL to your dynmap if you have one. Leave blank if you'd rather use a static background for web forms.

31
external/views.py vendored
View File

@ -1,5 +1,5 @@
from django.views.generic import View from django.views.generic import View
from django.shortcuts import render, reverse, redirect from django.shortcuts import render
from django.conf import settings from django.conf import settings
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
@ -8,12 +8,13 @@ import minecraft_manager.api.api as mcm_api
import minecraft_manager.utils as mcm_utils import minecraft_manager.utils as mcm_utils
import minecraft_manager.external.stats as mcm_stats import minecraft_manager.external.stats as mcm_stats
from minecraft_manager.models import Player from minecraft_manager.models import Player
import random, yaml, os, json, datetime, pytz import random, yaml, os
def config(): def config():
data = {} data = {}
data['discord_invite'] = getattr(settings, "DISCORD_INVITE", "#") data['invite_link'] = getattr(settings, "INVITE_LINK", "#")
data['invite_label'] = getattr(settings, "INVITE_LABEL", "community")
dynmap_url = getattr(settings, "DYNMAP_URL", "") dynmap_url = getattr(settings, "DYNMAP_URL", "")
data['dynmap_url'] = dynmap_url data['dynmap_url'] = dynmap_url
@ -50,7 +51,11 @@ def rules():
if cfg['rules']['application']['validate']: if cfg['rules']['application']['validate']:
data.append("The answer to the final question is \"{}\"".format(cfg['rules']['application']['answer'])) data.append("The answer to the final question is \"{}\"".format(cfg['rules']['application']['answer']))
return data return {
"rules": data,
"validate": cfg['rules']['application']['validate'],
"answer": cfg['rules']['application']['answer']
}
@method_decorator(csrf_exempt, name='dispatch') @method_decorator(csrf_exempt, name='dispatch')
@ -59,15 +64,17 @@ class Apply(View):
def get(self, request): def get(self, request):
form = ApplicationForm() form = ApplicationForm()
return render(request, 'minecraft_manager/external/apply.html', return render(request, 'minecraft_manager/external/apply.html',
{'form': form.as_p(), 'rules': rules(), 'valid': False, 'map': config(), {'form': form.as_p(), 'rules': rules()["rules"], 'valid': False, 'map': config(),
'captcha': hasattr(settings, "CAPTCHA_SECRET")}) 'captcha': getattr(settings, "CAPTCHA_SITE", "")})
def post(self, request): def post(self, request):
form = ApplicationForm(request.POST) form = ApplicationForm(request.POST)
valid_username = mcm_utils.validate_username(form.data['username']) valid_username = mcm_utils.validate_username(form.data['username'])
captcha = mcm_utils.Captcha(request.POST) captcha = mcm_utils.Captcha(request.POST)
valid = form.is_valid() valid = form.is_valid()
if valid and valid_username and captcha.success: r = rules()
valid_answer = not r["validate"] or r["answer"] == form.data['read_rules']
if valid and valid_username and valid_answer and captcha.success:
app = form.save() app = form.save()
msg = mcm_utils.build_application(app) msg = mcm_utils.build_application(app)
mcm_api.discord_mcm(message='New Application!', embed=msg) mcm_api.discord_mcm(message='New Application!', embed=msg)
@ -77,9 +84,11 @@ class Apply(View):
form.add_error(None, error) form.add_error(None, error)
if not valid_username: if not valid_username:
form.add_error(None, "That username is not a premium Minecraft account") 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")
return render(request, 'minecraft_manager/external/apply.html', return render(request, 'minecraft_manager/external/apply.html',
{'form': form.as_p(), 'rules': rules(), 'valid': valid and valid_username and captcha.success, 'map': config(), {'form': form.as_p(), 'rules': r["rules"], 'valid': valid and valid_username and valid_answer and captcha.success, 'map': config(),
'captcha': hasattr(settings, "CAPTCHA_SECRET")}) 'captcha': getattr(settings, "CAPTCHA_SITE", "")})
@method_decorator(csrf_exempt, name='dispatch') @method_decorator(csrf_exempt, name='dispatch')
@ -89,7 +98,7 @@ class Ticket(View):
form = TicketForm() form = TicketForm()
return render(request, 'minecraft_manager/external/ticket.html', return render(request, 'minecraft_manager/external/ticket.html',
{'form': form.as_p(), 'valid': False, 'map': config(), {'form': form.as_p(), 'valid': False, 'map': config(),
'captcha': hasattr(settings, "CAPTCHA_SECRET")}) 'captcha': getattr(settings, "CAPTCHA_SITE", "")})
def post(self, request): def post(self, request):
post = request.POST.copy() post = request.POST.copy()
@ -120,7 +129,7 @@ class Ticket(View):
form.data['player'] = username form.data['player'] = username
return render(request, 'minecraft_manager/external/ticket.html', return render(request, 'minecraft_manager/external/ticket.html',
{'form': form.as_p(), 'valid': valid and captcha.success, 'map': config(), {'form': form.as_p(), 'valid': valid and captcha.success, 'map': config(),
'captcha': hasattr(settings, "CAPTCHA_SECRET")}) 'captcha': getattr(settings, "CAPTCHA_SITE", "")})
@method_decorator(csrf_exempt, name='dispatch') @method_decorator(csrf_exempt, name='dispatch')

View File

@ -67,4 +67,9 @@
.rule { .rule {
margin-bottom: .5em; margin-bottom: .5em;
}
.errorlist {
color: #D8000C;
background-color: #FFD2D2;
} }

View File

@ -17,7 +17,7 @@
<br/> <br/>
We will get back to you soon. We will get back to you soon.
<br/> <br/>
Consider joining our <a href="https://discord.gg/{{ map.discord_invite }}">Discord</a></h2> Consider joining our <a href="{{ map.invite_link }}">{{ map.invite_label }}</a></h2>
{% endblock %} {% endblock %}
{% endif %} {% endif %}

View File

@ -21,7 +21,7 @@
{% else %} {% else %}
<form method="{% block method %}POST{% endblock %}"> <form method="{% block method %}POST{% endblock %}">
{% block form %}{{ form }}{% endblock %}<br/><br/> {% block form %}{{ form }}{% endblock %}<br/><br/>
{% if captcha %}<div class="g-recaptcha" data-sitekey="6LeLcGEUAAAAAMMpHR-7VL6Q3nNV5v03S5sq1Ddz"></div>{% endif %} {% if captcha %}<div class="g-recaptcha" data-sitekey="{{ captcha }}"></div>{% endif %}
<button type="submit">{% block submit %}Submit{% endblock %}</button> <button type="submit">{% block submit %}Submit{% endblock %}</button>
</form> </form>
{% endif %} {% endif %}