Skip to content
Browse files

Change so inclusion rules on a snippet that don't

match stop the snippet from being sent to the user
  • Loading branch information...
1 parent b75190d commit feb90685d545b8a34053e782676664c0a40c3f17 @Osmose Osmose committed Jun 28, 2011
Showing with 126 additions and 33 deletions.
  1. +12 −6 apps/homesnippets/models.py
  2. +76 −27 apps/homesnippets/tests/caching.py
  3. +38 −0 apps/homesnippets/tests/rules.py
View
18 apps/homesnippets/models.py
@@ -55,12 +55,18 @@ def find_ids_for_matches(self, args):
if not cache_hit:
# Cache miss, so recalculate the results and cache them.
- matches = [ rule for rule in self._cached_all()
- if rule.is_match(args) ]
- (include_ids, exclude_ids) = (
- [str(rule.id) for rule in matches if not rule.exclude],
- [str(rule.id) for rule in matches if rule.exclude],
- )
+ rules = self._cached_all()
+ include_ids, exclude_ids = [], []
+
+ for rule in rules:
+ if rule.is_match(args):
+ if rule.exclude:
+ exclude_ids.append(str(rule.id))
+ else:
+ include_ids.append(str(rule.id))
+ elif not rule.exclude:
+ exclude_ids.append(str(rule.id))
+
cache_hit = (mktime(gmtime()), (include_ids, exclude_ids))
cache.set(cache_key, cache_hit, CACHE_TIMEOUT)
View
103 apps/homesnippets/tests/caching.py
@@ -205,27 +205,37 @@ def test_cache_hits(self):
# Request 1
('get', CACHE_RULE_MATCH_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
- CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_NEW_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_NEW_LASTMOD_PREFIX]),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
- CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX,
- CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX]),
# Request 2
('get', CACHE_RULE_MATCH_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
- CACHE_RULE_NEW_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX]),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
- CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
# Request 3
('get', CACHE_RULE_MATCH_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX,
- CACHE_RULE_NEW_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_NEW_LASTMOD_PREFIX]),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
))
@@ -254,6 +264,9 @@ def test_invalidation_on_rule_change(self):
('get', CACHE_RULE_MATCH_PREFIX),
('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_NEW_LASTMOD_PREFIX]),
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_NEW_LASTMOD_PREFIX]),
# All rules cache miss
('get_many', [CACHE_RULE_ALL_PREFIX, CACHE_RULE_ALL_LASTMOD_PREFIX]),
@@ -264,8 +277,10 @@ def test_invalidation_on_rule_change(self):
# Snippet cache miss
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
- CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX,
- CACHE_SNIPPET_LASTMOD_PREFIX]),
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
('set', CACHE_SNIPPET_LOOKUP_PREFIX),
@@ -294,13 +309,20 @@ def test_invalidation_on_snippet_change(self):
# Rule cache miss
('get', CACHE_RULE_MATCH_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_NEW_LASTMOD_PREFIX]),
- ('get_many', [CACHE_RULE_ALL_PREFIX, CACHE_RULE_ALL_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_NEW_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_ALL_PREFIX,
+ CACHE_RULE_ALL_LASTMOD_PREFIX]),
('set', CACHE_RULE_MATCH_PREFIX),
-
+
# Snippet cache miss
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
('set', CACHE_SNIPPET_LOOKUP_PREFIX),
))
@@ -338,7 +360,10 @@ def test_invalidation_with_url_variety(self):
('get_many', [CACHE_RULE_ALL_PREFIX, CACHE_RULE_ALL_LASTMOD_PREFIX]),
('set', CACHE_RULE_MATCH_PREFIX),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
# Snippets looked up & cached for this request only
('set', CACHE_SNIPPET_LOOKUP_PREFIX),
@@ -347,32 +372,56 @@ def test_invalidation_with_url_variety(self):
('get_many', [CACHE_RULE_ALL_PREFIX, CACHE_RULE_ALL_LASTMOD_PREFIX]),
('set', CACHE_RULE_MATCH_PREFIX),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
# Request #3 - cache miss on rules, but hit on snippets
('get', CACHE_RULE_MATCH_PREFIX),
('get_many', [CACHE_RULE_ALL_PREFIX, CACHE_RULE_ALL_LASTMOD_PREFIX]),
('set', CACHE_RULE_MATCH_PREFIX),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
# Request #4 - cache hits, all around
('get', CACHE_RULE_MATCH_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_NEW_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_NEW_LASTMOD_PREFIX]),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
# Request #5 - cache hits, all around
('get', CACHE_RULE_MATCH_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_NEW_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_NEW_LASTMOD_PREFIX]),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
# Request #6 - cache hits, all around
('get', CACHE_RULE_MATCH_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_NEW_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_NEW_LASTMOD_PREFIX]),
('get', CACHE_SNIPPET_LOOKUP_PREFIX),
- ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_SNIPPET_LASTMOD_PREFIX]),
+ ('get_many', [CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX, CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_RULE_LASTMOD_PREFIX,
+ CACHE_SNIPPET_LASTMOD_PREFIX]),
))
View
38 apps/homesnippets/tests/rules.py
@@ -241,3 +241,41 @@ def test_regex_rules(self):
),
})
+ def test_unmatched_inclusion_rules(self):
+ """Exercise inclusion rules that don't match"""
+
+ rules = self.setup_rules({
+ 'fields': ('name', 'version', 'exclude'),
+ 'items': {
+ 'firefox': ('Firefox', None, False),
+ '4.0': (None, '4.0', False),
+ }
+ })
+
+ snippets = self.setup_snippets(rules, {
+ 'fields': ('name', 'body', 'rules'),
+ 'items': {
+ 'fire_4.0': ('Firefox 4.0', 'Firefox and 4.0',
+ (rules['firefox'], rules['4.0'])),
+ 'fire': ('Firefox', 'Firefox but not 4.0', (rules['firefox'],)),
+ '4.0': ('4.0', '4.0 but not Firefox', (rules['4.0'],)),
+ }
+ })
+
+ self.assert_snippets({
+ '/1/Firefox/4.0/xxx/xxx/en-US/xxx/xxx/default/default/': (
+ (snippets['fire_4.0'], True),
+ (snippets['fire'], True),
+ (snippets['4.0'], True),
+ ),
+ '/1/Firefox/3.0/xxx/xxx/en-US/xxx/xxx/default/default/': (
+ (snippets['fire_4.0'], False),
+ (snippets['fire'], True),
+ (snippets['4.0'], False),
+ ),
+ '/1/Mudfish/4.0/xxx/xxx/en-US/xxx/xxx/default/default/': (
+ (snippets['fire_4.0'], False),
+ (snippets['fire'], False),
+ (snippets['4.0'], True),
+ ),
+ })

0 comments on commit feb9068

Please sign in to comment.
Something went wrong with that request. Please try again.