add compiled regex to RewriteRule

This commit is contained in:
Jens Langhammer 2019-03-21 16:21:51 +01:00
parent ec1e25fe71
commit 81fdcbadad
3 changed files with 50 additions and 4 deletions

View File

@ -4,7 +4,7 @@ from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple from django.contrib.admin.widgets import FilteredSelectMultiple
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from passbook.app_gw.models import ApplicationGatewayProvider from passbook.app_gw.models import ApplicationGatewayProvider, RewriteRule
from passbook.lib.fields import DynamicArrayField from passbook.lib.fields import DynamicArrayField
@ -15,7 +15,7 @@ class ApplicationGatewayProviderForm(forms.ModelForm):
model = ApplicationGatewayProvider model = ApplicationGatewayProvider
fields = ['server_name', 'upstream', 'enabled', 'authentication_header', fields = ['server_name', 'upstream', 'enabled', 'authentication_header',
'default_content_type', 'upstream_ssl_verification'] 'default_content_type', 'upstream_ssl_verification', 'property_mappings']
widgets = { widgets = {
'authentication_header': forms.TextInput(), 'authentication_header': forms.TextInput(),
'default_content_type': forms.TextInput(), 'default_content_type': forms.TextInput(),
@ -26,5 +26,20 @@ class ApplicationGatewayProviderForm(forms.ModelForm):
'upstream': DynamicArrayField 'upstream': DynamicArrayField
} }
labels = { labels = {
'upstream_ssl_verification': _('Verify upstream SSL Certificates?') 'upstream_ssl_verification': _('Verify upstream SSL Certificates?'),
'property_mappings': _('Rewrite Rules')
}
class RewriteRuleForm(forms.ModelForm):
"""Rewrite Rule Form"""
class Meta:
model = RewriteRule
fields = ['name', 'match', 'halt', 'replacement', 'redirect', 'conditions']
widgets = {
'name': forms.TextInput(),
'match': forms.TextInput(attrs={'data-is-monospace': True}),
'replacement': forms.TextInput(attrs={'data-is-monospace': True}),
'conditions': FilteredSelectMultiple(_('Conditions'), False)
} }

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1.7 on 2019-03-21 15:21
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('passbook_app_gw', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='rewriterule',
name='conditions',
field=models.ManyToManyField(blank=True, to='passbook_core.Policy'),
),
]

View File

@ -1,4 +1,6 @@
"""passbook app_gw models""" """passbook app_gw models"""
import re
from django.contrib.postgres.fields import ArrayField from django.contrib.postgres.fields import ArrayField
from django.db import models from django.db import models
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -48,10 +50,21 @@ class RewriteRule(PropertyMapping):
match = models.TextField() match = models.TextField()
halt = models.BooleanField(default=False) halt = models.BooleanField(default=False)
conditions = models.ManyToManyField(Policy) conditions = models.ManyToManyField(Policy, blank=True)
replacement = models.TextField() # python formatted strings, use {match.1} replacement = models.TextField() # python formatted strings, use {match.1}
redirect = models.CharField(max_length=50, choices=REDIRECTS) redirect = models.CharField(max_length=50, choices=REDIRECTS)
form = 'passbook.app_gw.forms.RewriteRuleForm'
_matcher = None
@property
def compiled_matcher(self):
"""Cache the compiled regex in memory"""
if not self._matcher:
self._matcher = re.compile(self.match)
return self._matcher
def __str__(self): def __str__(self):
return "Rewrite Rule %s" % self.name return "Rewrite Rule %s" % self.name