class TestColorField(amo.tests.TestCase): def setUp(self): self.field = ColorField() def test_validation_letters_after_f(self): with self.assertRaises(exceptions.ValidationError): self.field.validate('#GGGGGG', None) def test_validation_too_short(self): with self.assertRaises(exceptions.ValidationError): self.field.validate('#00000', None) def test_validation_no_pound(self): with self.assertRaises(exceptions.ValidationError): self.field.validate('FF00FF', None) def should_pass(self, val): try: self.field.validate(val, None) except exceptions.ValidationError: self.fail('Value "%s" should pass validation.') def test_validation_passes(self): for value in ['#010101', '#FF00FF', '#FFFFFF']: self.should_pass(value)
class FeedApp(amo.models.ModelBase): """ Model for "Custom Featured Apps", a feed item highlighting a single app and some additional metadata (e.g. a review or a screenshot). """ app = models.ForeignKey(Webapp) feedapp_type = models.CharField(choices=FEEDAPP_TYPES, max_length=30) description = PurifiedField() slug = SlugField(max_length=30, unique=True) background_color = ColorField(null=True) # Optionally linked to a Preview (screenshot or video). preview = models.ForeignKey(Preview, null=True, blank=True) # Optionally linked to a pull quote. pullquote_attribution = models.CharField(max_length=50, null=True, blank=True) pullquote_rating = models.PositiveSmallIntegerField(null=True, blank=True, validators=[validate_rating]) pullquote_text = PurifiedField(null=True) image_hash = models.CharField(default=None, max_length=8, null=True, blank=True) class Meta: db_table = 'mkt_feed_app' def clean(self): """ Require `pullquote_text` if `pullquote_rating` or `pullquote_attribution` are set. """ if not self.pullquote_text and (self.pullquote_rating or self.pullquote_attribution): raise ValidationError('Pullquote text required if rating or ' 'attribution is defined.') super(FeedApp, self).clean() def image_path(self): return os.path.join(settings.FEATURED_APP_BG_PATH, str(self.pk / 1000), 'featured_app_%s.png' % (self.pk,)) @property def has_image(self): return bool(self.image_hash)
class FeedApp(BaseFeedImage, ModelBase): """ Model for "Custom Featured Apps", a feed item highlighting a single app and some additional metadata (e.g. a review or a screenshot). """ app = models.ForeignKey(Webapp) description = PurifiedField() slug = models.CharField(max_length=30, unique=True) background_color = ColorField(null=True) type = models.CharField(choices=FEEDAPP_TYPE_CHOICES, max_length=30) # Optionally linked to a Preview (screenshot or video). preview = models.ForeignKey(Preview, null=True, blank=True) # Optionally linked to a pull quote. pullquote_attribution = models.CharField(max_length=50, null=True, blank=True) pullquote_rating = models.PositiveSmallIntegerField( null=True, blank=True, validators=[validate_rating]) pullquote_text = PurifiedField(null=True) class Meta: db_table = 'mkt_feed_app' @classmethod def get_indexer(self): return indexers.FeedAppIndexer def clean(self): """ Require `pullquote_text` if `pullquote_rating` or `pullquote_attribution` are set. """ if not self.pullquote_text and (self.pullquote_rating or self.pullquote_attribution): raise ValidationError('Pullquote text required if rating or ' 'attribution is defined.') super(FeedApp, self).clean() def image_path(self, suffix=''): return os.path.join( settings.FEATURED_APP_BG_PATH, str(self.pk / 1000), 'featured_app{suffix}_{pk}.png'.format(suffix=suffix, pk=self.pk))
class FeedApp(amo.models.ModelBase): """ Thin wrapper around the Webapp class that allows single apps to be featured on the feed. """ app = models.ForeignKey(Webapp) feedapp_type = models.CharField(choices=FEEDAPP_TYPES, max_length=30) description = PurifiedField() slug = SlugField(max_length=30) background_color = ColorField(null=True) has_image = models.BooleanField(default=False) # Optionally linked to a Preview (screenshot or video). preview = models.ForeignKey(Preview, null=True, blank=True) # Optionally linked to a pull quote. pullquote_rating = models.PositiveSmallIntegerField( null=True, blank=True, validators=[validate_rating]) pullquote_text = PurifiedField(null=True) pullquote_attribution = PurifiedField(null=True) class Meta: db_table = 'mkt_feed_app' def clean(self): """ Require `pullquote_text` if `pullquote_rating` or `pullquote_attribution` are set. """ if not self.pullquote_text and (self.pullquote_rating or self.pullquote_attribution): raise ValidationError('Pullquote text required if rating or ' 'attribution is defined.') super(FeedApp, self).clean() def image_path(self): return os.path.join(settings.FEATURED_APP_BG_PATH, str(self.pk / 1000), 'featured_app_%s.png' % (self.pk, ))
def setUp(self): self.field = ColorField()