Skip to content

Instantly share code, notes, and snippets.

View mitchellrj's full-sized avatar

Richard Mitchell mitchellrj

View GitHub Profile
@mitchellrj
mitchellrj / link-remap.sh
Created December 19, 2011 14:09
Remap links
@mitchellrj
mitchellrj / ie.py
Created January 24, 2012 14:58
Internet Explorer detection
def is_ie(request, min_version=None, max_version=None):
user_agent = request.environ.get('HTTP_USER_AGENT', '')
ie = user_agent.find('MSIE')
if ie==-1:
return False
if not min_version and not max_version:
return True
version_match = re.match('[\d\.]*', user_agent[ie+5:])
@mitchellrj
mitchellrj / buildout.cfg
Created January 31, 2012 10:39
Patch PasteScript to support safe termination of daemon threads
[buildout]
eggs = ...
parts = ...
pastescript
[pastescript]
recipe = zc.recipe.egg
eggs = PasteScript # required to generate the specific paster script if not already in eggs list
${buildout:eggs}
extra-paths = path/to/pastescriptpatches.py
@mitchellrj
mitchellrj / get_teaser.py
Created February 20, 2012 15:42 — forked from enko/teaser.py
teaser behavior
## Script (Python) "get_teaser"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##
# Put this file in a skins directory, call it by doing context/get_teaser or context.get_teaser()
@mitchellrj
mitchellrj / unicode_tools.py
Created February 21, 2012 15:58
How to beat unicode
# http://pypi.python.org/pypi/chardet
import chardet
# List of your preferred charsets, or detect those available automatically (see below)
CHARSETS = ['ascii', 'latin_1', 'utf_8']
def get_charset(string):
"""Given a string or unicode object, returns the simplest available
@mitchellrj
mitchellrj / strftime_1900.py
Created March 8, 2012 12:42
Python datetime.strftime < 1900 workaround
import datetime
import re
import warnings
def strftime(dt, fmt):
if dt.year < 1900:
# create a copy of this datetime, just in case, then set the year to
# something acceptable, then replace that year in the resulting string
tmp_dt = datetime.datetime(datetime.MAXYEAR, dt.month, dt.day,
dt.hour, dt.minute,
@mitchellrj
mitchellrj / plone-2.1.1.cfg
Created March 9, 2012 15:10
Example buildout for Plone 2.1.1
[buildout]
extends = versions.cfg
parts =
plone
zope2
productdistros
instance
live-client
omelette
@mitchellrj
mitchellrj / clear_comments.py
Created March 16, 2012 18:25
Clear all plone.app.discussion comments
catalog_interfaces = ('plone.app.discussion.interfaces.IComment',)
pc = context.portal_catalog
comment_brains = pc(object_provides=tuple(catalog_interfaces))
for b in comment_brains:
comment = b.getObject()
comment_id = comment.comment_id
conversation = comment.aq_parent
@mitchellrj
mitchellrj / clear_davlocks.py
Created April 4, 2012 14:22
Clear WebDAV locks
def unlockAllObjects(app, path):
lockedobjs = findLockedObjects(app, path)
for (o, p, i) in lockedobjs:
errs = False
for l in [li['token'] for li in i]:
try:
o.wl_delLock(l)
except:
errs = True
if errs:
@mitchellrj
mitchellrj / configure.zcml
Created April 10, 2012 10:27
Make Plone object undeletable but still work with iterate / stagingbehavior
<configure xmlns="http://namespaces.zope.org/zope">
<subscriber
for=".interfaces.IMyType
OFS.interfaces.IObjectWillBeRemovedEvent"
handler=".events.event_handler"
/>
</configure>