From 81fdcbadaddeb4bd9a8309e778b2865dfcb8f8a7 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Thu, 21 Mar 2019 16:21:51 +0100 Subject: [PATCH] add compiled regex to RewriteRule --- passbook/app_gw/forms.py | 21 ++++++++++++++++--- .../migrations/0002_auto_20190321_1521.py | 18 ++++++++++++++++ passbook/app_gw/models.py | 15 ++++++++++++- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 passbook/app_gw/migrations/0002_auto_20190321_1521.py diff --git a/passbook/app_gw/forms.py b/passbook/app_gw/forms.py index 11aafd6215..3f673eb87f 100644 --- a/passbook/app_gw/forms.py +++ b/passbook/app_gw/forms.py @@ -4,7 +4,7 @@ from django import forms from django.contrib.admin.widgets import FilteredSelectMultiple 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 @@ -15,7 +15,7 @@ class ApplicationGatewayProviderForm(forms.ModelForm): model = ApplicationGatewayProvider fields = ['server_name', 'upstream', 'enabled', 'authentication_header', - 'default_content_type', 'upstream_ssl_verification'] + 'default_content_type', 'upstream_ssl_verification', 'property_mappings'] widgets = { 'authentication_header': forms.TextInput(), 'default_content_type': forms.TextInput(), @@ -26,5 +26,20 @@ class ApplicationGatewayProviderForm(forms.ModelForm): 'upstream': DynamicArrayField } 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) } diff --git a/passbook/app_gw/migrations/0002_auto_20190321_1521.py b/passbook/app_gw/migrations/0002_auto_20190321_1521.py new file mode 100644 index 0000000000..3a9dbe3015 --- /dev/null +++ b/passbook/app_gw/migrations/0002_auto_20190321_1521.py @@ -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'), + ), + ] diff --git a/passbook/app_gw/models.py b/passbook/app_gw/models.py index a6a8aa1fa1..1bdf2b80b6 100644 --- a/passbook/app_gw/models.py +++ b/passbook/app_gw/models.py @@ -1,4 +1,6 @@ """passbook app_gw models""" +import re + from django.contrib.postgres.fields import ArrayField from django.db import models from django.utils.translation import gettext as _ @@ -48,10 +50,21 @@ class RewriteRule(PropertyMapping): match = models.TextField() halt = models.BooleanField(default=False) - conditions = models.ManyToManyField(Policy) + conditions = models.ManyToManyField(Policy, blank=True) replacement = models.TextField() # python formatted strings, use {match.1} 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): return "Rewrite Rule %s" % self.name