Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Events with Plone Form Gen

I am trying to create an Event content type using Plone Form Gen. I have been using this tutorial in order to do this.

When creating an Event content type using the Add New... menu, you are given two fields to fill in that are start and end dates for the event, I would like my form to pull the information from these fields and apply it to the event content type I am using to create it.

My problem as I understand it is described with examples below:

The custom script adapter script contains the following:

obj.setDescription(form['replyto'])

I can see that it gets the contents for the Description of the Event content type from the following:

<input id="replyto" class="" type="text" size="30" name="replyto" />

The date/Time field when added to a PFG form, is made up of multiple <select> inputs and not just one like the above, I guess this means there isn't a simple obj.setEndDate() command for this... Although with no way to reference the select boxes I'm kind of stuck.

Does anyone know if it is possible to create an Event content type and specify start and end dates on it, using Plone Form Gen?

Edit

Using this link I have gotten around the original issue but I have run into more problems

I have adapted my script (using the above link) to look like the following:

target = context.viewjobs

form = request.form

from DateTime import DateTime
uid = str(DateTime().millis())

loc = form['location-of-event']

target.invokeFactory("Event", id=uid, title=form['topic'], event_url=loc)

obj = target[uid]

obj.setFormat('text/plain')
obj.setText(form['comments'])
obj.setDescription(form['replyto'])

obj.reindexObject()

(I used event_url just to test as I wasn't having any luck with the event_start option).

It creates the event alright, but when I go to view the event I get:

 Module zope.tales.expressions, line 217, in __call__
Module Products.PageTemplates.Expressions, line 147, in _eval
Module zope.tales.expressions, line 124, in _eval
Module Products.PageTemplates.Expressions, line 74, in boboAwareZopeTraverse
Module OFS.Traversable, line 317, in restrictedTraverse
Module OFS.Traversable, line 285, in unrestrictedTraverse
__traceback_info__: ([], 'location')

AttributeError: location

I have not referenced location anywhere in my script, and when I do, I get the same error.

Any thoughts would be appreciated

like image 281
Dan Avatar asked Apr 03 '13 11:04

Dan


2 Answers

You can simplify your code and avoid the reindex call by doing something like this:

target = context.viewjobs

form = request.form

from DateTime import DateTime
uid = str(DateTime().millis())

target.invokeFactory(
    "Event",
    id=uid,
    title=form['job-title'],
    description=form['description-1'],
    text=form['comments'],
    location=form['location-of-event'],
    startDate=form['start-date'],
    endDate=form['end-date-due-by']
    )

With regard to collecting the starting and ending date. If you use the Date/Time widget and look at the HTML that is generated you will notice there is a hidden input field who's name matches the short-name of the widget. That hidden input contains a full textual representation of what was selected by the various select boxes, thus giving you want you achieved by using a text field but without having to rely on the user to use a specific format.

If you're wondering how to find the names of the various fields to specify in the invokeFactory call, find the python file that defines the content type your trying to create. In the case of the event object it's /Plone/buildout-cache/eggs/Products.ATContentTypes-2.1.8-py2.7.egg/Products/ATContentTypes/content/event.py

Line 32 starts "ATEventSchema = ..." and from there you will see the field names for all the parts of an event.

like image 140
PaulR Avatar answered Sep 18 '22 09:09

PaulR


I managed to resolve this by using a text field and asking the users to enter the date in this format: 2013-12-12, I then used obj.setStartDate(form['name-of-field']) and obj.setEndDate(form['name-of-field']) to set that on the Event.

To get around the Location traceback, I used obj.setLocation() and removed the location line from the invoke method shown in the script above.

Script for anyone who's interested:

target = context.viewjobs

form = request.form

from DateTime import DateTime
uid = str(DateTime().millis())

target.invokeFactory("Event", id=uid, title=form['job-title'])

obj = target[uid]

obj.setFormat('text/plain')
obj.setText(form['comments'])
obj.setDescription(form['description-1'])
obj.setLocation(form['location-of-event'])
obj.setStartDate(form['start-date'])
obj.setEndDate(form['end-date-due-by'])

obj.reindexObject()
like image 44
Dan Avatar answered Sep 20 '22 09:09

Dan