-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use metaclass to add images and links to blocks
- Loading branch information
Sebastian Vetter
committed
Nov 28, 2013
1 parent
5f889bd
commit c05e0af
Showing
6 changed files
with
88 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django.db import models | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from ...assets.fields import AssetKey | ||
|
||
|
||
class MultiImageMeta(models.base.ModelBase): | ||
|
||
def __init__(cls, name, bases, nmspc): | ||
super(MultiImageMeta, cls).__init__(name, bases, nmspc) | ||
if hasattr(cls, 'num_images'): | ||
cls.add_image_fields() | ||
|
||
def add_image_fields(cls): | ||
if not getattr(cls, 'image_field_name', None): | ||
cls.image_field_name = "image_%d" | ||
|
||
for idx in range(1, cls.num_images + 1): | ||
cls.add_to_class( | ||
cls.image_field_name % idx, | ||
AssetKey('assets.ImageAsset', verbose_name=_("Image %d" % idx), | ||
related_name="+", blank=True, null=True)) | ||
|
||
|
||
class MultiLinkMeta(models.base.ModelBase): | ||
|
||
def __init__(cls, name, bases, nmspc): | ||
super(MultiLinkMeta, cls).__init__(name, bases, nmspc) | ||
if hasattr(cls, 'num_links'): | ||
cls.add_link_fields() | ||
|
||
def add_link_fields(cls): | ||
if not getattr(cls, 'link_field_name', None): | ||
cls.link_field_name = "link_url_%d" | ||
|
||
for idx in range(1, cls.num_links + 1): | ||
cls.add_to_class( | ||
cls.link_field_name % idx, | ||
models.CharField(_("Link URL %d" % idx), max_length=500, | ||
blank=True, null=True)) | ||
|
||
|
||
class MultiImageLinkMeta(MultiImageMeta, MultiLinkMeta): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.db.models import get_model | ||
from fancypages.test.testcases import BlockTestCase | ||
|
||
CarouselBlock = get_model('fancypages', 'CarouselBlock') | ||
|
||
|
||
class TestCarouselBlock(BlockTestCase): | ||
|
||
def test_has_10_image_attributes(self): | ||
carousel = CarouselBlock() | ||
for idx in range(1, 11): | ||
self.assertTrue(hasattr(carousel, carousel.image_field_name % idx)) | ||
|
||
def test_has_10_link_attributes(self): | ||
carousel = CarouselBlock() | ||
for idx in range(1, 11): | ||
self.assertTrue(hasattr(carousel, carousel.link_field_name % idx)) | ||
|
||
def test_can_be_rendered_in_template(self): | ||
html = self.get_rendered_block(CarouselBlock()) | ||
self.assertIn('flexslider', html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[pytest] | ||
addopts = -m 'not integration' | ||
addopts= -m 'not integration' | ||
pep8ignore= | ||
conftest.py ALL | ||
|
||
|