core: fix form not showing general errors
This commit is contained in:
		| @ -2,6 +2,13 @@ | |||||||
| {% load i18n %} | {% load i18n %} | ||||||
|  |  | ||||||
| {% csrf_token %} | {% csrf_token %} | ||||||
|  | {% if form.non_field_errors %} | ||||||
|  | <div class="pf-c-form__group has-error"> | ||||||
|  |     <p class="pf-c-form__helper-text pf-m-error"> | ||||||
|  |         {{ form.non_field_errors }} | ||||||
|  |     </p> | ||||||
|  | </div> | ||||||
|  | {% endif %} | ||||||
| {% for field in form %} | {% for field in form %} | ||||||
| <div class="pf-c-form__group {% if field.errors %} has-error {% endif %}"> | <div class="pf-c-form__group {% if field.errors %} has-error {% endif %}"> | ||||||
|     {% if field.field.widget|fieldtype == 'RadioSelect' %} |     {% if field.field.widget|fieldtype == 'RadioSelect' %} | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| """passbook expression policy evaluator""" | """passbook expression policy evaluator""" | ||||||
| import re | import re | ||||||
| from typing import TYPE_CHECKING, Any, Dict, List, Optional | from typing import Any, Dict, List, Optional | ||||||
|  |  | ||||||
| from django.core.exceptions import ValidationError | from django.core.exceptions import ValidationError | ||||||
| from django.http import HttpRequest | from django.http import HttpRequest | ||||||
| @ -10,14 +10,12 @@ from jinja2.nativetypes import NativeEnvironment | |||||||
| from requests import Session | from requests import Session | ||||||
| from structlog import get_logger | from structlog import get_logger | ||||||
|  |  | ||||||
|  | from passbook.core.models import User | ||||||
| from passbook.flows.planner import PLAN_CONTEXT_SSO | from passbook.flows.planner import PLAN_CONTEXT_SSO | ||||||
| from passbook.flows.views import SESSION_KEY_PLAN | from passbook.flows.views import SESSION_KEY_PLAN | ||||||
| from passbook.lib.utils.http import get_client_ip | from passbook.lib.utils.http import get_client_ip | ||||||
| from passbook.policies.types import PolicyRequest, PolicyResult | from passbook.policies.types import PolicyRequest, PolicyResult | ||||||
|  |  | ||||||
| if TYPE_CHECKING: |  | ||||||
|     from passbook.core.models import User |  | ||||||
|  |  | ||||||
| LOGGER = get_logger() | LOGGER = get_logger() | ||||||
|  |  | ||||||
|  |  | ||||||
| @ -43,6 +41,7 @@ class Evaluator: | |||||||
|         self._env.globals["pb_message"] = self.jinja2_func_message |         self._env.globals["pb_message"] = self.jinja2_func_message | ||||||
|         self._context = { |         self._context = { | ||||||
|             "pb_is_group_member": Evaluator.jinja2_func_is_group_member, |             "pb_is_group_member": Evaluator.jinja2_func_is_group_member, | ||||||
|  |             "pb_user_by": Evaluator.jinja2_func_user_by, | ||||||
|             "pb_logger": get_logger(), |             "pb_logger": get_logger(), | ||||||
|             "requests": Session(), |             "requests": Session(), | ||||||
|         } |         } | ||||||
| @ -64,7 +63,15 @@ class Evaluator: | |||||||
|         return re.sub(regex, repl, value) |         return re.sub(regex, repl, value) | ||||||
|  |  | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def jinja2_func_is_group_member(user: "User", group_name: str) -> bool: |     def jinja2_func_user_by(**filters) -> Optional[User]: | ||||||
|  |         """Get user by filters""" | ||||||
|  |         users = User.objects.filter(**filters) | ||||||
|  |         if users: | ||||||
|  |             return users.first() | ||||||
|  |         return None | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|  |     def jinja2_func_is_group_member(user: User, group_name: str) -> bool: | ||||||
|         """Check if `user` is member of group with name `group_name`""" |         """Check if `user` is member of group with name `group_name`""" | ||||||
|         return user.groups.filter(name=group_name).exists() |         return user.groups.filter(name=group_name).exists() | ||||||
|  |  | ||||||
| @ -126,4 +133,4 @@ class Evaluator: | |||||||
|             self._env.from_string(expression) |             self._env.from_string(expression) | ||||||
|             return True |             return True | ||||||
|         except TemplateSyntaxError as exc: |         except TemplateSyntaxError as exc: | ||||||
|             raise ValidationError("Expression Syntax Error") from exc |             raise ValidationError(f"Expression Syntax Error: {str(exc)}") from exc | ||||||
|  | |||||||
| @ -39,4 +39,6 @@ class PolicyResult: | |||||||
|         self.messages = messages |         self.messages = messages | ||||||
|  |  | ||||||
|     def __str__(self): |     def __str__(self): | ||||||
|         return f"<PolicyResult passing={self.passing}>" |         if self.messages: | ||||||
|  |             return f"PolicyResult passing={self.passing} messages={self.messages}" | ||||||
|  |         return f"PolicyResult passing={self.passing}" | ||||||
|  | |||||||
| @ -64,4 +64,4 @@ class PromptForm(forms.Form): | |||||||
|         engine.build() |         engine.build() | ||||||
|         result = engine.result |         result = engine.result | ||||||
|         if not result.passing: |         if not result.passing: | ||||||
|             raise forms.ValidationError(result.messages) |             raise forms.ValidationError(list(result.messages)) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer