Skip to content

Instantly share code, notes, and snippets.

from django.dispatch import Signal
request_started = Signal()
request_finished = Signal()
got_request_exception = Signal(providing_args=["request"])
class Signal(object):
"""
Base class for all signals
Internal attributes:
receivers
{ receriverkey (id) : weakref(receiver) }
"""
def send(self, sender, **named):
"""
Send signal from sender to all connected receivers.
If any receiver raises an error, the error propagates back through send,
terminating the dispatch loop, so it is quite possible to not have all
receivers called if a raises an error.
Arguments:
def send(self, sender, **named):
"""
Send signal from sender to all connected receivers.
If any receiver raises an error, the error propagates back through send,
terminating the dispatch loop, so it is quite possible to not have all
receivers called if a raises an error.
Arguments:
def logArticle(sender, **kwargs):
f = open('log.txt','w')
f.write('%s -- Signal received. An article was created and saved.\n' % datetime.datetime.now())
f.close()
return
@qoelet
qoelet / gist:410802
Created May 23, 2010 09:50
django.views.generic.simple
def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
"""
Render a given template with any extra URL parameters in the context as
``{{ params }}``.
"""
if extra_context is None: extra_context = {}
dictionary = {'params': kwargs}
for key, value in extra_context.items():
if callable(value):
dictionary[key] = value()
@qoelet
qoelet / gist:410817
Created May 23, 2010 10:09
django.views.generic.simple
def redirect_to(request, url, permanent=True, **kwargs):
"""
Redirect to a given URL.
The given url may contain dict-style string formatting, which will be
interpolated against the params in the URL. For example, to redirect from
``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf::
urlpatterns = patterns('',
('^foo/(?P<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}),
class HttpResponseGone(HttpResponse):
status_code = 410
def __init__(self, *args, **kwargs):
HttpResponse.__init__(self, *args, **kwargs)
def apply_extra_context(extra_context, context):
"""
Adds items from extra_context dict to context. If a value in extra_context
is callable, then it is called and the result is added to context.
"""
for key, value in extra_context.iteritems():
if callable(value):
context[key] = value()
else:
context[key] = value
>>> mydict = {'a':1, 'b':2}
>>> for something in mydict.iteritems():
... print something
...
('a', 1)
('b', 2)
>>> for key, value in mydict.iteritems():
... print key, value
...
a 1