code_junkie

How do I search for unpublished Plone content in an IPython debug shell?

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. $ bin/instance shell
  2. $ ipython --profile=zope
  3.  
  4. from Products.CMFPlone.utils import getToolByName
  5. catalog = getToolByName(context, 'portal_catalog')
  6. catalog({'path':'Plone/testing'})
  7.  
  8. from sys import stdin, stdout, exit
  9. import base64
  10. from thread import get_ident
  11. from ZPublisher.HTTPRequest import HTTPRequest
  12. from ZPublisher.HTTPResponse import HTTPResponse
  13. from ZPublisher.BaseRequest import RequestContainer
  14. from ZPublisher import Publish
  15.  
  16. from AccessControl import ClassSecurityInfo, getSecurityManager
  17. from AccessControl.SecurityManagement import newSecurityManager
  18. from AccessControl.User import UnrestrictedUser
  19.  
  20. def loginAsUnrestrictedUser():
  21. """Exemple of use :
  22. old_user = loginAsUnrestrictedUser()
  23. # Manager stuff
  24. loginAsUser(old_user)
  25. """
  26. current_user = getSecurityManager().getUser()
  27. newSecurityManager(None, UnrestrictedUser('manager', '', ['Manager'], []))
  28. return current_user
  29.  
  30. def loginAsUser(user):
  31. newSecurityManager(None, user)
  32.  
  33. def makerequest(app, stdout=stdout, query_string=None, user_pass=None):
  34. """Make a request suitable for CMF sites & Plone
  35. - user_pass = "user:pass"
  36. """
  37. # copy from Testing.makerequest
  38. resp = HTTPResponse(stdout=stdout)
  39. env = {}
  40. env['SERVER_NAME'] = 'lxtools.makerequest.fr'
  41. env['SERVER_PORT'] = '80'
  42. env['REQUEST_METHOD'] = 'GET'
  43. env['REMOTE_HOST'] = 'a.distant.host'
  44. env['REMOTE_ADDR'] = '77.77.77.77'
  45. env['HTTP_HOST'] = '127.0.0.1'
  46. env['HTTP_USER_AGENT'] = 'LxToolsUserAgent/1.0'
  47. env['HTTP_ACCEPT']='image/gif, image/x-xbitmap, image/jpeg, */* '
  48. if user_pass:
  49. env['HTTP_AUTHORIZATION']="Basic %s" % base64.encodestring(user_pass)
  50. if query_string:
  51. p_q = query_string.split('?')
  52. if len(p_q) == 1:
  53. env['PATH_INFO'] = p_q[0]
  54. elif len(p_q) == 2:
  55. (env['PATH_INFO'], env['QUERY_STRING'])=p_q
  56. else:
  57. raise TypeError, ''
  58. req = HTTPRequest(stdin, env, resp)
  59. req['URL1']=req['URL'] # fix for CMFQuickInstaller
  60. #
  61. # copy/hacked from Localizer __init__ patches
  62. # first put the needed values in the request
  63. req['HTTP_ACCEPT_CHARSET'] = 'latin-9'
  64. #req.other['AcceptCharset'] = AcceptCharset(req['HTTP_ACCEPT_CHARSET'])
  65. #
  66. req['HTTP_ACCEPT_LANGUAGE'] = 'fr'
  67. #accept_language = AcceptLanguage(req['HTTP_ACCEPT_LANGUAGE'])
  68. #req.other['AcceptLanguage'] = accept_language
  69. # XXX For backwards compatibility
  70. #req.other['USER_PREF_LANGUAGES'] = accept_language
  71. #req.other['AcceptLanguage'] = accept_language
  72. #
  73. # Plone stuff
  74. #req['plone_skin'] = 'Plone Default'
  75. #
  76. # then store the request in Publish._requests
  77. # with the thread id
  78. id = get_ident()
  79. if hasattr(Publish, '_requests'):
  80. # we do not have _requests inside ZopeTestCase
  81. Publish._requests[id] = req
  82. # add a brainless session container
  83. req['SESSION'] = {}
  84. #
  85. # ok, let's wrap
  86. return app.__of__(RequestContainer(REQUEST = req))
  87.  
  88.  
  89. def debug_init(app):
  90. loginAsUnrestrictedUser()
  91. app = makerequest(app)
  92. return app
  93.  
  94. >> from Products.wshelpers import wsdebug
  95. >> app = wsdebug.debug_init(app)
  96. >> # now you're logged in as admin
Add Comment
Please, Sign In to add comment