policies/dummy: separate dummy policy from core into app
This commit is contained in:
0
passbook/policies/dummy/__init__.py
Normal file
0
passbook/policies/dummy/__init__.py
Normal file
21
passbook/policies/dummy/api.py
Normal file
21
passbook/policies/dummy/api.py
Normal file
@ -0,0 +1,21 @@
|
||||
"""Dummy Policy API Views"""
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
|
||||
from passbook.policies.dummy.models import DummyPolicy
|
||||
from passbook.policies.forms import GENERAL_SERIALIZER_FIELDS
|
||||
|
||||
|
||||
class DummyPolicySerializer(ModelSerializer):
|
||||
"""Dummy Policy Serializer"""
|
||||
|
||||
class Meta:
|
||||
model = DummyPolicy
|
||||
fields = GENERAL_SERIALIZER_FIELDS + ["result", "wait_min", "wait_max"]
|
||||
|
||||
|
||||
class DummyPolicyViewSet(ModelViewSet):
|
||||
"""Dummy Viewset"""
|
||||
|
||||
queryset = DummyPolicy.objects.all()
|
||||
serializer_class = DummyPolicySerializer
|
||||
11
passbook/policies/dummy/apps.py
Normal file
11
passbook/policies/dummy/apps.py
Normal file
@ -0,0 +1,11 @@
|
||||
"""Passbook policy dummy app config"""
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PassbookPolicyDummyConfig(AppConfig):
|
||||
"""Passbook policy_dummy app config"""
|
||||
|
||||
name = "passbook.policies.dummy"
|
||||
label = "passbook_policies_dummy"
|
||||
verbose_name = "passbook Policies.Dummy"
|
||||
20
passbook/policies/dummy/forms.py
Normal file
20
passbook/policies/dummy/forms.py
Normal file
@ -0,0 +1,20 @@
|
||||
"""passbook Policy forms"""
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from passbook.policies.dummy.models import DummyPolicy
|
||||
from passbook.policies.forms import GENERAL_FIELDS
|
||||
|
||||
|
||||
class DummyPolicyForm(forms.ModelForm):
|
||||
"""DummyPolicyForm Form"""
|
||||
|
||||
class Meta:
|
||||
|
||||
model = DummyPolicy
|
||||
fields = GENERAL_FIELDS + ["result", "wait_min", "wait_max"]
|
||||
widgets = {
|
||||
"name": forms.TextInput(),
|
||||
}
|
||||
labels = {"result": _("Allow user")}
|
||||
40
passbook/policies/dummy/migrations/0001_initial.py
Normal file
40
passbook/policies/dummy/migrations/0001_initial.py
Normal file
@ -0,0 +1,40 @@
|
||||
# Generated by Django 3.0.5 on 2020-05-10 00:08
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
("passbook_core", "0013_delete_debugpolicy"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="DummyPolicy",
|
||||
fields=[
|
||||
(
|
||||
"policy_ptr",
|
||||
models.OneToOneField(
|
||||
auto_created=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
parent_link=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
to="passbook_core.Policy",
|
||||
),
|
||||
),
|
||||
("result", models.BooleanField(default=False)),
|
||||
("wait_min", models.IntegerField(default=5)),
|
||||
("wait_max", models.IntegerField(default=30)),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "Dummy Policy",
|
||||
"verbose_name_plural": "Dummy Policies",
|
||||
},
|
||||
bases=("passbook_core.policy",),
|
||||
),
|
||||
]
|
||||
0
passbook/policies/dummy/migrations/__init__.py
Normal file
0
passbook/policies/dummy/migrations/__init__.py
Normal file
35
passbook/policies/dummy/models.py
Normal file
35
passbook/policies/dummy/models.py
Normal file
@ -0,0 +1,35 @@
|
||||
"""Dummy policy"""
|
||||
from random import SystemRandom
|
||||
from time import sleep
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from structlog import get_logger
|
||||
|
||||
from passbook.core.models import Policy
|
||||
from passbook.policies.types import PolicyRequest, PolicyResult
|
||||
|
||||
LOGGER = get_logger()
|
||||
|
||||
|
||||
class DummyPolicy(Policy):
|
||||
"""Policy used for debugging the PolicyEngine. Returns a fixed result,
|
||||
but takes a random time to process."""
|
||||
|
||||
result = models.BooleanField(default=False)
|
||||
wait_min = models.IntegerField(default=5)
|
||||
wait_max = models.IntegerField(default=30)
|
||||
|
||||
form = "passbook.policies.dummy.forms.DummyPolicyForm"
|
||||
|
||||
def passes(self, request: PolicyRequest) -> PolicyResult:
|
||||
"""Wait random time then return result"""
|
||||
wait = SystemRandom().randrange(self.wait_min, self.wait_max)
|
||||
LOGGER.debug("Policy waiting", policy=self, delay=wait)
|
||||
sleep(wait)
|
||||
return PolicyResult(self.result, "dummy")
|
||||
|
||||
class Meta:
|
||||
|
||||
verbose_name = _("Dummy Policy")
|
||||
verbose_name_plural = _("Dummy Policies")
|
||||
Reference in New Issue
Block a user