Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add better documentation

  • Loading branch information...
commit d584b972bf65b6adca742dc420e89ed4ea9afc0c 1 parent e2abb9a
@Osmose Osmose authored
Showing with 16 additions and 3 deletions.
  1. +12 −2 apps/homesnippets/models.py
  2. +4 −1 apps/homesnippets/tests/rules.py
View
14 apps/homesnippets/models.py
@@ -32,7 +32,13 @@ def _key_from_client(args):
class ClientMatchRuleManager(models.Manager):
"""Manager for client match rules, allows filtering against match logic"""
- def find_ids_for_matches(self, args):
+ def find_match_ids_for_request(self, args):
+ """
+ Finds all match rules that affect the given request. Returns two lists
+ containing the rules that will exclude or include snippets in the
+ response.
+ """
+
cache_key = '%s%s' % (CACHE_RULE_MATCH_PREFIX, _key_from_client(args))
cache_hit = cache.get(cache_key)
@@ -54,6 +60,7 @@ def find_ids_for_matches(self, args):
rules = self._cached_all()
include_ids, exclude_ids = [], []
+ # Check every rule
for rule in rules:
if rule.is_match(args):
if rule.exclude:
@@ -61,6 +68,9 @@ def find_ids_for_matches(self, args):
else:
include_ids.append(str(rule.id))
elif not rule.exclude:
+ # Include rule that doesn't match? Add as an exclude rule
+ # so that snippets can split required matches into multiple
+ # rules and combine them together.
exclude_ids.append(str(rule.id))
cache_hit = (mktime(gmtime()), (include_ids, exclude_ids))
@@ -218,7 +228,7 @@ def find_snippets_with_match_rules(self, args, time_now=None):
preview = ( 'preview' in args ) and args['preview']
include_ids, exclude_ids = \
- ClientMatchRule.objects.find_ids_for_matches(args)
+ ClientMatchRule.objects.find_match_ids_for_request(args)
snippets = self.find_snippets_for_rule_ids(preview, include_ids, exclude_ids)
# Filter for date ranges here, rather than in SQL.
View
5 apps/homesnippets/tests/rules.py
@@ -224,7 +224,10 @@ def test_regex_rules(self):
})
def test_unmatched_inclusion_rules(self):
- """Exercise inclusion rules that don't match"""
+ """
+ Test to ensure that if an inclusion rule does not match, the associated
+ snippet is not sent.
+ """
rules = self.setup_rules({
'fields': ('name', 'version', 'exclude'),
Please sign in to comment.
Something went wrong with that request. Please try again.