core: add template field to PropertyMapping

This commit is contained in:
Jens Langhammer 2020-02-17 17:47:51 +01:00
parent a5629c5155
commit e6c47fee26
2 changed files with 30 additions and 2 deletions

View File

@ -0,0 +1,19 @@
# Generated by Django 3.0.3 on 2020-02-17 16:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("passbook_core", "0005_merge_20191025_2022"),
]
operations = [
migrations.AddField(
model_name="propertymapping",
name="template",
field=models.TextField(default=""),
preserve_default=False,
),
]

View File

@ -2,15 +2,17 @@
from datetime import timedelta
from random import SystemRandom
from time import sleep
from typing import Optional
from typing import Optional, Any
from uuid import uuid4
from jinja2.nativetypes import NativeEnvironment
from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import JSONField
from django.db import models
from django.urls import reverse_lazy
from django.http import HttpRequest
from django.utils.timezone import now
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy as _
from django_prometheus.models import ExportModelOperationsMixin
from guardian.mixins import GuardianUserMixin
from model_utils.managers import InheritanceManager
@ -22,6 +24,7 @@ from passbook.policies.exceptions import PolicyException
from passbook.policies.struct import PolicyRequest, PolicyResult
LOGGER = get_logger()
NATIVE_ENVIRONMENT = NativeEnvironment()
def default_nonce_duration():
@ -293,10 +296,16 @@ class PropertyMapping(UUIDModel):
"""User-defined key -> x mapping which can be used by providers to expose extra data."""
name = models.TextField()
template = models.TextField()
form = ""
objects = InheritanceManager()
def render(self, user: User, request: HttpRequest, **kwargs) -> Any:
"""Render `self.template` using `**kwargs` as Context."""
template = NATIVE_ENVIRONMENT.from_string(self.template)
return template.render(user=user, request=request, **kwargs)
def __str__(self):
return f"Property Mapping {self.name}"