Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Mario Orlandi

« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS 

review_state

// retrieve review state
from Products.CMFCore.utils import getToolByName
wtool = getToolByName(context, "portal_workflow")
wf_state = wtool.getInfoFor(obj,'review_state',None)

object_delete

// object_delete
from AccessControl import Unauthorized
from Products.CMFPlone.utils import transaction_note
from Products.CMFPlone import PloneMessageFactory as _

REQUEST = context.REQUEST
if REQUEST.get('REQUEST_METHOD', 'GET').upper() == 'GET':
    raise Unauthorized, 'This method can not be accessed using a GET request'

parent = context.aq_inner.aq_parent
parent.manage_delObjects(context.getId())

message = _(u'${title} has been deleted.',
            mapping={u'title' : context.title_or_id()})
transaction_note('Deleted %s' % context.absolute_url())

context.plone_utils.addPortalMessage(message)
return state.set(status = 'success')

Result Object Methods

// Assuming that we have set result to being a result object we can use the following methods:

len(result)
     this will show the number rows returned (which would be 3 in the example above).

result.names()
    a list of all the column headings, returning a list containing emp_id, first, last and salary

result.tuples()
    returns a list of tuples in our example:

    [(43, 'Bob', 'Roberts', 50000),
     (101, 'Cheeta', 'leCat', 100000),
     (99, 'Jane', 'Junglewoman', 100001)]

result.dictionaries()
    will return a list of dictionaries, with one dictionary for each row:

    [{'emp_id': 42, 'first': 'Bob','last': 'Roberts', 'salary': 50000},
     {'emp_id': 101, 'first: 'Cheeta', 'last': 'leCat', 'salary': 100000},
     {'emp_id': 99, 'first': 'Jane', 'last': 'Junglewoman', 'salary': 100001}]

result.data_dictionary()
    returns a dictionary describing the structure of the results table. 

The dictionary has the key name, type, null and width. 
Name and type are self explanatory, null is true if that field may contain a null value and width is the width in characters of the field. 
Note that null and width may not be set by some Database Adapters.

result.asRDB()
    displays the result in a similar way to a relational database. 

The DTML below displays the result below:
    <pre>
      <dtml-var "list_all_employees().asRDB()">
    </pre>

    ... displays ...

    emp_id first last salary
    42 Bob Roberts 50000
    101 Cheeta leCat 100000
    99 Jane Junglewoman 100001

result[0][1]
    return row 0, column 1 of the result, bob in this example. 

Be careful using this method as changes in the schema will cause unexpected results. 

Dead Content Type Inspector

print "Dead Content Type Inspector"
print

for i in context.portal_catalog.uniqueValuesFor('portal_type'):
   if i in context.portal_types: continue
   print i
   results = context.portal_catalog(portal_type=i)
   for i in results:
       print i.getURL()
   print 
   print

return printed

Db Connection

// eGenix mxODBC Database Connection: connection string sample
DSN=LocalServer;
DATABASE=GammaMedidata;
UID=sa;
PWD=******;


// eGenix mxODBC Database Connection: connection string sample
DRIVER=SQL Server;
SERVER=(local);
UID=sa;
PWD=****;
DATABASE=wam


// MySQL connection string sample
dbcobraf@www2.cobraf.com cobraf 'password'

contentItems()

p.Members.contentItems(filter={'portal_type':['Folder',]})

// Search a specific Purchase, then remove all SalesAttachment from it
purchaseBrains = context.portal_catalog.searchResults( portal_type='Purchase', getPurchaseId = 1000 )
if len(purchaseBrains)==1:
    purchase = purchaseBrains[0].getObject()
    print purchase 
    objs = purchase.contentItems(filter={'portal_type':['SalesAttachment',]})
    ids = []
    for obj in objs:
        id = obj[0]
        print id
        ids.append(id)
    print ids
    purchase.manage_delObjects(ids=ids)
return printed

get folder object

portal = context.portal_url.getPortalObject()
photosFolder = getattr(portal,'photos')


photosFolder = portal.restrictedTraverse('projects/picture-masking-service/incoming-pictures/')

getHomeFolder

// retrieving the home folder of the current user
m = pm.getAuthenticatedMember()
pm = context.portal_membership
mf = pm.getHomeFolder()

// retrieving the home folder of the a specific user
mf = pm.getHomeFolder(userId)

redirect

request = context.REQUEST
...
request.RESPONSE.redirect(request.HTTP_REFERER)
...
« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS