| Description | Code |
|---|---|
| Loop trough folder contents, deleting objects along the way |
contents = o.getFolderContents() for x in contents: o.manage_delObjects([x.id]) |
| PopupReferenceWidget for Archetypes Schema |
from Products.PopupReferenceWidget.Widget import * ReferenceField('customer', searchable=1, required=1, relationship='requestor', allowed_types=("Contact",), widget=PopupReferenceWidget(lable='Customer',initial_location='/Hive/contacts',addable=1,destination='contacts'), ), |
| Check existence of object |
if hasattr(context, myId): return getattr(context, myId) return getattr(context, myId, 0) zpt style: tal:condition="exists:fa/links" |
| Try and accept code |
try: u=context.DestinationURL() except: u=REQUEST['URL1'] REQUEST.RESPONSE.redirect(u+'/manage_main?update_menu=1') |
| Redirect Page | REQUEST.RESPONSE.redirect(u+'/manage_main?update_menu=1') |
| Transaction Note Plone |
from Products.CMFPlone import transaction_note transaction_note('Cloned %s: %s. New object has id of %s and is located at %s' % (o.getTypeInfo().getId(), context.getId(), o.getId(), o.absolute_url())) |
| Replace | var = var.replace('ProductRequest', '') |
| First 255 characters of a string | descript = descrip[0:255] |
| Protect content by workflow role | tal:condition="python: checkPermission('Review portal content', here)" |
| Excel File Creation |
request = container.REQUEST RESPONSE = request.RESPONSE table_example = """<table> <tr> <td colspan="4" align="center"><h3>This is the Header for a Spreadsheet</h3></td> </tr> <tr bgcolor="#CCCCCC"> <td align="center"><b>Data Column 1</b></td> <td align="center"><b>Data Column 2</b></td> <td align="center"><b>Data Column 3</b></td> <td align="center"><b>Data Column 4</b></td> </tr> <tr> <td align="center">Information</td> <td align="center">More info</td> <td align="center">...</td> <td align="center"> </td> </tr> <tr> <td align="center"><font color="#CC0000">This colour is from a <font> tag</font></td> <td align="center"><span style="color:#CC0000;">This colour is from a <span> tag</span></td> <td align="center">...</td> <td align="center"> </td> </tr> </table>""" RESPONSE.setHeader("Content-type","application/vnd.ms-excel") RESPONSE.setHeader("Content-disposition","attachment;filename=examplespreadsheet.xls") return table_example |
| Conditional SELECTED on option form element | tal:attributes="selected python:test(year == request.fundee_year, 1, None) |
| SELECT distinct indexes from catalog |
tal:repeat="value python:here.portal_catalog.uniqueValuesFor('Type')" tal:content="value" |
| Another Reference Widget |
ReferenceField('feature_event_6', multiValued=0, relationship='Rel1', widget=ReferenceBrowserWidget(default_search_index='SearchableText', description='This is the first field. Pick an object.')), |
| Find ID of template | template.getId() |
| Use autocompletewidget to manage keywords | LinesField('test_field', searchable=1, required=0, mutator = 'setSubject', accessor = 'Subject', edit_accessor = 'getSubjectRaw', vocabulary='getSubjectVocab', widget=AutocompleteWidget(label='Test Widget', description='Test this', ), enforceVocabulary=0, ), Method to grab put our keywords in a dictionary: security.declareProtected(View, 'getSubjectVocab') def getSubjectVocab(self): """Get subject (keywords) vocabulary""" catalog = getToolByName(self, 'portal_catalog') return catalog.uniqueValuesFor('Subject') Note: you must also set the autocomplete.pt to read from the accessor, not "value", or field will be blank when user edits it. metal:fill fill-slot="widget_body" tal:define="vocab python:field.Vocabulary(here); widget_value python:test(field.type=='lines', ','.join(accessor()), accessor())" |
| protect field by permission see write_permission |
ATEventSchema = ATContentTypeSchema.copy() + Schema(( DateTimeField('startDate', required=True, searchable=False, accessor='start', write_permission = ChangeEvents, default_method=DateTime, languageIndependent=True, widget = CalendarWidget( description= "", description_msgid = "help_event_start", label="Event Starts", label_msgid = "label_event_start", i18n_domain = "plone")), |
| get user id |
mtool = context.portal_membership member = mtool.getAuthenticatedMember() member_id = member.getId() return member_id |
| iterate over submitted form fields |
for k,v in request.form.items(): OUT += str(k) + ':' + str(v) + '\n' return OUT |