def fiddle(self, schema): """ """ dgf = schema['SampleData'] temp_var = [i for i in dgf.columns] if "ClientReference" not in temp_var: temp_var.append("ClientReference") if "Strain" not in temp_var: temp_var.append("Strain") if "Lot" not in temp_var: temp_var.append("Lot") if "CultivationBatch" not in temp_var: temp_var.append("CultivationBatch") if "ClientStateLicenseID" not in temp_var: temp_var.append("ClientStateLicenseID") if "SamplePoint" in temp_var: temp_var.remove("SamplePoint") if "SampleMatrix" in temp_var: temp_var.remove("SampleMatrix") if "ContainerType" in temp_var: temp_var.remove("ContainerType") if "ReportDryMatter" in temp_var: temp_var.remove("ReportDryMatter") dgf.columns = tuple(temp_var) strain_vocab = Vocabulary_Strain() dgf.widget.columns["ClientReference"] = Column('ClientReference') dgf.widget.columns["Strain"] = SelectColumn('Strain', vocabulary=strain_vocab) dgf.widget.columns["Lot"] = Column('Lot') dgf.widget.columns["CultivationBatch"] = Column('Cultivation Batch') cslid_vocab = Vocabulary_ClientStateLicenseID() dgf.widget.columns["ClientStateLicenseID"] = SelectColumn( "Client's State License ID", vocabulary=cslid_vocab) if "SamplePoint" in dgf.widget.columns.keys(): del dgf.widget.columns["SamplePoint"] if "SampleMatrix" in dgf.widget.columns.keys(): del dgf.widget.columns["SampleMatrix"] if "ContainerType" in dgf.widget.columns.keys(): del dgf.widget.columns["ContainerType"] if "ReportDryMatter" in dgf.widget.columns.keys(): del dgf.widget.columns["ReportDryMatter"] return schema
def update_config_schema(baseSchema): specificSchema = Schema( ( # field for defining title that will be used for item created in the Council DataGridField( # very strange bug when using a field name ending with 'References'... name='archivingRefs', widget=DataGridField._properties['widget']( description="ArchivingRefs", description_msgid="archiving_refs_descr", columns={ 'row_id': Column("Archiving reference row id", visible=False), 'code': Column("Archiving reference code"), 'label': Column("Archiving reference label"), 'restrict_to_groups': MultiSelectColumn( "Archiving reference restrict to selected groups", vocabulary="listActiveOrgsForArchivingRefs"), 'active': SelectColumn("Archiving reference active?", vocabulary="listBooleanVocabulary", default='1'), }, label='ArchivingRefs', label_msgid='MeetingLiege_label_archivingRefs', i18n_domain='PloneMeeting', visible={ 'view': 'invisible', }), allow_oddeven=True, default=(), # do not use 'finance_advice' column for now, replaced (definitively?) # by field 'financeAdvice' columns=('row_id', 'code', 'label', 'restrict_to_groups', 'active'), allow_empty_rows=False, write_permission=WriteRiskyConfig, ), ), ) completeConfigSchema = baseSchema + specificSchema.copy() return completeConfigSchema
'SamplePoint', 'SampleMatrix', 'SampleType', # not a schema field! 'ContainerType', # not a schema field! 'ReportDryMatter', 'Priority', 'Analyses', # not a schema field! 'Profiles' # not a schema field! ), widget=DataGridWidget( label=_('Samples'), columns={ 'ClientSampleID': Column('Sample ID'), 'SamplingDate': DateColumn('Sampling Date'), 'DateSampled': DateColumn('Date Sampled'), 'SamplePoint': SelectColumn( 'Sample Point', vocabulary='Vocabulary_SamplePoint'), 'SampleMatrix': SelectColumn( 'Sample Matrix', vocabulary='Vocabulary_SampleMatrix'), 'SampleType': SelectColumn( 'Sample Type', vocabulary='Vocabulary_SampleType'), 'ContainerType': SelectColumn( 'Container', vocabulary='Vocabulary_ContainerType'), 'ReportDryMatter': CheckboxColumn('Dry'), 'Priority': SelectColumn( 'Priority', vocabulary='Vocabulary_Priority'), 'Analyses': LinesColumn('Analyses'), 'Profiles': LinesColumn('Profiles'), } ) )
'SampleType', # not a schema field! 'ContainerType', # not a schema field! 'Analyses', # not a schema field! 'Profiles' # not a schema field! ), widget=DataGridWidget( label=_('Samples'), columns={ 'ClientSampleID': Column('Sample ID'), 'SamplingDate': DateColumn('Sampling Date'), 'DateSampled': DateColumn('Date Sampled'), 'SamplePoint': SelectColumn('Sample Point', vocabulary='Vocabulary_SamplePoint'), 'SampleMatrix': SelectColumn('Sample Matrix', vocabulary='Vocabulary_SampleMatrix'), 'SampleType': SelectColumn('Sample Type', vocabulary='Vocabulary_SampleType'), 'ContainerType': SelectColumn('Container', vocabulary='Vocabulary_ContainerType'), 'Analyses': LinesColumn('Analyses'), 'Profiles': LinesColumn('Profiles'), })) Errors = LinesField('Errors', widget=LinesWidget( label=_('Errors'),
allow_reorder=True, allow_empty_rows=False, columns=('LicenseType', 'LicenseID', 'LicenseNumber', 'Authority'), default=[{ 'LicenseType': '', 'LicenseID': '', 'LicenseNumber': '', 'Authority': '' }], widget=DataGridWidget(description=_( "Details of client licenses that authorise them to operate, sometimes to be included on documentation." ), columns={ 'LicenseType': SelectColumn( 'License Type', vocabulary='Vocabulary_LicenseType'), 'LicenseID': Column('License ID'), 'LicenseNumber': Column('Registation Number'), 'Authority': Column('Issuing Authority') })), )) schema['AccountNumber'].write_permission = ManageClients schema['title'].widget.visible = False schema['description'].widget.visible = False schema['EmailAddress'].schemata = 'default'
class FormDataGridField(fieldsBase.BaseFormField): """ A PloneFormGen Form Field """ security = ClassSecurityInfo() ####################### # We could derive our schema from several schema defined in # fieldsBase. In general, choose by which has a matching data # type for the default field value. # # DataGridField won't match any, so we'll use the BaseFieldSchema, # which has no default, and provide a getFgDefault method to make # up for it. schema = fieldsBase.BaseFieldSchema.copy() + Schema(( # We'll make DataGridField eat its own dog food by using it # to get our column definitions and defaults. DataGridField( 'columnDefs', searchable=False, required=True, columns=LIST_OF_COLUMNS, default=[ { 'columnId': 'column1', 'columnTitle': 'Column One', 'columnDefault': '', 'columnType': 'String', 'columnVocab': '' }, ], widget=DataGridWidget( label='Column Definitions', i18n_domain="pfgdatagrid", label_msgid="label_column_defs_text", description=""" Specify a unique identifier and a title for each column you wish to appear in the datagrid. The default value is optional. """, description_msgid="help_column_defs_text", columns={ 'columnId': Column('Column Id'), 'columnTitle': Column('Column Title'), 'columnDefault': Column('Default Value'), 'columnType': SelectColumn('Column Type', vocabulary='supportedColumnTypes'), 'columnVocab': LinesColumn('Vocabulary (for Select col. only)'), }, ), ), # A few DFG-specific fields. We'll need to provide # mutator and accessor methods that tie these # to values in the widget BooleanField( 'allowDelete', searchable=False, default='1', widget=BooleanWidget( label='Allow Row Deletion', i18n_domain="pfgdatagrid", label_msgid="label_allow_delete_text", ), ), BooleanField( 'allowInsert', searchable=False, default='1', widget=BooleanWidget( label='Allow Row Insertion', i18n_domain="pfgdatagrid", label_msgid="label_allow_insert_text", ), ), BooleanField( 'allowReorder', searchable=False, default='1', widget=BooleanWidget( label='Allow Row Reordering', i18n_domain="pfgdatagrid", label_msgid="label_allow_reorder_text", ), ), )) finalizeATCTSchema(schema, folderish=True, moveDiscussion=False) content_icon = 'BasicField.gif' meta_type = \ portal_type = 'FormDataGridField' archetype_name = 'DataGrid Field' typeDescription = 'A flexible datagrid field.' typeDescMsgId = 'datagridformfield_description' ####################### # Every form field must have an fgField attribute that is a field # definition created outside of the normal schema. # Since this needs to be an instance attribute, we'll set it up # in the __init__ method. def __init__(self, oid, **kwargs): """ initialize class """ fieldsBase.BaseFormField.__init__(self, oid, **kwargs) # set a preconfigured field as an instance attribute self.fgField = DataGridField( 'fg_datagrid_field', searchable=False, required=False, write_permission=View, widget=DataGridWidget(), columns=LIST_OF_COLUMNS, allow_delete=True, allow_insert=True, allow_reorder=True, ) ####################### # Mutators and accessors that tie items in the form field's # schema to attributes of the fgField field. # Most are simple. # setColumnDefs is more complex because it's translating the # data from the columnDefs DataGrid for multiple uses. security.declareProtected(View, 'supportedColumnTypes') def supportedColumnTypes(self, **kw): keys = SUPPORTED_COLUMN_TYPES_MAPPING.keys() return DisplayList([(key, key) for key in keys]) security.declareProtected(ModifyPortalContent, 'setColumnDefs') def setColumnDefs(self, value, **kwa): """ mutator for columnDefs """ try: myval = [col for col in value if col.get('columnId')] except AttributeError: # When importing PloneFormGen form TTW via "Actions -> Import", # value is a string, we need to convert it to a list first. # XXX: maybe this should be fixed elsewhere? # replace single quotes with double quotes so json.loads doesn't # complain value = value.replace("'", '"') value = [json.loads(val) for val in value.split('\n')] myval = [col for col in value if col.get('columnId')] self.columnDefs = myval self.fgField.columns = [col['columnId'] for col in myval] res = {} for col in myval: klass = SUPPORTED_COLUMN_TYPES_MAPPING.get(col['columnType'], None) if klass is not None: if col['columnType'] == 'Select': res[col['columnId']] = klass( col['columnTitle'], default=col['columnDefault'], vocabulary=SimpleDynamicVocabulary(col['columnVocab'])) elif col['columnType'] == 'SelectVocabulary': view_name = col['columnVocab'][0] view = self.restrictedTraverse(view_name, None) if view is not None: vocab = view() res[col['columnId']] = klass( col['columnTitle'], default=col['columnDefault'], vocabulary=vocab) else: res[col['columnId']] = klass( col['columnTitle'], default=col['columnDefault'], vocabulary=SimpleDynamicVocabulary([])) elif col['columnType'] == 'DynamicVocabulary': view_name = col['columnVocab'][0] res[col['columnId']] = klass(col['columnTitle'], default=col['columnDefault'], vocabulary=view_name) else: res[col['columnId']] = klass(col['columnTitle'], default=col['columnDefault']) self.fgField.widget.columns = res security.declareProtected(ModifyPortalContent, 'setAllowDelete') def setAllowDelete(self, value, **kwa): """ set allow_delete flag for field """ if type(value) == BooleanType: self.fgField.allow_delete = value else: self.fgField.allow_delete = value == '1' security.declareProtected(View, 'getAllowDelete') def getAllowDelete(self, **kw): """ get allow_delete flag for field """ return self.fgField.allow_delete security.declareProtected(ModifyPortalContent, 'setAllowInsert') def setAllowInsert(self, value, **kwa): """ set allow_insert flag for field """ if type(value) == BooleanType: self.fgField.allow_insert = value else: self.fgField.allow_insert = value == '1' security.declareProtected(View, 'getAllowInsert') def getAllowInsert(self, **kw): """ get allow_insert flag for field """ return self.fgField.allow_insert security.declareProtected(ModifyPortalContent, 'setAllowReorder') def setAllowReorder(self, value, **kwa): """ set allow_reorder flag for field """ if type(value) == BooleanType: self.fgField.allow_reorder = value else: self.fgField.allow_reorder = value == '1' security.declareProtected(View, 'getAllowReorder') def getAllowReorder(self, **kw): """ get allow_reorder flag for field """ return self.fgField.allow_reorder ####################### # Due to the very-specific nature of the DataGridField # default, we're going to provide it via an accessor method. # If the data structure was common, we could have done it in # the form field schema with an fgDefault field. def getFgDefault(self, **kwargs): """ supply defaults for fgField datagrid as a list with a single dictionary """ res = {} for col in self.columnDefs: if col.get('columnId'): res[col['columnId']] = col['columnDefault'] return [res] ####################### # The string representation of the DataGridField value isn't suitable # for public consumption. So, let's massage it by overriding # the htmlValue method def htmlValue(self, REQUEST): """ return from REQUEST, this field's value in html. """ value = REQUEST.form.get(self.__name__, 'No Input') header = '' for col in self.columnDefs: header += "<th>%s</th>" % col['columnTitle'] res = '<table class="listing"><thead><tr>%s</tr></thead><tbody>' % header for adict in value: if adict.get('orderindex_', '') != 'template_row_marker': res += "<tr>" for col in self.columnDefs: akey = col['columnId'] if col['columnType'] == "File": file = adict[akey] file.seek(0) fdata = file.read() filename = file.filename mimetype, enc = guess_content_type( filename, fdata, None) out = "%s %s: %s bytes" % (filename, mimetype, len(fdata)) res = "%s\n<td>%s</td>" % (res, cgi.escape(out)) else: res = "%s\n<td>%s</td>" % (res, cgi.escape( adict[akey])) res += "</tr>" return "%s</tbody></table>" % res
#'Priority', 'Analyses', # not a schema field! 'Profiles' # not a schema field! ), widget=DataGridWidget( label=_('Samples'), columns={ 'ClientSampleID': Column('Sample ID'), #'SamplingDate': DatetimeColumn('Sampling Date'), 'DateSampled': DatetimeColumn('Date Sampled'), 'Sampler': Column('Sampler'), 'SamplePoint': SelectColumn('Sample Point', vocabulary='Vocabulary_SamplePoint'), 'SampleType': SelectColumn('Sample Type', vocabulary='Vocabulary_SampleType'), #'Container': SelectColumn( # 'Container', vocabulary='Vocabulary_Container'), 'ReportDryMatter': CheckboxColumn('Dry'), #'Priority': SelectColumn( # 'Priority', vocabulary='Vocabulary_Priority'), 'Analyses': LinesColumn('Analyses'), 'Profiles': LinesColumn('Profiles'), })) Errors = LinesField('Errors', widget=LinesWidget(