diff --git a/django/contrib/webdesign/__init__.py b/django/contrib/webdesign/__init__.py index 2340116bc8..635ed88a67 100644 --- a/django/contrib/webdesign/__init__.py +++ b/django/contrib/webdesign/__init__.py @@ -1 +1,11 @@ +import warnings + +from django.utils.deprecation import RemovedInDjango20Warning + default_app_config = 'django.contrib.webdesign.apps.WebDesignConfig' + +warnings.warn( + "django.contrib.webdesign will be removed in Django 2.0. The " + "{% lorem %} tag is now included in the built-in tags.", + RemovedInDjango20Warning +) diff --git a/django/contrib/webdesign/templatetags/webdesign.py b/django/contrib/webdesign/templatetags/webdesign.py index 2ff73e75d0..d7842df534 100644 --- a/django/contrib/webdesign/templatetags/webdesign.py +++ b/django/contrib/webdesign/templatetags/webdesign.py @@ -1,71 +1,6 @@ -from __future__ import unicode_literals - -from django.contrib.webdesign.lorem_ipsum import words, paragraphs from django import template +from django.template.defaulttags import lorem register = template.Library() - -class LoremNode(template.Node): - def __init__(self, count, method, common): - self.count, self.method, self.common = count, method, common - - def render(self, context): - try: - count = int(self.count.resolve(context)) - except (ValueError, TypeError): - count = 1 - if self.method == 'w': - return words(count, common=self.common) - else: - paras = paragraphs(count, common=self.common) - if self.method == 'p': - paras = ['
%s
' % p for p in paras] - return '\n\n'.join(paras) - - -@register.tag -def lorem(parser, token): - """ - Creates random Latin text useful for providing test data in templates. - - Usage format:: - - {% lorem [count] [method] [random] %} - - ``count`` is a number (or variable) containing the number of paragraphs or - words to generate (default is 1). - - ``method`` is either ``w`` for words, ``p`` for HTML paragraphs, ``b`` for - plain-text paragraph blocks (default is ``b``). - - ``random`` is the word ``random``, which if given, does not use the common - paragraph (starting "Lorem ipsum dolor sit amet, consectetuer..."). - - Examples: - - * ``{% lorem %}`` will output the common "lorem ipsum" paragraph - * ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph - and two random paragraphs each wrapped in HTML ```` tags - * ``{% lorem 2 w random %}`` will output two random latin words - """ - bits = list(token.split_contents()) - tagname = bits[0] - # Random bit - common = bits[-1] != 'random' - if not common: - bits.pop() - # Method bit - if bits[-1] in ('w', 'p', 'b'): - method = bits.pop() - else: - method = 'b' - # Count bit - if len(bits) > 1: - count = bits.pop() - else: - count = '1' - count = parser.compile_filter(count) - if len(bits) != 1: - raise template.TemplateSyntaxError("Incorrect format for %r tag" % tagname) - return LoremNode(count, method, common) +register.tag(lorem) diff --git a/django/contrib/webdesign/tests.py b/django/contrib/webdesign/tests.py index e4d9635522..239af194b7 100644 --- a/django/contrib/webdesign/tests.py +++ b/django/contrib/webdesign/tests.py @@ -3,19 +3,11 @@ from __future__ import unicode_literals import unittest -from django.contrib.webdesign.lorem_ipsum import paragraphs, words from django.template import loader, Context class WebdesignTest(unittest.TestCase): - def test_words(self): - self.assertEqual(words(7), 'lorem ipsum dolor sit amet consectetur adipisicing') - - def test_paragraphs(self): - self.assertEqual(paragraphs(1), - ['Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.']) - def test_lorem_tag(self): t = loader.get_template_from_string("{% load webdesign %}{% lorem 3 w %}") self.assertEqual(t.render(Context({})), diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index c08f869e5f..ceeb7f3335 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -19,6 +19,7 @@ from django.template.smartif import IfParser, Literal from django.template.defaultfilters import date from django.utils.deprecation import RemovedInDjango20Warning from django.utils.encoding import force_text, smart_text +from django.utils.lorem_ipsum import words, paragraphs from django.utils.safestring import mark_safe from django.utils.html import format_html from django.utils import six @@ -324,6 +325,24 @@ class IfNode(Node): return '' +class LoremNode(Node): + def __init__(self, count, method, common): + self.count, self.method, self.common = count, method, common + + def render(self, context): + try: + count = int(self.count.resolve(context)) + except (ValueError, TypeError): + count = 1 + if self.method == 'w': + return words(count, common=self.common) + else: + paras = paragraphs(count, common=self.common) + if self.method == 'p': + paras = ['
%s
' % p for p in paras] + return '\n\n'.join(paras) + + class RegroupNode(Node): def __init__(self, target, expression, var_name): self.target, self.expression = target, expression @@ -1116,6 +1135,53 @@ def load(parser, token): return LoadNode() +@register.tag +def lorem(parser, token): + """ + Creates random Latin text useful for providing test data in templates. + + Usage format:: + + {% lorem [count] [method] [random] %} + + ``count`` is a number (or variable) containing the number of paragraphs or + words to generate (default is 1). + + ``method`` is either ``w`` for words, ``p`` for HTML paragraphs, ``b`` for + plain-text paragraph blocks (default is ``b``). + + ``random`` is the word ``random``, which if given, does not use the common + paragraph (starting "Lorem ipsum dolor sit amet, consectetuer..."). + + Examples: + + * ``{% lorem %}`` will output the common "lorem ipsum" paragraph + * ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph + and two random paragraphs each wrapped in HTML ```` tags + * ``{% lorem 2 w random %}`` will output two random latin words + """ + bits = list(token.split_contents()) + tagname = bits[0] + # Random bit + common = bits[-1] != 'random' + if not common: + bits.pop() + # Method bit + if bits[-1] in ('w', 'p', 'b'): + method = bits.pop() + else: + method = 'b' + # Count bit + if len(bits) > 1: + count = bits.pop() + else: + count = '1' + count = parser.compile_filter(count) + if len(bits) != 1: + raise TemplateSyntaxError("Incorrect format for %r tag" % tagname) + return LoremNode(count, method, common) + + @register.tag def now(parser, token): """ diff --git a/django/contrib/webdesign/lorem_ipsum.py b/django/utils/lorem_ipsum.py similarity index 100% rename from django/contrib/webdesign/lorem_ipsum.py rename to django/utils/lorem_ipsum.py diff --git a/docs/ref/contrib/webdesign.txt b/docs/ref/contrib/webdesign.txt index 0e739484b9..f3c8ebe402 100644 --- a/docs/ref/contrib/webdesign.txt +++ b/docs/ref/contrib/webdesign.txt @@ -6,51 +6,7 @@ django.contrib.webdesign :synopsis: Helpers and utilities targeted primarily at Web *designers* rather than Web *developers*. -The ``django.contrib.webdesign`` package, part of the -:doc:`"django.contrib" add-ons `, provides various Django -helpers that are particularly useful to Web *designers* (as opposed to -developers). +.. deprecated:: 1.8 -At present, the package contains only a single template tag. If you have ideas -for Web-designer-friendly functionality in Django, please -:doc:`suggest them `. - -Template tags -============= - -To use these template tags, add ``'django.contrib.webdesign'`` to your -:setting:`INSTALLED_APPS` setting. Once you've done that, use -``{% load webdesign %}`` in a template to give your template access to the tags. - - -lorem -===== - -Displays random "lorem ipsum" Latin text. This is useful for providing sample -data in templates. - -Usage:: - - {% lorem [count] [method] [random] %} - -The ``{% lorem %}`` tag can be used with zero, one, two or three arguments. -The arguments are: - -=========== ============================================================= -Argument Description -=========== ============================================================= -``count`` A number (or variable) containing the number of paragraphs or - words to generate (default is 1). -``method`` Either ``w`` for words, ``p`` for HTML paragraphs or ``b`` - for plain-text paragraph blocks (default is ``b``). -``random`` The word ``random``, which if given, does not use the common - paragraph ("Lorem ipsum dolor sit amet...") when generating - text. -=========== ============================================================= - -Examples: - -* ``{% lorem %}`` will output the common "lorem ipsum" paragraph. -* ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph - and two random paragraphs each wrapped in HTML ``
`` tags. -* ``{% lorem 2 w random %}`` will output two random Latin words. + The package contained only a single template tag and it has been moved + to the built-in tags (:ttag:`lorem`). diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 97860d1463..242f2c2c9a 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -726,6 +726,44 @@ and ``bar`` will be loaded from ``somelibrary``:: See :doc:`Custom tag and filter libraries ` for more information. +.. templatetag:: lorem + +lorem +^^^^^ + +.. versionadded:: 1.8 + + The tag was previously located in :mod:`django.contrib.webdesign`. + +Displays random "lorem ipsum" Latin text. This is useful for providing sample +data in templates. + +Usage:: + + {% lorem [count] [method] [random] %} + +The ``{% lorem %}`` tag can be used with zero, one, two or three arguments. +The arguments are: + +=========== ============================================================= +Argument Description +=========== ============================================================= +``count`` A number (or variable) containing the number of paragraphs or + words to generate (default is 1). +``method`` Either ``w`` for words, ``p`` for HTML paragraphs or ``b`` + for plain-text paragraph blocks (default is ``b``). +``random`` The word ``random``, which if given, does not use the common + paragraph ("Lorem ipsum dolor sit amet...") when generating + text. +=========== ============================================================= + +Examples: + +* ``{% lorem %}`` will output the common "lorem ipsum" paragraph. +* ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph + and two random paragraphs each wrapped in HTML ``
`` tags. +* ``{% lorem 2 w random %}`` will output two random Latin words. + .. templatetag:: now now diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index bbd573d782..30c62f845d 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -563,3 +563,10 @@ and will be removed in Django 1.9. The function has been informally marked as "Deprecated" for some time. Replace ``resolve_variable(path, context)`` with ``django.template.Variable(path).resolve(context)``. + +``django.contrib.webdesign`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +It provided the :ttag:`lorem` template tag which is now included in the +built-in tags. Simply remove ``'django.contrib.webdesign'`` from +:setting:`INSTALLED_APPS` and ``{% load webdesign %}`` from your templates. diff --git a/tests/runtests.py b/tests/runtests.py index e0d5a173f2..aa8313036c 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -135,6 +135,12 @@ def setup(verbosity, test_labels): handler = logging.StreamHandler() logger.addHandler(handler) + warnings.filterwarnings( + 'ignore', + 'django.contrib.webdesign will be removed in Django 2.0.', + RemovedInDjango20Warning + ) + # Load all the ALWAYS_INSTALLED_APPS. django.setup() diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index 4ec6815710..e785962c21 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -1351,6 +1351,8 @@ class TemplateTests(TestCase): 'load11': ("{% load subpackage.echo_invalid %}", {}, template.TemplateSyntaxError), 'load12': ("{% load subpackage.missing %}", {}, template.TemplateSyntaxError), + 'lorem1': ("{% lorem 3 w %}", {}, "lorem ipsum dolor"), + ### I18N ################################################################## # {% spaceless %} tag diff --git a/tests/utils_tests/test_lorem_ipsum.py b/tests/utils_tests/test_lorem_ipsum.py new file mode 100644 index 0000000000..9d0a2d6b2a --- /dev/null +++ b/tests/utils_tests/test_lorem_ipsum.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +import unittest + +from django.utils.lorem_ipsum import paragraphs, words + + +class WebdesignTest(unittest.TestCase): + + def test_words(self): + self.assertEqual(words(7), 'lorem ipsum dolor sit amet consectetur adipisicing') + + def test_paragraphs(self): + self.assertEqual(paragraphs(1), + ['Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'])