diff --git a/tailbone/forms/__init__.py b/tailbone/forms/__init__.py deleted file mode 100644 index 9982a978..00000000 --- a/tailbone/forms/__init__.py +++ /dev/null @@ -1,39 +0,0 @@ -# -*- coding: utf-8 -*- -################################################################################ -# -# Rattail -- Retail Software Framework -# Copyright © 2010-2017 Lance Edgar -# -# This file is part of Rattail. -# -# Rattail is free software: you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation, either version 3 of the License, or (at your option) any later -# version. -# -# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -# details. -# -# You should have received a copy of the GNU General Public License along with -# Rattail. If not, see . -# -################################################################################ -""" -Forms -""" - -from __future__ import unicode_literals, absolute_import - -from formencode import Schema - -from .core import Form, Field, FieldSet, GenericFieldSet -from .simpleform import SimpleForm, FormRenderer -from .alchemy import AlchemyForm -from .fields import AssociationProxyField -from .renderers import * - -from . import fields -from . import renderers -from . import validators diff --git a/tailbone/forms/alchemy.py b/tailbone/forms/alchemy.py deleted file mode 100644 index 161d549f..00000000 --- a/tailbone/forms/alchemy.py +++ /dev/null @@ -1,117 +0,0 @@ -# -*- coding: utf-8; -*- -################################################################################ -# -# Rattail -- Retail Software Framework -# Copyright © 2010-2017 Lance Edgar -# -# This file is part of Rattail. -# -# Rattail is free software: you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation, either version 3 of the License, or (at your option) any later -# version. -# -# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -# details. -# -# You should have received a copy of the GNU General Public License along with -# Rattail. If not, see . -# -################################################################################ -""" -FormAlchemy Forms -""" - -from __future__ import unicode_literals, absolute_import - -from rattail.core import Object - -import formalchemy as fa -from pyramid.renderers import render -from webhelpers2.html import HTML, tags - -from tailbone.db import Session - - -class TemplateEngine(fa.templates.TemplateEngine): - """ - Mako template engine for FormAlchemy. - """ - - def render(self, template, prefix='/forms/', suffix='.mako', **kwargs): - template = ''.join((prefix, template, suffix)) - return render(template, kwargs) - - -class AlchemyForm(Object): - """ - Form to contain a :class:`formalchemy.FieldSet` instance. - """ - id = None - create_label = "Create" - update_label = "Save" - - allow_successive_creates = False - - def __init__(self, request, fieldset, session=None, csrf_field='_csrf', **kwargs): - super(AlchemyForm, self).__init__(**kwargs) - self.request = request - self.fieldset = fieldset - self.session = session - self.csrf_field = csrf_field - - def _get_readonly(self): - return self.fieldset.readonly - def _set_readonly(self, val): - self.fieldset.readonly = val - readonly = property(_get_readonly, _set_readonly) - - @property - def successive_create_label(self): - return "%s and continue" % self.create_label - - def csrf(self, name=None): - """ - NOTE: this method was copied from `pyramid_simpleform.FormRenderer` - - Returns the CSRF hidden input. Creates new CSRF token - if none has been assigned yet. - - The name of the hidden field is **_csrf** by default. - """ - name = name or self.csrf_field - - token = self.request.session.get_csrf_token() - if token is None: - token = self.request.session.new_csrf_token() - - return tags.hidden(name, value=token) - - def csrf_token(self, name=None): - """ - NOTE: this method was copied from `pyramid_simpleform.FormRenderer` - - Convenience function. Returns CSRF hidden tag inside hidden DIV. - """ - return HTML.tag("div", self.csrf(name), style="display:none;") - - def render(self, **kwargs): - kwargs['form'] = self - if self.readonly: - template = '/forms/form_readonly.mako' - else: - template = '/forms/form.mako' - return render(template, kwargs) - - def render_fields(self): - return self.fieldset.render() - - def save(self): - self.fieldset.sync() - self.session.flush() - - def validate(self): - self.fieldset.rebind(data=self.request.params) - return self.fieldset.validate() diff --git a/tailbone/forms/core.py b/tailbone/forms/core.py deleted file mode 100644 index c2702fb7..00000000 --- a/tailbone/forms/core.py +++ /dev/null @@ -1,119 +0,0 @@ -# -*- coding: utf-8 -*- -################################################################################ -# -# Rattail -- Retail Software Framework -# Copyright © 2010-2017 Lance Edgar -# -# This file is part of Rattail. -# -# Rattail is free software: you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation, either version 3 of the License, or (at your option) any later -# version. -# -# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -# details. -# -# You should have received a copy of the GNU General Public License along with -# Rattail. If not, see . -# -################################################################################ -""" -Forms Core -""" - -from __future__ import unicode_literals, absolute_import - -from rattail.util import OrderedDict, prettify - -import formalchemy -from formalchemy.helpers import content_tag -from pyramid.renderers import render - - -class Form(object): - """ - Base class for all forms. - """ - create_label = "Create" - update_label = "Save" - - def __init__(self, request, readonly=False, action_url=None): - self.request = request - self.readonly = readonly - self.action_url = action_url - - def render(self, **kwargs): - kwargs.setdefault('form', self) - if self.readonly: - template = '/forms/form_readonly.mako' - else: - template = '/forms/form.mako' - return render(template, kwargs) - - def render_fields(self, **kwargs): - kwargs.setdefault('fieldset', self.fieldset) - if self.readonly: - template = '/forms/fieldset_readonly.mako' - else: - template = '/forms/fieldset.mako' - return render(template, kwargs) - - -class Field(object): - """ - Manually create instances of this class to populate a simple form. - """ - - def __init__(self, name, value=None, label=None, requires_label=True): - self.name = name - self.value = value - self._label = label or prettify(self.name) - self.requires_label = requires_label - - def is_required(self): - return True - - def label(self): - return self._label - - def label_tag(self, **html_options): - """ - Logic stolen from FormAlchemy so all fields can render their own label. - Original docstring follows. - - return the