admin: add view to test rules
This commit is contained in:
parent
0ce242e956
commit
520e991393
|
@ -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())
|
|
@ -14,7 +14,7 @@
|
|||
<li class="{% is_active 'passbook_admin:sources' 'passbook_admin:source-create' 'passbook_admin:source-update' 'passbook_admin:source-delete' %}">
|
||||
<a href="{% url 'passbook_admin:sources' %}">{% trans 'Sources' %}</a>
|
||||
</li>
|
||||
<li class="{% is_active 'passbook_admin:rules' 'passbook_admin:rule-create' 'passbook_admin:rule-update' 'passbook_admin:rule-delete' %}">
|
||||
<li class="{% is_active 'passbook_admin:rules' 'passbook_admin:rule-create' 'passbook_admin:rule-update' 'passbook_admin:rule-delete' 'passbook_admin:rule-test' %}">
|
||||
<a href="{% url 'passbook_admin:rules' %}">{% trans 'Rules' %}</a>
|
||||
</li>
|
||||
<li class="{% is_active 'passbook_admin:providers' 'passbook_admin:provider-create' 'passbook_admin:provider-update' 'passbook_admin:provider-delete' %}">
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
{% extends 'generic/form.html' %}
|
||||
|
||||
{% load i18n %}
|
||||
|
||||
{% block above_form %}
|
||||
<h1>{% blocktrans with rule=rule %}Test rule {{ rule }}{% endblocktrans %}</h1>
|
||||
{% endblock %}
|
|
@ -25,6 +25,7 @@ urlpatterns = [
|
|||
path('rules/create/', rules.RuleCreateView.as_view(), name='rule-create'),
|
||||
path('rules/<uuid:pk>/update/', rules.RuleUpdateView.as_view(), name='rule-update'),
|
||||
path('rules/<uuid:pk>/delete/', rules.RuleDeleteView.as_view(), name='rule-delete'),
|
||||
path('rules/<uuid:pk>/test/', rules.RuleTestView.as_view(), name='rule-test'),
|
||||
# Providers
|
||||
path('providers/', providers.ProviderListView.as_view(), name='providers'),
|
||||
path('providers/create/',
|
||||
|
|
|
@ -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))
|
||||
|
|
Reference in New Issue