Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions admin/pageaudit/report/migrations/0016_merge_20181204_1315.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.0.8 on 2018-12-04 18:15

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('report', '0015_auto_20181130_1123'),
('report', '0012_auto_20181109_1225'),
]

operations = [
]
24 changes: 24 additions & 0 deletions admin/pageaudit/report/migrations/0017_auto_20181204_1315.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.0.8 on 2018-12-04 18:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('report', '0016_merge_20181204_1315'),
]

operations = [
migrations.AddField(
model_name='urlfilter',
name='mode',
field=models.CharField(choices=[('AND', 'AND'), ('OR', 'OR')], default='AND', max_length=16),
preserve_default=False,
),
migrations.AlterField(
model_name='urlfilter',
name='slug',
field=models.SlugField(unique=True),
),
]
9 changes: 8 additions & 1 deletion admin/pageaudit/report/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,11 +961,17 @@ class UrlFilter(models.Model):
A named UrlFilter. Allows users to create and save a filter for reuse and shared usage.
"""

FILTER_MODES = (
('AND', 'AND',),
('OR', 'OR',),
)

created_date = models.DateTimeField(auto_now_add=True, editable=False)
modified_date = models.DateTimeField(auto_now=True, editable=False)
name = models.CharField(max_length=255, unique=True)
description = models.TextField(null=True, blank=True)
slug = models.SlugField(unique=True)
mode = models.CharField(max_length=16, choices=FILTER_MODES)

class Meta:
ordering = ['name']
Expand All @@ -985,12 +991,13 @@ def run_query(self):
to do dashboard operations on.
"""
filter_parts = UrlFilterPart.objects.filter(url_filter=self)
filter_map = {'OR': Q.OR, 'AND': Q.AND}

and_condition = Q()

for part in filter_parts:
query_obj = self.make_query_object(part)
and_condition.add(Q(**query_obj), Q.AND)
and_condition.add(Q(**query_obj), filter_map[self.mode])

query_set = Url.objects.filter(and_condition).distinct()

Expand Down
5 changes: 4 additions & 1 deletion admin/pageaudit/report/templates/reports_filters.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@
<tr>
<td colspan="4" class="pb3 o-0">
{% for part in parts %}
<span class="pa1 ph2 mr3 bg-dark-green dib white {{ templateHelpers.classes.rounded }}">{{ part.prop|title }} = {{ part.filter_val }}</span>
<span class="pa1 ph2 ml1 mr1 bg-dark-green dib white {{ templateHelpers.classes.rounded }}">{{ part.prop|title }} = {{ part.filter_val }}</span>
{% if forloop.last == False %}
<span>{{ filter.mode|lower }}</span>
{% endif %}
{% endfor %}
</td>
</tr>
Expand Down
21 changes: 15 additions & 6 deletions admin/pageaudit/report/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ def setUp(self):
)

# make 3 UrlFilters
filters = ['foo', 'bar', 'w00t']
filters = ['foo', 'bar', 'foo or w00t']
filter_arr = []

for filter in filters:
filter_arr.append(
UrlFilter.objects.create(
name='%s filter' % filter,
description='%s filter description' % filter
description='%s filter description' % filter,
slug='%s-filter' % filter,
mode='AND' if 'or' not in filter else 'OR'
)
)

Expand All @@ -63,12 +65,17 @@ def setUp(self):
filter_path_index=1,
url_filter=filter_arr[1]
)
elif filter == 'w00t':
elif filter == 'foo or w00t':
UrlFilterPart.objects.create(
prop='hash',
filter_val='w00t',
url_filter=filter_arr[2]
)
UrlFilterPart.objects.create(
prop='path_segment',
filter_val='foo',
url_filter=filter_arr[2]
)

def test_UrlFilters(self):
# import ipdb; ipdb.set_trace()
Expand All @@ -81,9 +88,11 @@ def test_UrlFilters(self):
bar = UrlFilter.objects.get(name='bar filter')
urls = bar.run_query()

self.assertEqual(urls[0].url, 'https://ibm.com/bar/baz/biff')
self.assertEqual(urls[0].url, 'https://ibm.com/bar/baz/#w00t')

woot = UrlFilter.objects.get(name='w00t filter')
urls = woot.run_query()
foo_or_woot = UrlFilter.objects.get(name='foo or w00t filter')
urls = foo_or_woot.run_query()

self.assertEqual(urls[0].url, 'https://ibm.com/bar/baz/#w00t')
self.assertEqual(urls[1].url, 'https://ibm.com/foo')
self.assertEqual(len(urls), 2)