This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.dispatch import Signal | |
request_started = Signal() | |
request_finished = Signal() | |
got_request_exception = Signal(providing_args=["request"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Signal(object): | |
""" | |
Base class for all signals | |
Internal attributes: | |
receivers | |
{ receriverkey (id) : weakref(receiver) } | |
""" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'}), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HttpResponseGone(HttpResponse): | |
status_code = 410 | |
def __init__(self, *args, **kwargs): | |
HttpResponse.__init__(self, *args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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 |
OlderNewer