Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f0cbb8

Browse files
author
Dmitry Dygalo
committedAug 19, 2015
Initial approach for parametrization
1 parent cf2bcec commit 3f0cbb8

File tree

8 files changed

+33
-36
lines changed

8 files changed

+33
-36
lines changed
 

‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ develop:
55
pip install -q -e .
66

77
test: develop
8-
python runtests.py
8+
DJANGO_SETTINGS_MODULE=crispy_forms.tests.test_settings py.test crispy_forms/tests --cov=crispy_forms

‎crispy_forms/layout.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.utils.html import conditional_escape
44

55
from crispy_forms.compatibility import string_types, text_type
6-
from crispy_forms.utils import render_field, flatatt, TEMPLATE_PACK
6+
from crispy_forms.utils import render_field, flatatt, TEMPLATE_PACK, get_template_pack
77

88

99
class LayoutObject(object):
@@ -197,7 +197,7 @@ class Submit(BaseInput):
197197
input_type = 'submit'
198198

199199
def __init__(self, *args, **kwargs):
200-
self.field_classes = 'submit submitButton' if TEMPLATE_PACK == 'uni_form' else 'btn btn-primary'
200+
self.field_classes = 'submit submitButton' if get_template_pack() == 'uni_form' else 'btn btn-primary'
201201
super(Submit, self).__init__(*args, **kwargs)
202202

203203

@@ -212,7 +212,7 @@ class Button(BaseInput):
212212
input_type = 'button'
213213

214214
def __init__(self, *args, **kwargs):
215-
self.field_classes = 'button' if TEMPLATE_PACK == 'uni_form' else 'btn'
215+
self.field_classes = 'button' if get_template_pack() == 'uni_form' else 'btn'
216216
super(Button, self).__init__(*args, **kwargs)
217217

218218

@@ -235,7 +235,7 @@ class Reset(BaseInput):
235235
input_type = 'reset'
236236

237237
def __init__(self, *args, **kwargs):
238-
self.field_classes = 'reset resetButton' if TEMPLATE_PACK == 'uni_form' else 'btn btn-inverse'
238+
self.field_classes = 'reset resetButton' if get_template_pack() == 'uni_form' else 'btn btn-inverse'
239239
super(Reset, self).__init__(*args, **kwargs)
240240

241241

@@ -355,7 +355,7 @@ class Row(Div):
355355
"""
356356

357357
def __init__(self, *args, **kwargs):
358-
self.css_class = 'formRow' if TEMPLATE_PACK == 'uni_form' else 'row'
358+
self.css_class = 'formRow' if get_template_pack() == 'uni_form' else 'row'
359359
super(Row, self).__init__(*args, **kwargs)
360360

361361

‎crispy_forms/templatetags/crispy_forms_field.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.template import loader, Context
99
from django.conf import settings
1010

11-
from crispy_forms.utils import TEMPLATE_PACK
11+
from crispy_forms.utils import TEMPLATE_PACK, get_template_pack
1212

1313
register = template.Library()
1414

@@ -173,7 +173,7 @@ def crispy_addon(field, append="", prepend="", form_show_labels=True):
173173
'form_show_errors': True,
174174
'form_show_labels': form_show_labels,
175175
})
176-
template = loader.get_template('%s/layout/prepended_appended_text.html' % TEMPLATE_PACK)
176+
template = loader.get_template('%s/layout/prepended_appended_text.html' % get_template_pack())
177177
context['crispy_prepended_text'] = prepend
178178
context['crispy_appended_text'] = append
179179

‎crispy_forms/templatetags/crispy_forms_tags.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# We import the filters, so they are available when doing load crispy_forms_tags
1616
from crispy_forms.templatetags.crispy_forms_filters import *
1717

18-
from crispy_forms.utils import TEMPLATE_PACK
18+
from crispy_forms.utils import TEMPLATE_PACK, get_template_pack
1919

2020

2121
class ForLoopSimulator(object):
@@ -76,13 +76,13 @@ class BasicNode(template.Node):
7676
both the form object and parses out the helper string into attributes
7777
that templates can easily handle.
7878
"""
79-
def __init__(self, form, helper, template_pack=TEMPLATE_PACK):
79+
def __init__(self, form, helper, template_pack=None):
8080
self.form = form
8181
if helper is not None:
8282
self.helper = helper
8383
else:
8484
self.helper = None
85-
self.template_pack = template_pack
85+
self.template_pack = template_pack or get_template_pack()
8686

8787
def get_render(self, context):
8888
"""
@@ -249,7 +249,7 @@ def do_uni_form(parser, token):
249249
form = token.pop(1)
250250

251251
helper = None
252-
template_pack = "'%s'" % TEMPLATE_PACK
252+
template_pack = "'%s'" % get_template_pack()
253253

254254
# {% crispy form helper %}
255255
try:

‎crispy_forms/tests/conftest.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
# coding: utf-8
2-
from django.conf import settings
3-
42
import pytest
53

64
from crispy_forms.layout import Layout, Div, Field, Submit, Fieldset, HTML
75

86

9-
def get_skip_mark(*template_packs):
10-
return pytest.mark.skipif(settings.CRISPY_TEMPLATE_PACK not in template_packs,
11-
reason='Requires %s template pack' % ' or '.join(template_packs))
12-
13-
14-
only_uni_form = get_skip_mark('uni_form')
15-
only_bootstrap = get_skip_mark('bootstrap', 'bootstrap3')
16-
only_bootstrap3 = get_skip_mark('bootstrap3')
7+
only_uni_form = pytest.mark.only('uni_form')
8+
only_bootstrap = pytest.mark.only('bootstrap', 'bootstrap3')
9+
only_bootstrap3 = pytest.mark.only('bootstrap3')
1710

1811

1912
@pytest.fixture
@@ -34,3 +27,16 @@ def advanced_layout():
3427
),
3528
'last_name',
3629
)
30+
31+
32+
@pytest.fixture(autouse=True, params=('uni_form', 'bootstrap', 'bootstrap3'))
33+
def template_packs(request, settings):
34+
check_template_pack(request._pyfuncitem._obj, request.param)
35+
settings.CRISPY_TEMPLATE_PACK = request.param
36+
37+
38+
def check_template_pack(function, template_pack):
39+
if hasattr(function, 'only'):
40+
mark = function.only
41+
if template_pack not in mark.args:
42+
pytest.skip('Requires %s template pack' % ' or '.join(mark.args))

‎crispy_forms/tests/test_settings.py

-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,4 @@ def __contains__(self, search):
4848

4949
TEMPLATE_DEBUG = True
5050
TEMPLATE_STRING_IF_INVALID = InvalidVarException()
51-
CRISPY_TEMPLATE_PACK = os.environ['CRISPY_TEMPLATE_PACK']
5251
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )

‎crispy_forms/utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
from .compatibility import text_type, PY2, memoize, SimpleLazyObject
1313

1414

15-
TEMPLATE_PACK = SimpleLazyObject(lambda: getattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap'))
15+
def get_template_pack():
16+
return getattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap')
17+
18+
19+
TEMPLATE_PACK = SimpleLazyObject(get_template_pack)
1620

1721

1822
# By memoizeing we avoid loading the template every time render_field

‎runtests.py

-12
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.