From 47fb48fd06ceb3df27533f6eaad8aebb098b681b Mon Sep 17 00:00:00 2001 From: Etz Elia Date: Sat, 6 Jun 2020 19:48:46 +0200 Subject: [PATCH] Fix captcha and generify community invite (#52) Add validation for final question and some minor CSS Signed-off-by: Etzelia Change some docs Signed-off-by: Etzelia Fix captcha and generify community invite Signed-off-by: Etzelia Co-authored-by: Etzelia Reviewed-by: ZeroHD --- docs/source/django-settings.rst | 4 ++- external/views.py | 31 ++++++++++++------- static/minecraft_manager/css/external.css | 5 +++ .../minecraft_manager/external/apply.html | 2 +- .../minecraft_manager/external/base.html | 2 +- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/docs/source/django-settings.rst b/docs/source/django-settings.rst index 76a6469..a4ebbd8 100644 --- a/docs/source/django-settings.rst +++ b/docs/source/django-settings.rst @@ -43,7 +43,9 @@ Optional ``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. diff --git a/external/views.py b/external/views.py index ce3f693..5d40f82 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, redirect +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 @@ -8,12 +8,13 @@ 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, json, datetime, pytz +import random, yaml, os def config(): 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", "") data['dynmap_url'] = dynmap_url @@ -50,7 +51,11 @@ def rules(): if cfg['rules']['application']['validate']: 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') @@ -59,15 +64,17 @@ class Apply(View): def get(self, request): form = ApplicationForm() return render(request, 'minecraft_manager/external/apply.html', - {'form': form.as_p(), 'rules': rules(), 'valid': False, 'map': config(), - 'captcha': hasattr(settings, "CAPTCHA_SECRET")}) + {'form': form.as_p(), 'rules': rules()["rules"], 'valid': False, 'map': config(), + 'captcha': getattr(settings, "CAPTCHA_SITE", "")}) 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() - 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() msg = mcm_utils.build_application(app) mcm_api.discord_mcm(message='New Application!', embed=msg) @@ -77,9 +84,11 @@ class Apply(View): 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") return render(request, 'minecraft_manager/external/apply.html', - {'form': form.as_p(), 'rules': rules(), 'valid': valid and valid_username and captcha.success, 'map': config(), - 'captcha': hasattr(settings, "CAPTCHA_SECRET")}) + {'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", "")}) @method_decorator(csrf_exempt, name='dispatch') @@ -89,7 +98,7 @@ class Ticket(View): form = TicketForm() return render(request, 'minecraft_manager/external/ticket.html', {'form': form.as_p(), 'valid': False, 'map': config(), - 'captcha': hasattr(settings, "CAPTCHA_SECRET")}) + 'captcha': getattr(settings, "CAPTCHA_SITE", "")}) def post(self, request): post = request.POST.copy() @@ -120,7 +129,7 @@ class Ticket(View): form.data['player'] = username return render(request, 'minecraft_manager/external/ticket.html', {'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') diff --git a/static/minecraft_manager/css/external.css b/static/minecraft_manager/css/external.css index 0b2adc0..8f3436b 100644 --- a/static/minecraft_manager/css/external.css +++ b/static/minecraft_manager/css/external.css @@ -67,4 +67,9 @@ .rule { margin-bottom: .5em; +} + +.errorlist { + color: #D8000C; + background-color: #FFD2D2; } \ No newline at end of file diff --git a/templates/minecraft_manager/external/apply.html b/templates/minecraft_manager/external/apply.html index 072c6b2..d376524 100644 --- a/templates/minecraft_manager/external/apply.html +++ b/templates/minecraft_manager/external/apply.html @@ -17,7 +17,7 @@
We will get back to you soon.
- Consider joining our Discord + Consider joining our {{ map.invite_label }} {% endblock %} {% endif %} diff --git a/templates/minecraft_manager/external/base.html b/templates/minecraft_manager/external/base.html index 3819580..74197ad 100644 --- a/templates/minecraft_manager/external/base.html +++ b/templates/minecraft_manager/external/base.html @@ -21,7 +21,7 @@ {% else %}
{% block form %}{{ form }}{% endblock %}

- {% if captcha %}
{% endif %} + {% if captcha %}
{% endif %}
{% endif %}