Thursday, April 21, 2011

How does one default to current date and time for content publishing?

In this link, http://plone.org/documentation/how-to/set-default-datetimefield-current-date-time it describes how to do this with new Schema attributes. I could update all umpteen content types in our system use this method, but I would prefer something a bit less work intensive, since if I have to change umpteen content types it will be all too easy to make a mistake.

From stackoverflow
  • I could just make 'published' be the default work flow state. That should address the issue.

    pydanny : Unfortunately, this solution is not acceptable. My task is to make the default publication date on new content items to be the current date. Not create a work flow change.
  • maybe a js soln? have an onload event that looks for datetime widgets by id (or one of those common attributes) and then reset the time based on the browser time. You can filter new vs edit based on whether or not a non-prefilled required attribute has been filled yet (i.e. Title)

    pydanny : So you mean use a bit of JavaScript do default empty effectiveDate fields to the current date? I'll give that a try.
    pydanny : This method worked perfectly! Entirely view based so I did not have to modify umpteen content types!
  • You could use archetypes.schemaextender to modify those types with an adapter:

    in your configure.zcml

    <adapter
        factory=".adapters.DefaultDateModifier"
        name="my-package-defaultdate"
        />
    

    in the adapters.py

    class DefaultDateModifier(object):
        """DefaultDateModifier adapter
        """
        # XXX optionally adapt your content items iface here
        adapts(ATCTMixin)
        implements(ISchemaModifier)
    
        def fiddle(self, schema):
            # TODO switch out the default_method here...
            pass
    
        def __init__(self, context):
            self.context = context
    
    pydanny : How would that work against many (50+) Plone 2.5 content types running in Plone 3.x? Would I have to modify all of the many content types? Because that is what I would prefer not to do.
    claytron : If all the types had a common interface or subclass (ATCTMixin for example) then you would just need to extend that once. Then all the types get the addition for free. You can also tag all those types with an interface via zcml if it came down to it. Then extend via that interface.
    pydanny : This would be a great fix *but* we don't have a common interface or subclass.

0 comments:

Post a Comment