factors: -> stage

This commit is contained in:
Jens Langhammer
2020-05-08 19:46:39 +02:00
parent 08c0eb2ec6
commit 212e966dd4
99 changed files with 745 additions and 958 deletions

View File

View File

@ -0,0 +1,21 @@
"""CaptchaStage API Views"""
from rest_framework.serializers import ModelSerializer
from rest_framework.viewsets import ModelViewSet
from passbook.stages.captcha.models import CaptchaStage
class CaptchaStageSerializer(ModelSerializer):
"""CaptchaStage Serializer"""
class Meta:
model = CaptchaStage
fields = ["pk", "name", "public_key", "private_key"]
class CaptchaStageViewSet(ModelViewSet):
"""CaptchaStage Viewset"""
queryset = CaptchaStage.objects.all()
serializer_class = CaptchaStageSerializer

View File

@ -0,0 +1,10 @@
"""passbook captcha app"""
from django.apps import AppConfig
class PassbookStageCaptchaConfig(AppConfig):
"""passbook captcha app"""
name = "passbook.stages.captcha"
label = "passbook_stages_captcha"
verbose_name = "passbook Stages.Captcha"

View File

@ -0,0 +1,25 @@
"""passbook captcha stage forms"""
from captcha.fields import ReCaptchaField
from django import forms
from passbook.stages.captcha.models import CaptchaStage
class CaptchaForm(forms.Form):
"""passbook captcha stage form"""
captcha = ReCaptchaField()
class CaptchaStageForm(forms.ModelForm):
"""Form to edit CaptchaStage Instance"""
class Meta:
model = CaptchaStage
fields = ["name", "public_key", "private_key"]
widgets = {
"name": forms.TextInput(),
"public_key": forms.TextInput(),
"private_key": forms.TextInput(),
}

View File

@ -0,0 +1,49 @@
# Generated by Django 3.0.3 on 2020-05-08 17:58
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
("passbook_flows", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="CaptchaStage",
fields=[
(
"stage_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="passbook_flows.Stage",
),
),
(
"public_key",
models.TextField(
help_text="Public key, acquired from https://www.google.com/recaptcha/intro/v3.html"
),
),
(
"private_key",
models.TextField(
help_text="Private key, acquired from https://www.google.com/recaptcha/intro/v3.html"
),
),
],
options={
"verbose_name": "Captcha Stage",
"verbose_name_plural": "Captcha Stages",
},
bases=("passbook_flows.stage",),
),
]

View File

@ -0,0 +1,31 @@
"""passbook captcha stage"""
from django.db import models
from django.utils.translation import gettext_lazy as _
from passbook.flows.models import Stage
class CaptchaStage(Stage):
"""Captcha Stage instance"""
public_key = models.TextField(
help_text=_(
"Public key, acquired from https://www.google.com/recaptcha/intro/v3.html"
)
)
private_key = models.TextField(
help_text=_(
"Private key, acquired from https://www.google.com/recaptcha/intro/v3.html"
)
)
type = "passbook.stages.captcha.stage.CaptchaStage"
form = "passbook.stages.captcha.forms.CaptchaStageForm"
def __str__(self):
return f"Captcha Stage {self.name}"
class Meta:
verbose_name = _("Captcha Stage")
verbose_name_plural = _("Captcha Stages")

View File

@ -0,0 +1,8 @@
"""passbook captcha stage settings"""
# https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do
RECAPTCHA_PUBLIC_KEY = "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"
RECAPTCHA_PRIVATE_KEY = "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"
NOCAPTCHA = True
INSTALLED_APPS = ["captcha"]
SILENCED_SYSTEM_CHECKS = ["captcha.recaptcha_test_key_error"]

View File

@ -0,0 +1,24 @@
"""passbook captcha stage"""
from django.views.generic import FormView
from passbook.flows.stage import AuthenticationStage
from passbook.stages.captcha.forms import CaptchaForm
class CaptchaStage(FormView, AuthenticationStage):
"""Simple captcha checker, logic is handeled in django-captcha module"""
form_class = CaptchaForm
def form_valid(self, form):
return self.executor.stage_ok()
def get_form(self, form_class=None):
form = CaptchaForm(**self.get_form_kwargs())
form.fields["captcha"].public_key = self.executor.current_stage.public_key
form.fields["captcha"].private_key = self.executor.current_stage.private_key
form.fields["captcha"].widget.attrs["data-sitekey"] = form.fields[
"captcha"
].public_key
return form