Dexterity Content Type DateTime Issue & How to modify Content Type Values

319 views Asked by At

First of all: I am running a Plonce Plone 4.3 (4305) instance with Dexterity Content Types 2.0.7

My approach is to write a Python Script (added via ZMI) which creates my dexterity content type using the methods invokeFactory(...) or typestool.constructContent(..) described here: http://plone.org/documentation/kb/add-content-programmatically (I've written two scripts which do the same task but use different methods - for learning purposes)

Everything is working fine, except when i try to add a DateTime Object to the constructor of both methods above to create my Content Type. The Date field strangly only updates the day and year values. Because of the restrictions in importing libraries inside python i am stuck (with my current knowledge) with this code:

d = DateTime('12/12/2013')

My script returns the date object after completion which looks like this:

2013/12/12 00:00:00 GMT+1

I've written another small script which outputs the Date value after construction and it gives me the same result (which seems to be correct). The resulting Content Type has its day and year field updated correctly but the month value stays on January and raises the following TypeError upon viewing:

TypeError: int() argument must be a string or a number, not 'instancemethod'

I can fix this by editing the month value manually which is not exactly what i want. I guess it its a minor problem with my DateTime object but i am running out of ideas at this point (overall documentation seems to be a bit scattered). I've tried various date formats inside the DateTime constructor but without luck.

I am also not sure how to modifiy my objects custom field values. Plone seems only to provide setTitle() and setDescription() methods. Maybe someone has a good hint.

Thanky you all in advance, regards

2

There are 2 answers

0
dmunicio On

AFAIK, the problem is that DateTime field for dexterity types needs a datetime object, not a DateTime object.

In the invokeFactory, you should pass a datetime object instead of a DateTime object

>>>date=datetime.datetime(2011,1,1)
>>>myobj=target.invokeFactory(type_name="content_type_name", id=id, date=date)

>>from DateTime import DateTime
>>DateTime().month() 
7
>>from datetime import datetime
>>datetime.now().month
7
>>datetime.now().month()
TypeError: 'int' object is not callable 

>>> myobj.date
datetime.datetime(2013, 7, 26, 0, 0) 
>>> myobj.date.month 
7
>>> myobj.date.month() 
TypeError: 'int' object is not callable 
0
Don Keillor On

not sure about the dateTime issue, but check out indexes, indexing and custom index to figure out setting titles and description.

to set your title for instance

@indexer(IFormName)
def titleIndexer(obj):
    return obj.valueFromForm
grok.global_adapter(titleIndexer, name="Title")