Re: Dexterity DataGridField Default Factory

Simon Richardson <[email protected]>
Newsgroups gmane.comp.web.zope.plone.user
Message-ID <[email protected]>
Hi Sean

Thanks for the response yesterday.  I haven't tried your code snippet 
because I was trying to get the default to work using the supermodel / 
schema approach.

My response to your questions below - including my code snippets:

1. The default factory:

class DefaultStrokesFactory(object):
     implements(IDefaultFactory)

     def __call__(self):
         strokes = []

         strokes = [
                     { 'stroke': '50Free', 'notes': u''},
                     { 'stroke': '100Free', 'notes': u''},
                     { 'stroke': '200Free', 'notes': u''},
                     { 'stroke': '400Free', 'notes': u''},
                     { 'stroke': '800Free', 'notes': u'Female only'},
                     { 'stroke': '1500Free', 'notes': u'Male only'},
                     { 'stroke': '50Back', 'notes': u''},
                     { 'stroke': '100Back', 'notes': u''},
                     { 'stroke': '200Back', 'notes': u''},
                     { 'stroke': '50Breast', 'notes': u''},
                     { 'stroke': '100Breast', 'notes': u''},
                     { 'stroke': '200Breast', 'notes': u''},
                     { 'stroke': '50Fly', 'notes': u''},
                     { 'stroke': '100Fly', 'notes': u''},
                     { 'stroke': '200Fly', 'notes': u''},
                     { 'stroke': '100IM', 'notes': u''},
                     { 'stroke': '200IM', 'notes': u''},
                     { 'stroke': '400IM', 'notes': u''}
             ]
         return strokes

DefaultStrokesFactory = DefaultStrokesFactory()

===============
configure.zxml
===============
     <utility
         name="myswimmingclub.content.DefaultStrokes"
         provides="plone.supermodel.interfaces.IDefaultFactory"
component="myswimmingclub.content.defaults.DefaultStrokesFactory"
     />

==============================
models/swimming_meet.xml
==============================

          <fieldset name="events"
             label="Swimming Meet Events"
             description="The events being raced at the swimming meet">
             <field name="events" type="zope.schema.List"
form:widget="collective.z3cform.datagridfield.DataGridFieldFactory">
                 <title>Events</title>
                 <description/>
                 <value_type 
type="collective.z3cform.datagridfield.DictRow">
                     <title>Events</title>
<schema>myswimmingclub.content.swimming_meet_event.ISwimmingMeetEvent</schema>
<defaultFactory>myswimmingclub.content.defaults.DefaultStrokesFactory</defaultFactory>
                 </value_type>

             </field>
          </fieldset>


2. I'm not sure what you meant by "Does your defaultFactory callable 
suffer from this problem if you call it in a vacuum?"

3. The default factory above returns the 'unhashable type: list' error

I have also tried using tuple for the returned value e.g. return tuple( 
[ 'stroke', 0, 'notes', u'Female only' ] )
but this fails in "_validate" method of 
collective/z3cform/datagridfield/row.py

def _validate(self, value):
         # XXX HACK: Can't call the super, since it'll check to
         # XXX see if we provide DictRow.
         # We're only a dict, so we can't.
         # super(DictRow, self)._validate(value)

         # Validate the dict against the schema
         # Pass 1 - ensure fields are present
         if value is NO_VALUE:
             return
         errors = []
         for field_name in getFields(self.schema).keys():
             if field_name not in value:
                 errors.append(AttributeNotFoundError(field_name, 
self.schema))

         if errors:
             raise WrongContainedType(errors, self.__name__)

         # Pass 2 - Ensure fields are valid
         for field_name, field_type in getFields(self.schema).items():
             field_type.validate(value[field_name])



On 04/03/2015 18:51, Sean Upton wrote:
>
>
> On Mon, Mar 2, 2015 at 7:32 AM, Simon Richardson 
> <[email protected] <mailto:[email protected]>> wrote:
>
>     Hi Plone users
>
>     I'm unable to create a default factory for a datagridfield field,
>     due to running into a
>
>
> What type is your defaultFactory callable returning?  I am assuming 
> that it should be a list of dict, assuming you are using 
> value_type=DictRow in your schema field.
>
>     'unhashable type: list' error
>
>
> Does your defaultFactory callable suffer from this problem if you call 
> it in a vacuum?
>
>
>     Does anyone have an example of a datagridfield with a default
>     value defined?
>
>
> I have no working example (I don't use defaults in any of my grids), 
> but assume something like this might work (untested):
>
> def myTableDefault():
>     #return []  # simplest case
>     some_record = {'title': u'This', 'description': u'That', 'count': 1}
>     return [some_record]
>
> class IMyRowSchema(model.Schema):
>     title = schema.TextLine()
>     description = schema.Text()
>     count = schema.Int()
>
> class IMySchema(model.Schema):
>
>     form.widget(table=DataGridFieldFactory)
>     myfield = schema.Llst(
>         value_type=DictRow(schema=IRowSchema),
>         defaultFactory=myTableDefault
>         )
>
>
>
> Sean

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/

_______________________________________________
Plone-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/plone-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.