Add readonly support for email profile settings.
More to come... Also this required some form tweaking/overhaul(s).
This commit is contained in:
parent
ba6bf87ded
commit
ef40af814a
13 changed files with 453 additions and 48 deletions
|
@ -24,7 +24,10 @@
|
|||
Forms
|
||||
"""
|
||||
|
||||
from .simpleform import *
|
||||
from formencode import Schema
|
||||
|
||||
from .core import Form, Field, FieldSet
|
||||
from .simpleform import SimpleForm, FormRenderer
|
||||
from .alchemy import AlchemyForm
|
||||
from .fields import *
|
||||
from .renderers import *
|
||||
|
|
|
@ -64,6 +64,9 @@ class AlchemyForm(Object):
|
|||
template = '/forms/form.mako'
|
||||
return render(template, kwargs)
|
||||
|
||||
def render_fields(self):
|
||||
return self.fieldset.render()
|
||||
|
||||
def save(self):
|
||||
self.fieldset.sync()
|
||||
Session.flush()
|
||||
|
|
110
tailbone/forms/core.py
Normal file
110
tailbone/forms/core.py
Normal file
|
@ -0,0 +1,110 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2015 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 Affero 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 Affero General Public License for
|
||||
# more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
################################################################################
|
||||
"""
|
||||
Forms Core
|
||||
"""
|
||||
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
from edbob.util import prettify
|
||||
|
||||
from rattail.util import OrderedDict
|
||||
|
||||
from pyramid.renderers import render
|
||||
from formalchemy.helpers import content_tag
|
||||
|
||||
|
||||
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 <label /> tag for the field.
|
||||
"""
|
||||
html_options.update(for_=self.name)
|
||||
if 'class_' in html_options:
|
||||
html_options['class_'] += self.is_required() and ' field_req' or ' field_opt'
|
||||
else:
|
||||
html_options['class_'] = self.is_required() and 'field_req' or 'field_opt'
|
||||
return content_tag('label', self.label(), **html_options)
|
||||
|
||||
def render_readonly(self):
|
||||
if self.value is None:
|
||||
return ''
|
||||
return unicode(self.value)
|
||||
|
||||
|
||||
class FieldSet(object):
|
||||
"""
|
||||
Generic fieldset for use with manually-created simple forms.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.fields = OrderedDict()
|
||||
self.render_fields = self.fields
|
|
@ -1,9 +1,8 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2012 Lance Edgar
|
||||
# Copyright © 2010-2015 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
|
@ -21,11 +20,13 @@
|
|||
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
################################################################################
|
||||
|
||||
"""
|
||||
``pyramid_simpleform`` Forms
|
||||
Simple Forms
|
||||
"""
|
||||
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import pyramid_simpleform
|
||||
from pyramid_simpleform import renderers
|
||||
|
||||
from webhelpers.html import tags
|
||||
|
@ -33,8 +34,24 @@ from webhelpers.html import HTML
|
|||
|
||||
from edbob.util import prettify
|
||||
|
||||
from tailbone.forms import Form
|
||||
|
||||
__all__ = ['FormRenderer']
|
||||
|
||||
class SimpleForm(Form):
|
||||
"""
|
||||
Customized simple form.
|
||||
"""
|
||||
|
||||
def __init__(self, request, schema, obj=None, **kwargs):
|
||||
super(SimpleForm, self).__init__(request, **kwargs)
|
||||
self._form = pyramid_simpleform.Form(request, schema=schema, obj=obj)
|
||||
|
||||
def __getattr__(self, attr):
|
||||
return getattr(self._form, attr)
|
||||
|
||||
def render(self, **kwargs):
|
||||
kwargs['form'] = FormRenderer(self)
|
||||
return super(SimpleForm, self).render(**kwargs)
|
||||
|
||||
|
||||
class FormRenderer(renderers.FormRenderer):
|
||||
|
@ -42,6 +59,9 @@ class FormRenderer(renderers.FormRenderer):
|
|||
Customized form renderer. Provides some extra methods for convenience.
|
||||
"""
|
||||
|
||||
def __getattr__(self, attr):
|
||||
return getattr(self.form, attr)
|
||||
|
||||
def field_div(self, name, field, label=None):
|
||||
errors = self.errors_for(name)
|
||||
if errors:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue