I like programming in Python. I like learning about it, I like using it, and so I wish to share it.

Thursday, February 19, 2009

Install python-ldap with easy_install

Kept getting an error from gcc. Until finally I figured out to do:


sudo apt-get install libldap2-dev

And then it was able to finally compile.

Wednesday, February 18, 2009

Learning Plone Fast Forward

Currently developing a system to manage tasks using plone as it's base. Because of that I've recently learned a lot but have not had the time to properly document all that I've learned. Here's a few snippet of what I've learned.

How to add pagination
First you should define what is the data you want to paginate and also include the require macro into your template.


<div class="eventlist"
tal:define="results options/events;
Batch python:modules['Products.CMFPlone'].Batch;
b_size python:10;
b_start python:0;
b_start request/b_start | b_start;">


and then you need to make sure the list contain data and apply the list to the macro.

<div tal:condition="results"
tal:define="batch python:Batch(results, b_size, int(b_start), orphan=1);">


And of course after that you need to loop through all the data of the page.

<div tal:repeat="event batch">


And do you want to display the list of pages and allow users to navigate them?

<div metal:use-macro="here/batch_macros/macros/navigation" />


How to limit the user interface based on user groups
First we have to find out whether the user is in the said group.

def ksnview(self):
mt = getToolByName(self, 'portal_membership')
if mt.isAnonymousUser(): # the user has not logged in
return False
else:
gt = getToolByName(self, 'portal_groups')
member = mt.getAuthenticatedMember()
ksngroup = gt.getGroupById('KSN')
if ksngroup and member.id in ksngroup.getAllGroupMemberIds():
return True
return False

And then do a tal:condition on the style to hide what they do not need.

How to do query and also sort them
First get an instance of the portal_catalog

catalog = getToolByName(self.context, 'portal_catalog')

After that get the searchResult

event_brains = catalog.searchResults(sort_on="end",getStatus={'query':['Not Started','In Progress','Delayed'],'operator':'or'},portal_type={'query':['Event','Task'],'operator':'or'},path=path, **kw)


**Some of the above use the getToolByName function which should be added at the top like this

from Products.CMFCore.utils import getToolByName

Friday, February 6, 2009

Plone GenericSetup

GenericSetup is a cool feature in Plone that can be used to create profiles for products that can be imported when the product is installed. The original reference which I used can be found here. Basically you can setup one site exactly how you want it to be with all the details on the folders structures, portlets and permissions even. And then you can export the profile of that portal so that it can be imported into any other sites you might be developing.

To export it go to the ZMI, under portal_setup->export. At the very bottom there is an "Export all" button. If that is pressed then you'll be able to download a tarball which contain all the specification needed to recreate the portal you have just made. You won't need all of them but you can use whichever one you need.

Those files would go under profiles/default directory in a common archgenxml product. I've had problems customizing portlets because the exported portlets.xml gave a 'ConstratintNotSatisfied' error. Found out from here that I needed to delete some entries in the tag, namely:


<property name=”name” />
<property name=”root” />

Also learnt from there how to remove portlets:

<assignment remove=”true” name=”login” category=”context” key=”/”
manager=”plone.leftcolumn” type=”portlets.Login” />
<assignment remove=”true” name=”calendar” category=”context” key=”/”
manager=”plone.rightcolumn” type=”portlets.Calendar” />
<assignment remove=”true” name=”events” category=”context” key=”/”
manager=”plone.rightcolumn” type=”portlets.Events” />
<assignment remove=”true” name=”news” category=”context” key=”/”
manager=”plone.rightcolumn” type=”portlets.News” />
<assignment remove=”true” name=”recent-items” category=”context” key=”/”
manager=”plone.rightcolumn” type=”portlets.recent” />
<assignment remove=”true” name=”review-list” category=”context” key=”/”
manager=”plone.rightcolumn” type=”portlets.review” />