From 520e9913930ebab1ea3ce3b07a657bba6326bed7 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 30 Nov 2018 15:50:27 +0100 Subject: [PATCH] admin: add view to test rules --- passbook/admin/forms/rule.py | 10 ++++++ .../admin/templates/administration/base.html | 2 +- .../templates/administration/rule/test.html | 7 ++++ passbook/admin/urls.py | 1 + passbook/admin/views/rules.py | 36 ++++++++++++++++++- 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 passbook/admin/forms/rule.py create mode 100644 passbook/admin/templates/administration/rule/test.html diff --git a/passbook/admin/forms/rule.py b/passbook/admin/forms/rule.py new file mode 100644 index 000000000..a633a83e8 --- /dev/null +++ b/passbook/admin/forms/rule.py @@ -0,0 +1,10 @@ +"""passbook administration forms""" +from django import forms + +from passbook.core.models import User + + +class RuleTestForm(forms.Form): + """Form to test rule against user""" + + user = forms.ModelChoiceField(queryset=User.objects.all()) diff --git a/passbook/admin/templates/administration/base.html b/passbook/admin/templates/administration/base.html index a610184b0..5e30d0a1a 100644 --- a/passbook/admin/templates/administration/base.html +++ b/passbook/admin/templates/administration/base.html @@ -14,7 +14,7 @@
  • {% trans 'Sources' %}
  • -
  • +
  • {% trans 'Rules' %}
  • diff --git a/passbook/admin/templates/administration/rule/test.html b/passbook/admin/templates/administration/rule/test.html new file mode 100644 index 000000000..5d8a9f4f6 --- /dev/null +++ b/passbook/admin/templates/administration/rule/test.html @@ -0,0 +1,7 @@ +{% extends 'generic/form.html' %} + +{% load i18n %} + +{% block above_form %} +

    {% blocktrans with rule=rule %}Test rule {{ rule }}{% endblocktrans %}

    +{% endblock %} \ No newline at end of file diff --git a/passbook/admin/urls.py b/passbook/admin/urls.py index e81cf7c41..334e60ef1 100644 --- a/passbook/admin/urls.py +++ b/passbook/admin/urls.py @@ -25,6 +25,7 @@ urlpatterns = [ path('rules/create/', rules.RuleCreateView.as_view(), name='rule-create'), path('rules//update/', rules.RuleUpdateView.as_view(), name='rule-update'), path('rules//delete/', rules.RuleDeleteView.as_view(), name='rule-delete'), + path('rules//test/', rules.RuleTestView.as_view(), name='rule-test'), # Providers path('providers/', providers.ProviderListView.as_view(), name='providers'), path('providers/create/', diff --git a/passbook/admin/views/rules.py b/passbook/admin/views/rules.py index aa4917389..c4aa85510 100644 --- a/passbook/admin/views/rules.py +++ b/passbook/admin/views/rules.py @@ -1,10 +1,14 @@ """passbook Rule administration""" +from django.contrib import messages from django.contrib.messages.views import SuccessMessageMixin from django.http import Http404 from django.urls import reverse_lazy from django.utils.translation import ugettext as _ -from django.views.generic import CreateView, DeleteView, ListView, UpdateView +from django.views.generic import (CreateView, DeleteView, FormView, ListView, + UpdateView) +from django.views.generic.detail import DetailView +from passbook.admin.forms.rule import RuleTestForm from passbook.admin.mixins import AdminRequiredMixin from passbook.core.models import Rule from passbook.lib.utils.reflection import path_to_class @@ -68,3 +72,33 @@ class RuleDeleteView(SuccessMessageMixin, AdminRequiredMixin, DeleteView): def get_object(self, queryset=None): return Rule.objects.filter(pk=self.kwargs.get('pk')).select_subclasses().first() + + +class RuleTestView(AdminRequiredMixin, DetailView, FormView): + """View to test rule(s)""" + + model = Rule + form_class = RuleTestForm + template_name = 'administration/rule/test.html' + object = None + + def get_object(self, queryset=None): + return Rule.objects.filter(pk=self.kwargs.get('pk')).select_subclasses().first() + + def get_context_data(self, **kwargs): + kwargs['rule'] = self.get_object() + return super().get_context_data(**kwargs) + + def post(self, *args, **kwargs): + self.object = self.get_object() + return super().post(*args, **kwargs) + + def form_valid(self, form): + rule = self.get_object() + user = form.cleaned_data.get('user') + result = rule.passes(user) + if result: + messages.success(self.request, _('User successfully passed rule.')) + else: + messages.error(self.request, _("User didn't pass rule.")) + return self.render_to_response(self.get_context_data(form=form, result=result))