示例#1
0
文件: models.py 项目: xrile/fjord
class TriggerRule(ModelBase):
    # Trigger properties
    slug = models.SlugField(
        unique=True,
        help_text=(
            u'Required: One-word unique slug for this trigger rule. Used '
            u'in redirection urls and GA event tracking. Users will see '
            u'this. Do not change once it is live!'))
    title = models.CharField(
        max_length=255,
        help_text=(
            u'Required: Title of the suggestion--best to keep it a short '
            u'action phrase.'))
    description = models.TextField(
        help_text=(u'Required: Summary of suggestion--best to keep it a short '
                   u'paragraph.'))
    url = models.URLField(
        help_text=(u'Required: URL for the suggestion. This should be '
                   u'locale-independent if possible.'))
    sortorder = models.IntegerField(
        default=5,
        help_text=(
            u'Required: Allows you to dictate which trigger rules are more '
            u'important and thus show up first in a list of trigger rules '
            u'suggestions.'))
    is_enabled = models.BooleanField(default=False)

    # Trigger filters
    locales = ListField(blank=True, help_text=u'Locales to match.')
    products = models.ManyToManyField(Product,
                                      blank=True,
                                      help_text=u'Products to match.')
    keywords = ListField(blank=True,
                         help_text=u'Key words and phrases to match.')
    versions = ListField(
        blank=True,
        help_text=(
            u'Versions to match. Allows for prefix matches for strings that '
            u'end in "*".'))
    url_exists = models.NullBooleanField(
        default=None,
        blank=True,
        help_text=u'Has a url, does not have a url or do not care.')

    objects = TriggerRuleManager()

    def get_matcher(self):
        return TriggerRuleMatcher(
            locales=self.locales,
            product_names=[prod.db_name for prod in self.products.all()],
            versions=self.versions,
            keywords=self.keywords,
            url_exists=self.url_exists)

    def __unicode__(self):
        return u'{0}'.format(self.title)
示例#2
0
 def test_get_prep_value(self):
     tests = [
         # test data, expected
         ([], u'[]'),
         ([1, 2], u'[1, 2]'),
         ([u'a', u'b'], u"[u'a', u'b']")
     ]
     field = ListField()
     for testcase, expected in tests:
         assert field.get_prep_value(testcase), expected
示例#3
0
 def test_get_prep_value(self):
     tests = [
         # test data, expected
         ([], u'[]'),
         ([1, 2], u'[1, 2]'),
         ([u'a', u'b'], u"[u'a', u'b']")
     ]
     field = ListField()
     for testcase, expected in tests:
         assert field.get_prep_value(testcase), expected
示例#4
0
 def test_to_python(self):
     tests = [
         # test data, expected
         (None, []),
         ([], []),
         ([1, 2, 3], [1, 2, 3]),
         (u'[1, 2, 3]', [1, 2, 3]),
         (u"[u'a', u'b', u'c']", [u'a', u'b', u'c'])
     ]
     field = ListField()
     for testcase, expected in tests:
         assert field.to_python(testcase) == expected
示例#5
0
 def test_to_python(self):
     tests = [
         # test data, expected
         (None, []),
         ([], []),
         ([1, 2, 3], [1, 2, 3]),
         (u'[1, 2, 3]', [1, 2, 3]),
         (u"[u'a', u'b', u'c']", [u'a', u'b', u'c'])
     ]
     field = ListField()
     for testcase, expected in tests:
         assert field.to_python(testcase) == expected
示例#6
0
 def test_to_python_raises_validationerror_on_syntaxerror(self):
     field = ListField()
     with pytest.raises(ValidationError):
         field.to_python('abc')
示例#7
0
 def test_to_python_non_list_raises_validationerror_on_assert(self):
     field = ListField()
     with pytest.raises(ValidationError):
         field.to_python(42)