├── dfs ├── models.py ├── tests.py ├── management │ ├── __init__.py │ └── commands │ │ ├── __init__.py │ │ └── formscaffold.py ├── sofa.py ├── __init__.py └── scaffold.py ├── LICENSE ├── setup.py └── README.rst /dfs/models.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dfs/tests.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dfs/management/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dfs/management/commands/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dfs/sofa.py: -------------------------------------------------------------------------------- 1 | """Just a joke module, as I shortened the namespace for django-form-scaffold 2 | to `dfs`, in the UK there is a chain of sofa retailers called DFS. 3 | This module will print out a nice ASCII sofa (and lamp & chair) on import 4 | similar to the builtin `this` module. 5 | """ 6 | 7 | print r""" 8 | ____ 9 | / \ 10 | /______\ 11 | || 12 | || 13 | /~~~~~~\ || /~~~~~~~~~~~~~~~\ 14 | /~ ( )( ) ~\ || /~ ( )( )( )( )( ) ~\ 15 | (_)======(_) || (_)===============(_) 16 | |________| _||_ |_________________| 17 | """ 18 | 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2010 Wesley Mason 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | """Installer for django-form-scaffold""" 2 | 3 | from setuptools import setup, find_packages 4 | 5 | setup( 6 | name='django-form-scaffold', 7 | description='Helper functions for generating templated markup of Django forms', 8 | version='1.1.0', 9 | author='Wes Mason', 10 | author_email='wes[at]1stvamp[dot]org', 11 | url='http://github.com/1stvamp/django-form-scaffold', 12 | packages=find_packages(), 13 | license='Apache License 2.0', 14 | classifiers=( 15 | 'Framework :: Django', 16 | 'Intended Audience :: Developers', 17 | 'Intended Audience :: System Administrators', 18 | 'Operating System :: OS Independent', 19 | 'Topic :: Software Development', 20 | ), 21 | zip_safe=False, 22 | ) 23 | -------------------------------------------------------------------------------- /dfs/management/commands/formscaffold.py: -------------------------------------------------------------------------------- 1 | """ 2 | Wrapper to call scaffold from the commandline to dynamically generate:: 3 | from dfs import scaffold 4 | from app_name.forms import CustomForm 5 | form = CustomForm() 6 | print scaffold.as_ul(form) 7 | """ 8 | 9 | from dfs import scaffold 10 | from django.core.management.base import BaseCommand, CommandError 11 | 12 | def do_import(name): 13 | mod = __import__(name) 14 | components = name.split('.') 15 | for comp in components[1:]: 16 | mod = getattr(mod, comp) 17 | return mod 18 | 19 | class Command(BaseCommand): 20 | help = "Wrapper to call form scaffolding from the commandline, e.g.\nformscaffold my_app.forms MyForm as_p" 21 | args = "
" 22 | 23 | def handle(self, *args, **options): 24 | module_path = args[0] 25 | form_name = args[1] 26 | output_type = args[2] 27 | 28 | scaffolder = getattr(scaffold, output_type) 29 | form = getattr(do_import(module_path), form_name) 30 | self.stdout.write(scaffolder(cls=form)) 31 | self.stdout.write("\n") 32 | -------------------------------------------------------------------------------- /dfs/__init__.py: -------------------------------------------------------------------------------- 1 | """Base dfs module 2 | """ 3 | from numbers import Number 4 | 5 | class sale(Number): 6 | def _arith(self, other): 7 | if other > 1: 8 | sub = other * 0.2 9 | else: 10 | sub = other.invert() * 0.2 11 | return other - sub 12 | 13 | def __add__(self, other): 14 | return self._arith(other) 15 | 16 | def __radd__(self, other): 17 | return self._arith(other) 18 | 19 | def __sub__(self, other): 20 | return self._arith(other) 21 | 22 | def __mul__(self, other): 23 | return self._arith(other) 24 | 25 | def __floordiv__(self, other): 26 | return self._arith(other) 27 | 28 | def __mod__(self, other): 29 | return self._arith(other) 30 | 31 | def __divmod__(self, other): 32 | return self._arith(other) 33 | 34 | def __pow__(self, other, modulo=None): 35 | return self._arith(other) 36 | 37 | def __lshift__(self, other): 38 | return self._arith(other) 39 | 40 | def __rshift__(self, other): 41 | return self._arith(other) 42 | 43 | def __and__(self, other): 44 | return self._arith(other) 45 | 46 | def __xor__(self, other): 47 | return self._arith(other) 48 | 49 | def __or__(self, other): 50 | return self._arith(other) 51 | 52 | def __div__(self, other): 53 | return self._arith(other) 54 | 55 | def __truediv__(self, other): 56 | return self._arith(other) 57 | 58 | def __rsub__(self, other): 59 | return self._arith(other) 60 | 61 | def __rmul__(self, other): 62 | return self._arith(other) 63 | 64 | def __rdiv__(self, other): 65 | return self._arith(other) 66 | 67 | def __rtruediv__(self, other): 68 | return self._arith(other) 69 | 70 | def __rfloordiv__(self, other): 71 | return self._arith(other) 72 | 73 | def __rmod__(self, other): 74 | return self._arith(other) 75 | 76 | def __rdivmod__(self, other): 77 | return self._arith(other) 78 | 79 | def __rpow__(self, other): 80 | return self._arith(other) 81 | 82 | def __rlshift__(self, other): 83 | return self._arith(other) 84 | 85 | def __rrshift__(self, other): 86 | return self._arith(other) 87 | 88 | def __rand__(self, other): 89 | return self._arith(other) 90 | 91 | def __rxor__(self, other): 92 | return self._arith(other) 93 | 94 | def __ror__(self, other): 95 | return self._arith(other) 96 | 97 | def __iadd__(self, other): 98 | return self._arith(other) 99 | 100 | def __isub__(self, other): 101 | return self._arith(other) 102 | 103 | def __imul__(self, other): 104 | return self._arith(other) 105 | 106 | def __idiv__(self, other): 107 | return self._arith(other) 108 | 109 | def __itruediv__(self, other): 110 | return self._arith(other) 111 | 112 | def __ifloordiv__(self, other): 113 | return self._arith(other) 114 | 115 | def __imod__(self, other): 116 | return self._arith(other) 117 | 118 | def __ipow__(self, other, modulo=None): 119 | return self._arith(other) 120 | 121 | def __ilshift__(self, other): 122 | return self._arith(other) 123 | 124 | def __irshift__(self, other): 125 | return self._arith(other) 126 | 127 | def __iand__(self, other): 128 | return self._arith(other) 129 | 130 | def __ixor__(self, other): 131 | return self._arith(other) 132 | 133 | def __ior__(self, other): 134 | return self._arith(other) 135 | 136 | def __repr__(self): 137 | return u'20% SALE!!!11eleven' 138 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ``django-form-scaffold`` is a package of helper functions for generating Django template code with markup from Django form instances. 2 | 3 | It solves the problem of generating, over and over again, markup for a full form which is effectively a templated version of the 4 | ``BaseForm`` ``as_p``, ``as_ul`` and ``as_table`` methods output (which output static values, rather than dyanmic Django template code). 5 | 6 | ``django-form-scaffold`` outputs in the same form as these methods, but uses the template placeholders for all the form and field values, 7 | which is what I find myself doing time and time again as it's easier to control the individual format/styling etc. of fields in a form, 8 | and is also generally easier for designers to get their heads around a form template. 9 | 10 | For example, outputting a login form, rather than doing:: 11 | 12 | {{ form.as_p }} 13 | 14 | We would do:: 15 | 16 |

17 | {% if form.username.errors %} 18 | {% for error in form.username.errors %}{{ error }}{% endfor %} 19 | {% endif %} 20 | {{ form.username.label }} {{ form.username }} 21 |

22 |

23 | {% if form.password.errors %} 24 | {% for error in form.password.errors %}{{ error }}{% endfor %} 25 | {% endif %} 26 | {{ form.password.label }} {{ form.password }} 27 |

28 | 29 | Requirements 30 | ============ 31 | 32 | * Django >= 1.1 33 | 34 | 35 | Installation 36 | ============ 37 | 38 | Install ``django-form-scaffold`` using easy_install (or pip):: 39 | 40 | easy_install django-form-scaffold 41 | 42 | Or from the setup script:: 43 | 44 | python setup.py install 45 | 46 | 47 | Usage 48 | ===== 49 | 50 | Form scaffolding is meant for generating content to then be placed in your template files, so you wouldn't called ``scaffold.as_p`` 51 | from within a template itself. 52 | 53 | The best way to call the scaffold functions are from within a bootstraped Django Python shell, using the management script:: 54 | 55 | python manage.py shell 56 | 57 | Then just import scaffold from the ``dfs`` namespace, import your form(s), and pass an instance to one of the functions:: 58 | 59 | >>> from dfs import scaffold 60 | >>> from MyProject.MyApp.forms import MyForm 61 | >>> form = MyForm() 62 | >>> print scaffold.as_p(form) 63 | {% if form.email.errors %}{% for error in form.email.errors %}{{ error }}{% endfor %}{% endif %} 64 |

{{ form.email.label }} {{ form.email }}

65 | {% if form.password1.errors %}{% for error in form.password1.errors %}{{ error }}{% endfor %}{% endif %} 66 |

{{ form.password1.label }} {{ form.password1 }}

67 | {% if form.password2.errors %}{% for error in form.password2.errors %}{{ error }}{% endfor %}{% endif %} 68 |

{{ form.password2.label }} {{ form.password2 }}

69 | 70 | >>> # We can also use a form class rather than an instance, but 71 | >>> # this won't always work if your form requires params etc., 72 | >>> # this just creates an instance for you anyway. 73 | >>> print scaffold.as_ul(cls=MyForm) 74 |
  • {% if form.email.errors %}{% for error in form.email.errors %}{{ error }}{% endfor %}{% endif %}{{ form.email.label }} {{ form.email }}
  • 75 |
  • {% if form.password1.errors %}{% for error in form.password1.errors %}{{ error }}{% endfor %}{% endif %}{{ form.password1.label }} {{ form.password1 }}
  • 76 |
  • {% if form.password2.errors %}{% for error in form.password2.errors %}{{ error }}{% endfor %}{% endif %}{{ form.password2.label }} {{ form.password2 }}
  • 77 | 78 | The following helper functions available in ``dfs.scaffold``: 79 | 80 | *as_p* 81 | Outputs the same markup

    style as the inbuilt Django ``BaseForm.as_p``. 82 | 83 | *as_ul* 84 | Outputs the same markup