Don't you think when the new attribute is compulsory it can generate an
error in default display view?

I had this problem, but I found a way by adding a migration step where I
generate the new attributes on older objects.

It took me quite some time to figure out that the "default" value of a
field is None if not explicitly mentioned in the schema. Moreover, the
_type attribute of field can also be None (in case of Choice with
vocabulary => where the default has to be mentioned, but I don't know how
to force a _type then?)
I give you my module for you to see what I mean! (it will be on github soon
... on the ageliaco.rd2 project )

See you!
serge





2013/10/31 Steve McMahon <[email protected]>

> By my experience, you'll have no problem with Dexterity's default display
> view, which should display the default value if an attribute is missing.
> The problem would be with a custom template or other view. If it simply
> assumed that the new attribute was present, you'd get an error where it
> wasn't.
>
>
> On Thu, Oct 31, 2013 at 9:00 AM, Serge Renfer <[email protected]>wrote:
>
>> Thank you for your answer!
>>
>> This means, that default view form will check if the property is present
>> before trying to show it and :
>>
>>    - either won't show anything if it's an old object (without that
>>    property, from the previous schema)
>>    - or generating the default value for it for that object
>>
>> Or is it that I have to override the default view to have my own template
>> that check properties on the object to show only the ones this object has?
>>
>> Thanks!
>> serge
>>
>>
>> 2013/10/31 Steve McMahon <[email protected]>
>>
>>> The basic answer is that you typically don't need to worry about it
>>> beyond making sure you set a reasonable default in the declaration.
>>>
>>> After that, the only thing you really need to do is have some defensive
>>> coding in anything that tries to use the attribute (like a template) to do
>>> something reasonable if its absent.
>>>
>>>
>>> On Thu, Oct 31, 2013 at 12:51 AM, Serge Renfer 
>>> <[email protected]>wrote:
>>>
>>>> Hello,
>>>>
>>>> I've been searching for doc on how to update existing objects when for
>>>> example you add a property in dexterity interface!
>>>>
>>>> I'm quite sure a doc exists on that matter, but I can't get a hold on
>>>> it!
>>>>
>>>> If any of you could give me pointers on that!
>>>>
>>>> Thanks!
>>>> serge
>>>>
>>>> _______________________________________________
>>>> Product-Developers mailing list
>>>> [email protected]
>>>> https://lists.plone.org/mailman/listinfo/plone-product-developers
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Product-Developers mailing list
>>> [email protected]
>>> https://lists.plone.org/mailman/listinfo/plone-product-developers
>>>
>>>
>>
>
> _______________________________________________
> Product-Developers mailing list
> [email protected]
> https://lists.plone.org/mailman/listinfo/plone-product-developers
>
>
import logging
from zope.schema import getFieldsInOrder
from Products.CMFCore.utils import getToolByName
from interface import ICycle
from zope.component import getUtility
from plone.dexterity.interfaces import IDexterityFTI

PROFILE_ID='profile-ageliaco.rd2:default'

def convert_to_new_cycle(context, logger=None):
    """Method to convert old cycle objects to new schema (more attributes).

    When called from the import_various method, 'context' is
    the plone site and 'logger' is the portal_setup logger.

    But this method will be used as upgrade step, in which case 'context'
    will be portal_setup and 'logger' will be None.

    """
    if logger is None:
        # Called as upgrade step: define our own logger.
        logger = logging.getLogger('ageliaco.rd2')

    # Run the catalog.xml step as that may have defined new metadata
    # columns.  We could instead add <depends name="catalog"/> to
    # the registration of our import step in zcml, but doing it in
    # code makes this method usable as upgrade step as well.
    # Remove these lines when you have no catalog.xml file.
    setup = getToolByName(context, 'portal_setup')
    setup.runImportStepFromProfile(PROFILE_ID, 'catalog')

    catalog = getToolByName(context, 'portal_catalog')
    brains = catalog(portal_type='ageliaco.rd2.cycle')
    schema = getUtility(IDexterityFTI, name='ageliaco.rd2.cycle').lookupSchema()
    count = 0
    for content in brains:
        changed = False
        obj = content.getObject()
        fields = getFieldsInOrder(schema)
        for id, field in fields:
            #if ICycle.providedBy(field):
            try:
                attr = getattr(obj,id)
                if attr==None or type(attr)==None:
                    changed = True
                    print id, obj
                    default = field.default
                    if default==None:
                        default = field._type()
                    #import pdb; pdb.set_trace()
                    setattr(obj, id, default)
                
            except:
                changed = True
                logger.info("Could not change this field (%s) in this cycle : %s !"id,obj))
        if changed:
            count += 1
    setup.runImportStepFromProfile(PROFILE_ID, 'catalog')
    logger.info("%s Cycle objects converted." % count)
_______________________________________________
Product-Developers mailing list
[email protected]
https://lists.plone.org/mailman/listinfo/plone-product-developers

Reply via email to