def test_widget(self): class HelloWorld(Widget): def render(self, context, options): return u'Hello world!' registry.register('helloworld', HelloWorld()) out = Template("{% load widgeter %}" "{% widget \"helloworld\" %}").render(Context()) self.assertIn('Hello world!', out)
def test_widget(self): class HelloWorld(Widget): def render(self, context, options): return u'Hello world!' registry.register('helloworld', HelloWorld()) out = Template( "{% load widgeter %}" "{% widget \"helloworld\" %}" ).render(Context()) self.assertIn('Hello world!', out)
def test_widget_block(self): class HelloWorld(Widget): block = "testing" def render(self, context, options): return u'Hello world!' registry.register('helloworld', HelloWorld()) class FooBar(Widget): block = "not_testing" def render(self, context, options): return u'Bye bye world!' registry.register('foobar', FooBar()) out = Template("{% load widgeter %}" "{% widget_block \"testing\" %}").render(Context()) self.assertIn('Hello world!', out) self.assertNotIn('Bye bye world!', out)
def test_widget_block(self): class HelloWorld(Widget): block = "testing" def render(self, context, options): return u'Hello world!' registry.register('helloworld', HelloWorld()) class FooBar(Widget): block = "not_testing" def render(self, context, options): return u'Bye bye world!' registry.register('foobar', FooBar()) out = Template( "{% load widgeter %}" "{% widget_block \"testing\" %}" ).render(Context()) self.assertIn('Hello world!', out) self.assertNotIn('Bye bye world!', out)
def test_get_by_block(self): class Test1(Widget): block = 'testing' test1 = Test1() registry.register('test1', test1) class Test2(Widget): block = 'not_testing' test2 = Test2() registry.register('test2', test2) class Test3(Widget): block = 'testing' test3 = Test3() registry.register('test3', test3) widgets = registry.get_by_block('testing') self.assertIn(test1, widgets) self.assertNotIn(test2, widgets) self.assertIn(test3, widgets)
from widgeter.base import Widget from widgeter.registry import registry from news.models import NewsItem class NewsWidget(Widget): template = 'news/home_widget.html' block = 'home' priority = 1 def get_context(self, context, user): return { 'recent_news': NewsItem.objects.filter(published=True).order_by('-publish_date')[:3] } registry.register('news', NewsWidget())
def test_register_dup(self): widget = 'widget' registry.register('widget', widget) with self.assertRaises(KeyError): registry.register('widget', widget)
def test_register_valid(self): widget = 'widget' registry.register('widget', widget) self.assertIn(widget, registry.widgets) self.assertEqual(widget, registry.get('widget'))
from widgeter.base import Widget from widgeter.registry import registry from news.models import NewsItem class NewsWidget(Widget): template = 'news/home_widget.html' block = 'home' priority = 1 def get_context(self, context, user): news = NewsItem.objects.filter(published=True).order_by('-publish_date') sticky_news = news.filter(sticky=True)[:6] if len(sticky_news) >= 6: recent_news = news.none() else: recent_news = news.filter(sticky=False)[:6-len(sticky_news)] return { 'recent_news': recent_news, 'sticky_news': sticky_news } registry.register('news', NewsWidget())