Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
LONG_DESC = """
The name of the thing. This is a pretty long line, and hopefully I won't
need to type too much more of it because it's pretty boring to type this kind
of thing """
class MemoryTmpStore(dict):
def preview_url(self, uid):
return None
memory = MemoryTmpStore()
class DatesSchema(schema.SequenceSchema):
date = schema.SchemaNode(schema.Date())
class SeriesSchema(schema.MappingSchema):
name = schema.SchemaNode(schema.String())
dates = DatesSchema()
class FileUploads(schema.SequenceSchema):
file = schema.SchemaNode(schema.FileData())
class MySchema(schema.MappingSchema):
name = schema.SchemaNode(schema.String(), description=LONG_DESC)
title = schema.SchemaNode(schema.String(),
validator=colander.Length(max=10),
description=LONG_DESC)
password = schema.SchemaNode(schema.String(),
validator=colander.Length(min=5))
cool = schema.SchemaNode(schema.Boolean(), default=True)
series = SeriesSchema()
color = schema.SchemaNode(schema.String(),
of thing """
class MemoryTmpStore(dict):
def preview_url(self, uid):
return None
memory = MemoryTmpStore()
class DatesSchema(schema.SequenceSchema):
date = schema.SchemaNode(schema.Date())
class SeriesSchema(schema.MappingSchema):
name = schema.SchemaNode(schema.String())
dates = DatesSchema()
class FileUploads(schema.SequenceSchema):
file = schema.SchemaNode(schema.FileData())
class MySchema(schema.MappingSchema):
name = schema.SchemaNode(schema.String(), description=LONG_DESC)
title = schema.SchemaNode(schema.String(),
validator=colander.Length(max=10),
description=LONG_DESC)
password = schema.SchemaNode(schema.String(),
validator=colander.Length(min=5))
cool = schema.SchemaNode(schema.Boolean(), default=True)
series = SeriesSchema()
color = schema.SchemaNode(schema.String(),
validator=colander.OneOf(('red', 'blue')))
uploads = FileUploads()
def validate_form(form, value):
def inline_checkboxchoice(self):
class Schema(colander.Schema):
choice = colander.SchemaNode(
deform.schema.Set(),
widget=deform.widget.CheckboxChoiceWidget(
values=[('a', 'Awesome'),
('b', 'Bogus'),
('c', 'Crud')],
inline=True),
description='Pick as many as apply')
schema = Schema()
form = deform.Form(schema, buttons=('submit',))
return self.render_form(form)
from deform import widget
from deform import form
from deform import exception
LONG_DESC = """
The name of the thing. This is a pretty long line, and hopefully I won't
need to type too much more of it because it's pretty boring to type this kind
of thing """
class MemoryTmpStore(dict):
def preview_url(self, uid):
return None
memory = MemoryTmpStore()
class DatesSchema(schema.SequenceSchema):
date = schema.SchemaNode(schema.Date())
class SeriesSchema(schema.MappingSchema):
name = schema.SchemaNode(schema.String())
dates = DatesSchema()
class FileUploads(schema.SequenceSchema):
file = schema.SchemaNode(schema.FileData())
class MySchema(schema.MappingSchema):
name = schema.SchemaNode(schema.String(), description=LONG_DESC)
title = schema.SchemaNode(schema.String(),
validator=colander.Length(max=10),
description=LONG_DESC)
password = schema.SchemaNode(schema.String(),
validator=colander.Length(min=5))
class ImageUploadTempStore(FileUploadTempStore):
def preview_url(self, uid):
root = self.request.root
return self.request.mgmt_path(root, '@@preview_image_upload', uid)
@colander.deferred
def image_upload_widget(node, kw):
request = kw['request']
tmpstore = ImageUploadTempStore(request)
widget = deform.widget.FileUploadWidget(tmpstore)
widget.template = 'image_upload'
return widget
imagenode = colander.SchemaNode(
deform.schema.FileData(),
widget = image_upload_widget,
)
class ImageUploadSchema(Schema):
file = imagenode.clone()
class ImageUploadPropertySheet(FileUploadPropertySheet):
schema = ImageUploadSchema()
@content(
IImage,
name='Image',
icon='icon-camera',
add_view='add_image',
# prevent view tab from sorting first (it would display the image when
# manage_main clicked)
""" If a widget is not assigned directly to a field, this
function will be called to generate a default widget (only
once). The result of this function will then be assigned as
the ``widget`` attribute of the field for the rest of the
lifetime of this field. If a widget is assigned to a field
before form processing, this function will not be called."""
wdg = getattr(self.schema, "widget", None)
if wdg is not None:
return wdg
widget_maker = getattr(self.schema.typ, "widget_maker", None)
if widget_maker is None:
widget_maker = schema.default_widget_makers.get(
self.schema.typ.__class__
)
if widget_maker is None:
for (cls, wgt) in schema.default_widget_makers.items():
if isinstance(self.schema.typ, cls):
widget_maker = wgt
break
if widget_maker is None:
widget_maker = widget.TextInputWidget
return widget_maker(item_css_class=self.default_item_css_class())
)
def view_file(context, request):
return context.get_response(request=request)
@mgmt_view(
context=IFile,
name='view',
tab_title='View',
permission='sdi.view'
)
def view_tab(context, request):
return HTTPFound(location=request.sdiapi.mgmt_path(context))
class AddFileSchema(FilePropertiesSchema):
file = colander.SchemaNode(
deform.schema.FileData(),
widget = file_upload_widget,
missing = colander.null,
)
@colander.deferred
def name_or_file(node, kw):
def _name_or_file(node, struct):
if not struct['file'] and not struct['name']:
raise colander.Invalid(node, 'One of name or file is required')
if not struct['name']:
filename = struct['file'].get('filename')
if filename:
name_node = file_name_node.bind(
context=kw['context'], request=kw['request']
)
name_node.validator(node['file'], filename)
def widget(self):
""" If a widget is not assigned directly to a field, this
function will be called to generate a default widget (only
once). The result of this function will then be assigned as
the ``widget`` attribute of the field for the rest of the
lifetime of this field. If a widget is assigned to a field
before form processing, this function will not be called."""
wdg = getattr(self.schema, "widget", None)
if wdg is not None:
return wdg
widget_maker = getattr(self.schema.typ, "widget_maker", None)
if widget_maker is None:
widget_maker = schema.default_widget_makers.get(
self.schema.typ.__class__
)
if widget_maker is None:
for (cls, wgt) in schema.default_widget_makers.items():
if isinstance(self.schema.typ, cls):
widget_maker = wgt
break
if widget_maker is None:
widget_maker = widget.TextInputWidget
return widget_maker(item_css_class=self.default_item_css_class())
)
def view_file(context, request):
return context.get_response(request=request)
@mgmt_view(
context=IFile,
name='view',
tab_title='View',
permission='sdi.view'
)
def view_tab(context, request):
return HTTPFound(location=request.mgmt_path(context))
class AddFileSchema(FilePropertiesSchema):
file = colander.SchemaNode(
deform.schema.FileData(),
widget = file_upload_widget,
missing = colander.null,
)
@colander.deferred
def name_or_file(node, kw):
def _name_or_file(node, struct):
if not struct['file'] and not struct['name']:
raise colander.Invalid(node, 'One of name or file is required')
if not struct['name']:
filename = struct['file'].get('filename')
if filename:
name_node = file_name_node.bind(
context=kw['context'], request=kw['request']
)
name_node.validator(node['file'], filename)
name = schema.SchemaNode(schema.String())
dates = DatesSchema()
class FileUploads(schema.SequenceSchema):
file = schema.SchemaNode(schema.FileData())
class MySchema(schema.MappingSchema):
name = schema.SchemaNode(schema.String(), description=LONG_DESC)
title = schema.SchemaNode(schema.String(),
validator=colander.Length(max=10),
description=LONG_DESC)
password = schema.SchemaNode(schema.String(),
validator=colander.Length(min=5))
cool = schema.SchemaNode(schema.Boolean(), default=True)
series = SeriesSchema()
color = schema.SchemaNode(schema.String(),
validator=colander.OneOf(('red', 'blue')))
uploads = FileUploads()
def validate_form(form, value):
if value['name'] != value['title']:
exc = exception.Invalid(form, 'Name does not match title')
exc['title'] = 'Does not match title'
exc['name'] = 'Does not match name'
raise exc
def form_view(request):
# Create a schema; when the form is submitted, we want to assert
# that the name must match the title; we use a validator for the
# entire form by assigning it a validator
schema = MySchema(validator=validate_form)