/gluon/sqlhtml.py
Python | 3114 lines | 3043 code | 36 blank | 35 comment | 136 complexity | 64303a9106989697b69b9f6773bcd202 MD5 | raw file
Possible License(s): BSD-2-Clause, MIT, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- This file is part of the web2py Web Framework
- Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
- License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
- Holds:
- - SQLFORM: provide a form for a table (with/without record)
- - SQLTABLE: provides a table for a set of records
- - form_factory: provides a SQLFORM for an non-db backed table
- """
- try:
- from urlparse import parse_qs as psq
- except ImportError:
- from cgi import parse_qs as psq
- import os
- import copy
- from http import HTTP
- from html import XmlComponent
- from html import XML, SPAN, TAG, A, DIV, CAT, UL, LI, TEXTAREA, BR, IMG, SCRIPT
- from html import FORM, INPUT, LABEL, OPTION, SELECT
- from html import TABLE, THEAD, TBODY, TR, TD, TH, STYLE
- from html import URL, truncate_string, FIELDSET
- from dal import DAL, Field, Table, Row, CALLABLETYPES, smart_query, \
- bar_encode, Reference, REGEX_TABLE_DOT_FIELD
- from storage import Storage
- from utils import md5_hash
- from validators import IS_EMPTY_OR, IS_NOT_EMPTY, IS_LIST_OF, IS_DATE, \
- IS_DATETIME, IS_INT_IN_RANGE, IS_FLOAT_IN_RANGE, IS_STRONG
- import serializers
- import datetime
- import urllib
- import re
- import cStringIO
- from globals import current
- from http import redirect
- import inspect
- try:
- import settings
- is_gae = settings.global_settings.web2py_runtime_gae
- except ImportError:
- is_gae = False # this is an assumption (if settings missing)
- table_field = re.compile('[\w_]+\.[\w_]+')
- widget_class = re.compile('^\w*')
- def trap_class(_class=None, trap=True):
- return (trap and 'w2p_trap' or '') + (_class and ' ' + _class or '')
- def represent(field, value, record):
- f = field.represent
- if not callable(f):
- return str(value)
- n = f.func_code.co_argcount - len(f.func_defaults or [])
- if getattr(f, 'im_self', None):
- n -= 1
- if n == 1:
- return f(value)
- elif n == 2:
- return f(value, record)
- else:
- raise RuntimeError("field representation must take 1 or 2 args")
- def safe_int(x):
- try:
- return int(x)
- except ValueError:
- return 0
- def safe_float(x):
- try:
- return float(x)
- except ValueError:
- return 0
- def show_if(cond):
- if not cond:
- return None
- base = "%s_%s" % (cond.first.tablename, cond.first.name)
- if ((cond.op.__name__ == 'EQ' and cond.second == True) or
- (cond.op.__name__ == 'NE' and cond.second == False)):
- return base,":checked"
- if ((cond.op.__name__ == 'EQ' and cond.second == False) or
- (cond.op.__name__ == 'NE' and cond.second == True)):
- return base,":not(:checked)"
- if cond.op.__name__ == 'EQ':
- return base,"[value='%s']" % cond.second
- if cond.op.__name__ == 'NE':
- return base,"[value!='%s']" % cond.second
- if cond.op.__name__ == 'CONTAINS':
- return base,"[value~='%s']" % cond.second
- if cond.op.__name__ == 'BELONGS' and isinstance(cond.second,(list,tuple)):
- return base,','.join("[value='%s']" % (v) for v in cond.second)
- raise RuntimeError("Not Implemented Error")
- class FormWidget(object):
- """
- helper for SQLFORM to generate form input fields
- (widget), related to the fieldtype
- """
- _class = 'generic-widget'
- @classmethod
- def _attributes(cls, field,
- widget_attributes, **attributes):
- """
- helper to build a common set of attributes
- :param field: the field involved,
- some attributes are derived from this
- :param widget_attributes: widget related attributes
- :param attributes: any other supplied attributes
- """
- attr = dict(
- _id='%s_%s' % (field.tablename, field.name),
- _class=cls._class or
- widget_class.match(str(field.type)).group(),
- _name=field.name,
- requires=field.requires,
- )
- if getattr(field,'show_if',None):
- trigger, cond = show_if(field.show_if)
- attr['_data-show-trigger'] = trigger
- attr['_data-show-if'] = cond
- attr.update(widget_attributes)
- attr.update(attributes)
- return attr
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates the widget for the field.
- When serialized, will provide an INPUT tag:
- - id = tablename_fieldname
- - class = field.type
- - name = fieldname
- :param field: the field needing the widget
- :param value: value
- :param attributes: any other attributes to be applied
- """
- raise NotImplementedError
- class StringWidget(FormWidget):
- _class = 'string'
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates an INPUT text tag.
- see also: :meth:`FormWidget.widget`
- """
- default = dict(
- _type='text',
- value=(not value is None and str(value)) or '',
- )
- attr = cls._attributes(field, default, **attributes)
- return INPUT(**attr)
- class IntegerWidget(StringWidget):
- _class = 'integer'
- class DoubleWidget(StringWidget):
- _class = 'double'
- class DecimalWidget(StringWidget):
- _class = 'decimal'
- class TimeWidget(StringWidget):
- _class = 'time'
- class DateWidget(StringWidget):
- _class = 'date'
- class DatetimeWidget(StringWidget):
- _class = 'datetime'
- class TextWidget(FormWidget):
- _class = 'text'
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates a TEXTAREA tag.
- see also: :meth:`FormWidget.widget`
- """
- default = dict(value=value)
- attr = cls._attributes(field, default, **attributes)
- return TEXTAREA(**attr)
- class JSONWidget(FormWidget):
- _class = 'json'
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates a TEXTAREA for JSON notation.
- see also: :meth:`FormWidget.widget`
- """
- if not isinstance(value, basestring):
- if value is not None:
- value = serializers.json(value)
- default = dict(value=value)
- attr = cls._attributes(field, default, **attributes)
- return TEXTAREA(**attr)
- class BooleanWidget(FormWidget):
- _class = 'boolean'
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates an INPUT checkbox tag.
- see also: :meth:`FormWidget.widget`
- """
- default = dict(_type='checkbox', value=value)
- attr = cls._attributes(field, default,
- **attributes)
- return INPUT(**attr)
- class OptionsWidget(FormWidget):
- @staticmethod
- def has_options(field):
- """
- checks if the field has selectable options
- :param field: the field needing checking
- :returns: True if the field has options
- """
- return hasattr(field.requires, 'options')
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates a SELECT tag, including OPTIONs (only 1 option allowed)
- see also: :meth:`FormWidget.widget`
- """
- default = dict(value=value)
- attr = cls._attributes(field, default,
- **attributes)
- requires = field.requires
- if not isinstance(requires, (list, tuple)):
- requires = [requires]
- if requires:
- if hasattr(requires[0], 'options'):
- options = requires[0].options()
- else:
- raise SyntaxError(
- 'widget cannot determine options of %s' % field)
- opts = [OPTION(v, _value=k) for (k, v) in options]
- return SELECT(*opts, **attr)
- class ListWidget(StringWidget):
- @classmethod
- def widget(cls, field, value, **attributes):
- _id = '%s_%s' % (field.tablename, field.name)
- _name = field.name
- if field.type == 'list:integer':
- _class = 'integer'
- else:
- _class = 'string'
- requires = field.requires if isinstance(
- field.requires, (IS_NOT_EMPTY, IS_LIST_OF)) else None
- nvalue = value or ['']
- items = [LI(INPUT(_id=_id, _class=_class, _name=_name,
- value=v, hideerror=k < len(nvalue) - 1,
- requires=requires),
- **attributes) for (k, v) in enumerate(nvalue)]
- attributes['_id'] = _id + '_grow_input'
- attributes['_style'] = 'list-style:none'
- attributes['_class'] = 'w2p_list'
- return TAG[''](UL(*items, **attributes))
- class MultipleOptionsWidget(OptionsWidget):
- @classmethod
- def widget(cls, field, value, size=5, **attributes):
- """
- generates a SELECT tag, including OPTIONs (multiple options allowed)
- see also: :meth:`FormWidget.widget`
- :param size: optional param (default=5) to indicate how many rows must
- be shown
- """
- attributes.update(_size=size, _multiple=True)
- return OptionsWidget.widget(field, value, **attributes)
- class RadioWidget(OptionsWidget):
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates a TABLE tag, including INPUT radios (only 1 option allowed)
- see also: :meth:`FormWidget.widget`
- """
- if isinstance(value, (list,tuple)):
- value = str(value[0])
- else:
- value = str(value)
- attr = cls._attributes(field, {}, **attributes)
- attr['_class'] = attr.get('_class', 'web2py_radiowidget')
- requires = field.requires
- if not isinstance(requires, (list, tuple)):
- requires = [requires]
- if requires:
- if hasattr(requires[0], 'options'):
- options = requires[0].options()
- else:
- raise SyntaxError('widget cannot determine options of %s'
- % field)
- options = [(k, v) for k, v in options if str(v)]
- opts = []
- cols = attributes.get('cols', 1)
- totals = len(options)
- mods = totals % cols
- rows = totals / cols
- if mods:
- rows += 1
- #widget style
- wrappers = dict(
- table=(TABLE, TR, TD),
- ul=(DIV, UL, LI),
- divs=(CAT, DIV, DIV)
- )
- parent, child, inner = wrappers[attributes.get('style', 'table')]
- for r_index in range(rows):
- tds = []
- for k, v in options[r_index * cols:(r_index + 1) * cols]:
- checked = {'_checked': 'checked'} if k == value else {}
- tds.append(inner(INPUT(_type='radio',
- _id='%s%s' % (field.name, k),
- _name=field.name,
- requires=attr.get('requires', None),
- hideerror=True, _value=k,
- value=value,
- **checked),
- LABEL(v, _for='%s%s' % (field.name, k))))
- opts.append(child(tds))
- if opts:
- opts[-1][0][0]['hideerror'] = False
- return parent(*opts, **attr)
- class CheckboxesWidget(OptionsWidget):
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates a TABLE tag, including INPUT checkboxes (multiple allowed)
- see also: :meth:`FormWidget.widget`
- """
- # was values = re.compile('[\w\-:]+').findall(str(value))
- if isinstance(value, (list, tuple)):
- values = [str(v) for v in value]
- else:
- values = [str(value)]
- attr = cls._attributes(field, {}, **attributes)
- attr['_class'] = attr.get('_class', 'web2py_checkboxeswidget')
- requires = field.requires
- if not isinstance(requires, (list, tuple)):
- requires = [requires]
- if requires and hasattr(requires[0], 'options'):
- options = requires[0].options()
- else:
- raise SyntaxError('widget cannot determine options of %s'
- % field)
- options = [(k, v) for k, v in options if k != '']
- opts = []
- cols = attributes.get('cols', 1)
- totals = len(options)
- mods = totals % cols
- rows = totals / cols
- if mods:
- rows += 1
- #widget style
- wrappers = dict(
- table=(TABLE, TR, TD),
- ul=(DIV, UL, LI),
- divs=(CAT, DIV, DIV)
- )
- parent, child, inner = wrappers[attributes.get('style', 'table')]
- for r_index in range(rows):
- tds = []
- for k, v in options[r_index * cols:(r_index + 1) * cols]:
- if k in values:
- r_value = k
- else:
- r_value = []
- tds.append(inner(INPUT(_type='checkbox',
- _id='%s%s' % (field.name, k),
- _name=field.name,
- requires=attr.get('requires', None),
- hideerror=True, _value=k,
- value=r_value),
- LABEL(v, _for='%s%s' % (field.name, k))))
- opts.append(child(tds))
- if opts:
- opts.append(
- INPUT(requires=attr.get('requires', None),
- _style="display:none;",
- _disabled="disabled",
- _name=field.name,
- hideerror=False))
- return parent(*opts, **attr)
- class PasswordWidget(FormWidget):
- _class = 'password'
- DEFAULT_PASSWORD_DISPLAY = 8 * ('*')
- @classmethod
- def widget(cls, field, value, **attributes):
- """
- generates a INPUT password tag.
- If a value is present it will be shown as a number of '*', not related
- to the length of the actual value.
- see also: :meth:`FormWidget.widget`
- """
- # detect if attached a IS_STRONG with entropy
- default = dict(
- _type='password',
- _value=(value and cls.DEFAULT_PASSWORD_DISPLAY) or '',
- )
- attr = cls._attributes(field, default, **attributes)
- # deal with entropy check!
- requires = field.requires
- if not isinstance(requires, (list, tuple)):
- requires = [requires]
- is_strong = [r for r in requires if isinstance(r, IS_STRONG)]
- if is_strong:
- attr['_data-w2p_entropy'] = is_strong[0].entropy if is_strong[0].entropy else "null"
- # end entropy check
- output = INPUT(**attr)
- return output
- class UploadWidget(FormWidget):
- _class = 'upload'
- DEFAULT_WIDTH = '150px'
- ID_DELETE_SUFFIX = '__delete'
- GENERIC_DESCRIPTION = 'file'
- DELETE_FILE = 'delete'
- @classmethod
- def widget(cls, field, value, download_url=None, **attributes):
- """
- generates a INPUT file tag.
- Optionally provides an A link to the file, including a checkbox so
- the file can be deleted.
- All is wrapped in a DIV.
- see also: :meth:`FormWidget.widget`
- :param download_url: Optional URL to link to the file (default = None)
- """
- default = dict(_type='file',)
- attr = cls._attributes(field, default, **attributes)
- inp = INPUT(**attr)
- if download_url and value:
- if callable(download_url):
- url = download_url(value)
- else:
- url = download_url + '/' + value
- (br, image) = ('', '')
- if UploadWidget.is_image(value):
- br = BR()
- image = IMG(_src=url, _width=cls.DEFAULT_WIDTH)
- requires = attr["requires"]
- if requires == [] or isinstance(requires, IS_EMPTY_OR):
- inp = DIV(inp,
- SPAN('[',
- A(current.T(
- UploadWidget.GENERIC_DESCRIPTION), _href=url),
- '|',
- INPUT(_type='checkbox',
- _name=field.name + cls.ID_DELETE_SUFFIX,
- _id=field.name + cls.ID_DELETE_SUFFIX),
- LABEL(current.T(cls.DELETE_FILE),
- _for=field.name + cls.ID_DELETE_SUFFIX,
- _style='display:inline'),
- ']', _style='white-space:nowrap'),
- br, image)
- else:
- inp = DIV(inp,
- SPAN('[',
- A(cls.GENERIC_DESCRIPTION, _href=url),
- ']', _style='white-space:nowrap'),
- br, image)
- return inp
- @classmethod
- def represent(cls, field, value, download_url=None):
- """
- how to represent the file:
- - with download url and if it is an image: <A href=...><IMG ...></A>
- - otherwise with download url: <A href=...>file</A>
- - otherwise: file
- :param field: the field
- :param value: the field value
- :param download_url: url for the file download (default = None)
- """
- inp = cls.GENERIC_DESCRIPTION
- if download_url and value:
- if callable(download_url):
- url = download_url(value)
- else:
- url = download_url + '/' + value
- if cls.is_image(value):
- inp = IMG(_src=url, _width=cls.DEFAULT_WIDTH)
- inp = A(inp, _href=url)
- return inp
- @staticmethod
- def is_image(value):
- """
- Tries to check if the filename provided references to an image
- Checking is based on filename extension. Currently recognized:
- gif, png, jp(e)g, bmp
- :param value: filename
- """
- extension = value.split('.')[-1].lower()
- if extension in ['gif', 'png', 'jpg', 'jpeg', 'bmp']:
- return True
- return False
- class AutocompleteWidget(object):
- _class = 'string'
- def __init__(self, request, field, id_field=None, db=None,
- orderby=None, limitby=(0, 10), distinct=False,
- keyword='_autocomplete_%(tablename)s_%(fieldname)s',
- min_length=2, help_fields=None, help_string=None):
- self.help_fields = help_fields or []
- self.help_string = help_string
- if self.help_fields and not self.help_string:
- self.help_string = ' '.join('%%(%s)s'%f.name
- for f in self.help_fields)
- self.request = request
- self.keyword = keyword % dict(tablename=field.tablename,
- fieldname=field.name)
- self.db = db or field._db
- self.orderby = orderby
- self.limitby = limitby
- self.distinct = distinct
- self.min_length = min_length
- self.fields = [field]
- if id_field:
- self.is_reference = True
- self.fields.append(id_field)
- else:
- self.is_reference = False
- if hasattr(request, 'application'):
- self.url = URL(args=request.args)
- self.callback()
- else:
- self.url = request
- def callback(self):
- if self.keyword in self.request.vars:
- field = self.fields[0]
- if is_gae:
- rows = self.db(field.__ge__(self.request.vars[self.keyword]) & field.__lt__(self.request.vars[self.keyword] + u'\ufffd')).select(orderby=self.orderby, limitby=self.limitby, *(self.fields+self.help_fields))
- else:
- rows = self.db(field.like(self.request.vars[self.keyword] + '%')).select(orderby=self.orderby, limitby=self.limitby, distinct=self.distinct, *(self.fields+self.help_fields))
- if rows:
- if self.is_reference:
- id_field = self.fields[1]
- if self.help_fields:
- options = [OPTION(
- self.help_string % dict([(h.name, s[h.name]) for h in self.fields[:1] + self.help_fields]),
- _value=s[id_field.name], _selected=(k == 0)) for k, s in enumerate(rows)]
- else:
- options = [OPTION(
- s[field.name], _value=s[id_field.name],
- _selected=(k == 0)) for k, s in enumerate(rows)]
- raise HTTP(
- 200, SELECT(_id=self.keyword, _class='autocomplete',
- _size=len(rows), _multiple=(len(rows) == 1),
- *options).xml())
- else:
- raise HTTP(
- 200, SELECT(_id=self.keyword, _class='autocomplete',
- _size=len(rows), _multiple=(len(rows) == 1),
- *[OPTION(s[field.name],
- _selected=(k == 0))
- for k, s in enumerate(rows)]).xml())
- else:
- raise HTTP(200, '')
- def __call__(self, field, value, **attributes):
- default = dict(
- _type='text',
- value=(not value is None and str(value)) or '',
- )
- attr = StringWidget._attributes(field, default, **attributes)
- div_id = self.keyword + '_div'
- attr['_autocomplete'] = 'off'
- if self.is_reference:
- key2 = self.keyword + '_aux'
- key3 = self.keyword + '_auto'
- attr['_class'] = 'string'
- name = attr['_name']
- if 'requires' in attr:
- del attr['requires']
- attr['_name'] = key2
- value = attr['value']
- record = self.db(
- self.fields[1] == value).select(self.fields[0]).first()
- attr['value'] = record and record[self.fields[0].name]
- attr['_onblur'] = "jQuery('#%(div_id)s').delay(1000).fadeOut('slow');" % \
- dict(div_id=div_id, u='F' + self.keyword)
- attr['_onkeyup'] = "jQuery('#%(key3)s').val('');var e=event.which?event.which:event.keyCode; function %(u)s(){jQuery('#%(id)s').val(jQuery('#%(key)s :selected').text());jQuery('#%(key3)s').val(jQuery('#%(key)s').val())}; if(e==39) %(u)s(); else if(e==40) {if(jQuery('#%(key)s option:selected').next().length)jQuery('#%(key)s option:selected').attr('selected',null).next().attr('selected','selected'); %(u)s();} else if(e==38) {if(jQuery('#%(key)s option:selected').prev().length)jQuery('#%(key)s option:selected').attr('selected',null).prev().attr('selected','selected'); %(u)s();} else if(jQuery('#%(id)s').val().length>=%(min_length)s) jQuery.get('%(url)s?%(key)s='+encodeURIComponent(jQuery('#%(id)s').val()),function(data){if(data=='')jQuery('#%(key3)s').val('');else{jQuery('#%(id)s').next('.error').hide();jQuery('#%(div_id)s').html(data).show().focus();jQuery('#%(div_id)s select').css('width',jQuery('#%(id)s').css('width'));jQuery('#%(key3)s').val(jQuery('#%(key)s').val());jQuery('#%(key)s').change(%(u)s);jQuery('#%(key)s').click(%(u)s);};}); else jQuery('#%(div_id)s').fadeOut('slow');" % \
- dict(url=self.url, min_length=self.min_length,
- key=self.keyword, id=attr['_id'], key2=key2, key3=key3,
- name=name, div_id=div_id, u='F' + self.keyword)
- if self.min_length == 0:
- attr['_onfocus'] = attr['_onkeyup']
- return TAG[''](INPUT(**attr), INPUT(_type='hidden', _id=key3, _value=value,
- _name=name, requires=field.requires),
- DIV(_id=div_id, _style='position:absolute;'))
- else:
- attr['_name'] = field.name
- attr['_onblur'] = "jQuery('#%(div_id)s').delay(1000).fadeOut('slow');" % \
- dict(div_id=div_id, u='F' + self.keyword)
- attr['_onkeyup'] = "var e=event.which?event.which:event.keyCode; function %(u)s(){jQuery('#%(id)s').val(jQuery('#%(key)s').val())}; if(e==39) %(u)s(); else if(e==40) {if(jQuery('#%(key)s option:selected').next().length)jQuery('#%(key)s option:selected').attr('selected',null).next().attr('selected','selected'); %(u)s();} else if(e==38) {if(jQuery('#%(key)s option:selected').prev().length)jQuery('#%(key)s option:selected').attr('selected',null).prev().attr('selected','selected'); %(u)s();} else if(jQuery('#%(id)s').val().length>=%(min_length)s) jQuery.get('%(url)s?%(key)s='+encodeURIComponent(jQuery('#%(id)s').val()),function(data){jQuery('#%(id)s').next('.error').hide();jQuery('#%(div_id)s').html(data).show().focus();jQuery('#%(div_id)s select').css('width',jQuery('#%(id)s').css('width'));jQuery('#%(key)s').change(%(u)s);jQuery('#%(key)s').click(%(u)s);}); else jQuery('#%(div_id)s').fadeOut('slow');" % \
- dict(url=self.url, min_length=self.min_length,
- key=self.keyword, id=attr['_id'], div_id=div_id, u='F' + self.keyword)
- if self.min_length == 0:
- attr['_onfocus'] = attr['_onkeyup']
- return TAG[''](INPUT(**attr), DIV(_id=div_id, _style='position:absolute;'))
- def formstyle_table3cols(form, fields):
- ''' 3 column table - default '''
- table = TABLE()
- for id, label, controls, help in fields:
- _help = TD(help, _class='w2p_fc')
- _controls = TD(controls, _class='w2p_fw')
- _label = TD(label, _class='w2p_fl')
- table.append(TR(_label, _controls, _help, _id=id))
- return table
- def formstyle_table2cols(form, fields):
- ''' 2 column table '''
- table = TABLE()
- for id, label, controls, help in fields:
- _help = TD(help, _class='w2p_fc', _width='50%')
- _controls = TD(controls, _class='w2p_fw', _colspan='2')
- _label = TD(label, _class='w2p_fl', _width='50%')
- table.append(TR(_label, _help, _id=id + '1', _class='even'))
- table.append(TR(_controls, _id=id + '2', _class='odd'))
- return table
- def formstyle_divs(form, fields):
- ''' divs only '''
- table = FIELDSET()
- for id, label, controls, help in fields:
- _help = DIV(help, _class='w2p_fc')
- _controls = DIV(controls, _class='w2p_fw')
- _label = DIV(label, _class='w2p_fl')
- table.append(DIV(_label, _controls, _help, _id=id))
- return table
- def formstyle_inline(form, fields):
- ''' divs only '''
- if len(fields) != 2:
- raise RuntimeError("Not possible")
- id, label, controls, help = fields[0]
- submit_button = fields[1][2]
- return CAT(DIV(controls, _style='display:inline'),
- submit_button)
- def formstyle_ul(form, fields):
- ''' unordered list '''
- table = UL()
- for id, label, controls, help in fields:
- _help = DIV(help, _class='w2p_fc')
- _controls = DIV(controls, _class='w2p_fw')
- _label = DIV(label, _class='w2p_fl')
- table.append(LI(_label, _controls, _help, _id=id))
- return table
- def formstyle_bootstrap(form, fields):
- ''' bootstrap format form layout '''
- form.add_class('form-horizontal')
- parent = FIELDSET()
- for id, label, controls, help in fields:
- # wrappers
- _help = SPAN(help, _class='help-block')
- # embed _help into _controls
- _controls = DIV(controls, _help, _class='controls')
- # submit unflag by default
- _submit = False
- if isinstance(controls, INPUT):
- controls.add_class('span4')
- if controls['_type'] == 'submit':
- # flag submit button
- _submit = True
- controls['_class'] = 'btn btn-primary'
- if controls['_type'] == 'file':
- controls['_class'] = 'input-file'
- # For password fields, which are wrapped in a CAT object.
- if isinstance(controls, CAT) and isinstance(controls[0], INPUT):
- controls[0].add_class('span4')
- if isinstance(controls, SELECT):
- controls.add_class('span4')
- if isinstance(controls, TEXTAREA):
- controls.add_class('span4')
- if isinstance(label, LABEL):
- label['_class'] = 'control-label'
- if _submit:
- # submit button has unwrapped label and controls, different class
- parent.append(DIV(label, controls, _class='form-actions', _id=id))
- # unflag submit (possible side effect)
- _submit = False
- else:
- # unwrapped label
- parent.append(DIV(label, _controls, _class='control-group', _id=id))
- return parent
- class SQLFORM(FORM):
- """
- SQLFORM is used to map a table (and a current record) into an HTML form
- given a SQLTable stored in db.table
- generates an insert form::
- SQLFORM(db.table)
- generates an update form::
- record=db.table[some_id]
- SQLFORM(db.table, record)
- generates an update with a delete button::
- SQLFORM(db.table, record, deletable=True)
- if record is an int::
- record=db.table[record]
- optional arguments:
- :param fields: a list of fields that should be placed in the form,
- default is all.
- :param labels: a dictionary with labels for each field, keys are the field
- names.
- :param col3: a dictionary with content for an optional third column
- (right of each field). keys are field names.
- :param linkto: the URL of a controller/function to access referencedby
- records
- see controller appadmin.py for examples
- :param upload: the URL of a controller/function to download an uploaded file
- see controller appadmin.py for examples
- any named optional attribute is passed to the <form> tag
- for example _class, _id, _style, _action, _method, etc.
- """
- # usability improvements proposal by fpp - 4 May 2008 :
- # - correct labels (for points to field id, not field name)
- # - add label for delete checkbox
- # - add translatable label for record ID
- # - add third column to right of fields, populated from the col3 dict
- widgets = Storage(dict(
- string=StringWidget,
- text=TextWidget,
- json=JSONWidget,
- password=PasswordWidget,
- integer=IntegerWidget,
- double=DoubleWidget,
- decimal=DecimalWidget,
- time=TimeWidget,
- date=DateWidget,
- datetime=DatetimeWidget,
- upload=UploadWidget,
- boolean=BooleanWidget,
- blob=None,
- options=OptionsWidget,
- multiple=MultipleOptionsWidget,
- radio=RadioWidget,
- checkboxes=CheckboxesWidget,
- autocomplete=AutocompleteWidget,
- list=ListWidget,
- ))
- formstyles = Storage(dict(
- table3cols=formstyle_table3cols,
- table2cols=formstyle_table2cols,
- divs=formstyle_divs,
- ul=formstyle_ul,
- bootstrap=formstyle_bootstrap,
- inline=formstyle_inline,
- ))
- FIELDNAME_REQUEST_DELETE = 'delete_this_record'
- FIELDKEY_DELETE_RECORD = 'delete_record'
- ID_LABEL_SUFFIX = '__label'
- ID_ROW_SUFFIX = '__row'
- def assert_status(self, status, request_vars):
- if not status and self.record and self.errors:
- ### if there are errors in update mode
- # and some errors refers to an already uploaded file
- # delete error if
- # - user not trying to upload a new file
- # - there is existing file and user is not trying to delete it
- # this is because removing the file may not pass validation
- for key in self.errors.keys():
- if key in self.table \
- and self.table[key].type == 'upload' \
- and request_vars.get(key, None) in (None, '') \
- and self.record[key] \
- and not key + UploadWidget.ID_DELETE_SUFFIX in request_vars:
- del self.errors[key]
- if not self.errors:
- status = True
- return status
- def __init__(
- self,
- table,
- record=None,
- deletable=False,
- linkto=None,
- upload=None,
- fields=None,
- labels=None,
- col3={},
- submit_button='Submit',
- delete_label='Check to delete',
- showid=True,
- readonly=False,
- comments=True,
- keepopts=[],
- ignore_rw=False,
- record_id=None,
- formstyle='table3cols',
- buttons=['submit'],
- separator=': ',
- **attributes
- ):
- """
- SQLFORM(db.table,
- record=None,
- fields=['name'],
- labels={'name': 'Your name'},
- linkto=URL(f='table/db/')
- """
- T = current.T
- self.ignore_rw = ignore_rw
- self.formstyle = formstyle
- self.readonly = readonly
- # Default dbio setting
- self.detect_record_change = None
- nbsp = XML(' ') # Firefox2 does not display fields with blanks
- FORM.__init__(self, *[], **attributes)
- ofields = fields
- keyed = hasattr(table, '_primarykey') # for backward compatibility
- # if no fields are provided, build it from the provided table
- # will only use writable or readable fields, unless forced to ignore
- if fields is None:
- fields = [f.name for f in table if
- (ignore_rw or f.writable or f.readable) and
- (readonly or not f.compute)]
- self.fields = fields
- # make sure we have an id
- if self.fields[0] != table.fields[0] and \
- isinstance(table, Table) and not keyed:
- self.fields.insert(0, table.fields[0])
- self.table = table
- # try to retrieve the indicated record using its id
- # otherwise ignore it
- if record and isinstance(record, (int, long, str, unicode)):
- if not str(record).isdigit():
- raise HTTP(404, "Object not found")
- record = table._db(table._id == record).select().first()
- if not record:
- raise HTTP(404, "Object not found")
- self.record = record
- self.record_id = record_id
- if keyed:
- self.record_id = dict([(k, record and str(record[k]) or None)
- for k in table._primarykey])
- self.field_parent = {}
- xfields = []
- self.fields = fields
- self.custom = Storage()
- self.custom.dspval = Storage()
- self.custom.inpval = Storage()
- self.custom.label = Storage()
- self.custom.comment = Storage()
- self.custom.widget = Storage()
- self.custom.linkto = Storage()
- # default id field name
- if not keyed:
- self.id_field_name = table._id.name
- else:
- self.id_field_name = table._primarykey[0] # only works if one key
- sep = separator or ''
- for fieldname in self.fields:
- if fieldname.find('.') >= 0:
- continue
- field = self.table[fieldname]
- comment = None
- if comments:
- comment = col3.get(fieldname, field.comment)
- if comment is None:
- comment = ''
- self.custom.comment[fieldname] = comment
- if not labels is None and fieldname in labels:
- label = labels[fieldname]
- else:
- label = field.label
- self.custom.label[fieldname] = label
- field_id = '%s_%s' % (table._tablename, fieldname)
- label = LABEL(label, label and sep, _for=field_id,
- _id=field_id + SQLFORM.ID_LABEL_SUFFIX)
- row_id = field_id + SQLFORM.ID_ROW_SUFFIX
- if field.type == 'id':
- self.custom.dspval.id = nbsp
- self.custom.inpval.id = ''
- widget = ''
- # store the id field name (for legacy databases)
- self.id_field_name = field.name
- if record:
- if showid and field.name in record and field.readable:
- v = record[field.name]
- widget = SPAN(v, _id=field_id)
- self.custom.dspval.id = str(v)
- xfields.append((row_id, label, widget, comment))
- self.record_id = str(record[field.name])
- self.custom.widget.id = widget
- continue
- if readonly and not ignore_rw and not field.readable:
- continue
- if record:
- default = record[fieldname]
- else:
- default = field.default
- if isinstance(default, CALLABLETYPES):
- default = default()
- cond = readonly or \
- (not ignore_rw and not field.writable and field.readable)
- if default is not None and not cond:
- default = field.formatter(default)
- dspval = default
- inpval = default
- if cond:
- # ## if field.represent is available else
- # ## ignore blob and preview uploaded images
- # ## format everything else
- if field.represent:
- inp = represent(field, default, record)
- elif field.type in ['blob']:
- continue
- elif field.type == 'upload':
- inp = UploadWidget.represent(field, default, upload)
- elif field.type == 'boolean':
- inp = self.widgets.boolean.widget(
- field, default, _disabled=True)
- else:
- inp = field.formatter(default)
- elif field.type == 'upload':
- if field.widget:
- inp = field.widget(field, default, upload)
- else:
- inp = self.widgets.upload.widget(field, default, upload)
- elif field.widget:
- inp = field.widget(field, default)
- elif field.type == 'boolean':
- inp = self.widgets.boolean.widget(field, default)
- if default:
- inpval = 'checked'
- else:
- inpval = ''
- elif OptionsWidget.has_options(field):
- if not field.requires.multiple:
- inp = self.widgets.options.widget(field, default)
- else:
- inp = self.widgets.multiple.widget(field, default)
- if fieldname in keepopts:
- inpval = TAG[''](*inp.components)
- elif field.type.startswith('list:'):
- inp = self.widgets.list.widget(field, default)
- elif field.type == 'text':
- inp = self.widgets.text.widget(field, default)
- elif field.type == 'password':
- inp = self.widgets.password.widget(field, default)
- if self.record:
- dspval = PasswordWidget.DEFAULT_PASSWORD_DISPLAY
- else:
- dspval = ''
- elif field.type == 'blob':
- continue
- else:
- field_type = widget_class.match(str(field.type)).group()
- field_type = field_type in self.widgets and field_type or 'string'
- inp = self.widgets[field_type].widget(field, default)
- xfields.append((row_id, label, inp, comment))
- self.custom.dspval[fieldname] = dspval if (dspval is not None) else nbsp
- self.custom.inpval[
- fieldname] = inpval if not inpval is None else ''
- self.custom.widget[fieldname] = inp
- # if a record is provided and found, as is linkto
- # build a link
- if record and linkto:
- db = linkto.split('/')[-1]
- for rfld in table._referenced_by:
- if keyed:
- query = urllib.quote('%s.%s==%s' % (
- db, rfld, record[rfld.type[10:].split('.')[1]]))
- else:
- query = urllib.quote(
- '%s.%s==%s' % (db, rfld, record[self.id_field_name]))
- lname = olname = '%s.%s' % (rfld.tablename, rfld.name)
- if ofields and not olname in ofields:
- continue
- if labels and lname in labels:
- lname = labels[lname]
- widget = A(lname,
- _class='reference',
- _href='%s/%s?query=%s' % (linkto, rfld.tablename, query))
- xfields.append(
- (olname.replace('.', '__') + SQLFORM.ID_ROW_SUFFIX,
- '', widget, col3.get(olname, '')))
- self.custom.linkto[olname.replace('.', '__')] = widget
- # </block>
- # when deletable, add delete? checkbox
- self.custom.delete = self.custom.deletable = ''
- if record and deletable:
- #add secondary css class for cascade delete warning
- css = 'delete'
- for f in self.table.fields:
- on_del = self.table[f].ondelete
- if isinstance(on_del,str) and 'cascade' in on_del.lower():
- css += ' cascade_delete'
- break
- widget = INPUT(_type='checkbox',
- _class=css,
- _id=self.FIELDKEY_DELETE_RECORD,
- _name=self.FIELDNAME_REQUEST_DELETE,
- )
- xfields.append(
- (self.FIELDKEY_DELETE_RECORD + SQLFORM.ID_ROW_SUFFIX,
- LABEL(
- T(delete_label), separator,
- _for=self.FIELDKEY_DELETE_RECORD,
- _id=self.FIELDKEY_DELETE_RECORD + \
- SQLFORM.ID_LABEL_SUFFIX),
- widget,
- col3.get(self.FIELDKEY_DELETE_RECORD, '')))
- self.custom.delete = self.custom.deletable = widget
- # when writable, add submit button
- self.custom.submit = ''
- if not readonly:
- if 'submit' in buttons:
- widget = self.custom.submit = INPUT(_type='submit',
- _value=T(submit_button))
- elif buttons:
- widget = self.custom.submit = DIV(*buttons)
- if self.custom.submit:
- xfields.append(('submit_record' + SQLFORM.ID_ROW_SUFFIX,
- '', widget, col3.get('submit_button', '')))
- # if a record is provided and found
- # make sure it's id is stored in the form
- if record:
- if not self['hidden']:
- self['hidden'] = {}
- if not keyed:
- self['hidden']['id'] = record[table._id.name]
- (begin, end) = self._xml()
- self.custom.begin = XML("<%s %s>" % (self.tag, begin))
- self.custom.end = XML("%s</%s>" % (end, self.tag))
- table = self.createform(xfields)
- self.components = [table]
- def createform(self, xfields):
- formstyle = self.formstyle
- if isinstance(formstyle, basestring):
- if formstyle in SQLFORM.formstyles:
- formstyle = SQLFORM.formstyles[formstyle]
- else:
- raise RuntimeError('formstyle not found')
- if callable(formstyle):
- # backward compatibility, 4 argument function is the old style
- args, varargs, keywords, defaults = inspect.getargspec(formstyle)
- if defaults and len(args) - len(defaults) == 4 or len(args) == 4:
- table = TABLE()
- for id, a, b, c in xfields:
- newrows = formstyle(id, a, b, c)
- self.field_parent[id] = getattr(b, 'parent', None) \
- if isinstance(b,XmlComponent) else None
- if type(newrows).__name__ != "tuple":
- newrows = [newrows]
- for newrow in newrows:
- table.append(newrow)
- else:
- table = formstyle(self, xfields)
- for id, a, b, c in xfields:
- self.field_parent[id] = getattr(b, 'parent', None) \
- if isinstance(b,XmlComponent) else None
- else:
- raise RuntimeError('formstyle not supported')
- return table
- def accepts(
- self,
- request_vars,
- session=None,
- formname='%(tablename)s/%(record_id)s',
- keepvalues=None,
- onvalidation=None,
- dbio=True,
- hideerror=False,
- detect_record_change=False,
- **kwargs
- ):
- """
- similar FORM.accepts but also does insert, update or delete in DAL.
- but if detect_record_change == True than:
- form.record_changed = False (record is properly validated/submitted)
- form.record_changed = True (record cannot be submitted because changed)
- elseif detect_record_change == False than:
- form.record_changed = None
- """
- if keepvalues is None:
- keepvalues = True if self.record else False
- if self.readonly:
- return False
- if request_vars.__class__.__name__ == 'Request':
- request_vars = request_vars.post_vars
- keyed = hasattr(self.table, '_primarykey')
- # implement logic to detect whether record exist but has been modified
- # server side
- self.record_changed = None
- self.detect_record_change = detect_record_change
- if self.detect_record_change:
- if self.record:
- self.record_changed = False
- serialized = '|'.join(
- str(self.record[k]) for k in self.table.fields())
- self.record_hash = md5_hash(serialized)
- # logic to deal with record_id for keyed tables
- if self.record:
- if keyed:
- formname_id = '.'.join(str(self.record[k])
- for k in self.table._primarykey
- if hasattr(self.record, k))
- record_id = dict((k, request_vars.get(k, None))
- for k in self.table._primarykey)
- else:
- (formname_id, record_id) = (self.record[self.id_field_name],
- request_vars.get('id', None))
- keepvalues = True
- else:
- if keyed:
- formname_id = 'create'
- record_id = dict([(k, None) for k in self.table._primarykey])
- else:
- (formname_id, record_id) = ('create', None)
- if not keyed and isinstance(record_id, (list, tuple)):
- record_id = record_id[0]
- if formname:
- formname = formname % dict(tablename=self.table._tablename,
- record_id=formname_id)
- # ## THIS IS FOR UNIQUE RECORDS, read IS_NOT_IN_DB
- for fieldname in self.fields:
- field = self.table[fieldname]
- requires = field.requires or []
- if not isinstance(requires, (list, tuple)):
- requires = [requires]
- [item.set_self_id(self.record_id) for item in requires
- if hasattr(item, 'set_self_id') and self.record_id]
- # ## END
- fields = {}
- for key in self.vars:
- fields[key] = self.vars[key]
- ret = FORM.accepts(
- self,
- request_vars,
- session,
- formname,
- keepvalues,
- onvalidation,
- hideerror=hideerror,
- **kwargs
- )
- self.deleted = \
- request_vars.get(self.FIELDNAME_REQUEST_DELETE, False)
- self.custom.end = TAG[''](self.hidden_fields(), self.custom.end)
- auch = record_id and self.errors and self.deleted
- if self.record_changed and self.detect_record_change:
- message_onchange = \
- kwargs.setdefault("message_onchange",
- current.T("A record change was detected. " +
- "Consecutive update self-submissions " +
- "are not allowed. Try re-submitting or " +
- "refreshing the form page."))
- if message_onchange is not None:
- current.response.flash = message_onchange
- return ret
- elif (not ret) and (not auch):
- # auch is true when user tries to delete a record
- # that does not pass validation, yet it should be deleted
- for fieldname in self.fields:
- field = self.table[fieldname]
- ### this is a workaround! widgets should always have default not None!
- if not field.widget and field.type.startswith('list:') and \
- not OptionsWidget.has_options(field):
- field.widget = self.widgets.list.widget
- if field.widget and fieldname in request_vars:
- if fieldname in self.request_vars:
- value = self.request_vars[fieldname]
- elif self.record:
- value = self.record[fieldname]
- else:
- value = self.table[fieldname].default
- if field.type.startswith('list:') and isinstance(v…
Large files files are truncated, but you can click here to view the full file