djangosnippets.org: Latest snippets tagged with 'signals'https://djangosnippets.org/tags/signals/2012-09-19T23:27:40-05:00Pre-delete signal function for deleting files a model2012-09-19T23:27:40-05:00mindcruzerhttps://djangosnippets.org/snippets/2820/<p>This snippit is meant to be used with the pre_delete signal to delete any files associated with a model instance before the instance is deleted. It will search the model instance for fields that are subclasses of FieldFile, and then delete the corresponding files. As such, it will work with ...</p>
Freely redistributableShow logged users - keeping track of users login and logout2012-09-05T15:02:18-05:00albertorcfhttps://djangosnippets.org/snippets/2808/<p>Showing a list of logged users using the <em>user_logged_in</em> and <em>user_logged_out</em> signals.</p>
<p>See <a href="https://docs.djangoproject.com/en/1.4/topics/auth/#login-and-logout-signals">login and logout signals</a> in Django docs.</p>
Freely redistributableBinding signals to abstract models2012-05-15T11:25:45-05:00andreterrahttps://djangosnippets.org/snippets/2749/<h2>Intro</h2>
<p>I found a question on SO for which Justin Lilly's answer was correct but not as thorough as I'd like, so I ended up working on a simple snippet that shows how to bind signals at runtime, which is nifty when you want to bind signals to ...</p>
Freely redistributableUser Profile minimal code2011-08-02T04:02:05-05:00Natimhttps://djangosnippets.org/snippets/2501/<p>Usually I start an authentication app with this model.</p>
<p>Don't forget to set it up in the settings file</p>
<pre><code>AUTH_PROFILE_MODULE = 'authentication.UserProfile'
</code></pre>
Freely redistributableRun model validation before saving a model instance2011-01-12T01:11:48-06:00buriyhttps://djangosnippets.org/snippets/2319/<p>How to validate your model at save using the pre_save signal.
</p>
<p>from http://groups.google.com/group/django-developers/browse_thread/thread/eb2f760e4c8d7911/482d8fd36fba4596?hl=en&amp;lnk=gst&amp;q=problem+with+Model.objects.create#482d8fd36fba4596
</p>
Freely redistributableimmitating &#39;real&#39; post_syncdb signal2010-04-16T15:50:03-05:00jangohttps://djangosnippets.org/snippets/1988/<p>I did not like the idea of having to load fixtures by creating a huge initial_data.json file. I also did not want to store my initial data in a bunch of &lt;modelname&gt;.sql files.</p>
<p>Django has post_syncdb signal which fires when model(s) for an application are installed, but ...</p>
Freely redistributablemanually models unique_together check via signals2009-07-13T07:27:11-05:00jediehttps://djangosnippets.org/snippets/1628/<p>signal handler for checking unique_together manually.
</p>
<p>Also available via django-tools: http://code.google.com/p/django-tools/
</p>
Freely redistributableSignal to notify new saved comments2009-05-31T17:29:55-05:00arthurfurlanhttps://djangosnippets.org/snippets/1539/<p>Signal to notify new saved comments.</p>
<p><strong>Example:</strong></p>
<pre><code>from django.contrib.comment import models, signals
signals.comment_was_posted.connect(new_comment_notifier,
sender=models.Comment)
</code></pre>
Freely redistributableExecute a signal once 2009-05-05T15:58:02-05:00johnnoonehttps://djangosnippets.org/snippets/1490/<p>Decorates signals for executing only one time</p>
<p>Exemple usage :</p>
<pre><code>from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.contrib.auth.models import User
@one
def user_welcome(sender, instance, created, **kwargs):
# Send a welcome email
if created == True and isinstance(instance, User):
instance.message_set.create(message ...</code></pre>
Freely redistributableCustomImageField for Django 1.0 alpha2008-08-07T12:38:30-05:00hanksimshttps://djangosnippets.org/snippets/949/<p>The venerable CustomImageField, invented by <a href="http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/">Scott Barnham</a> and rejiggered for newforms-admin by <a href="http://pandemoniumillusion.wordpress.com/2008/08/06/django-imagefield-and-filefield-dynamic-upload-path-in-newforms-admin/#comments">jamstooks</a>.</p>
<p>This here is a stab at a <a href="http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Signalrefactoring">post-Signals-refactor</a> version. Seems to do 'er.</p>
<p>Note: This should be pointless once <a href="http://code.djangoproject.com/ticket/5361">fs-refactor</a> lands.</p>
Freely redistributableGeneric Recievers For Signals2008-06-04T16:20:00-05:00ericmoritzhttps://djangosnippets.org/snippets/784/<p>Allows you to add args and kwargs to the signal reciever</p>
Freely redistributableAlter Column Lengths of Contrib Apps2008-04-23T07:39:35-05:00guettlihttps://djangosnippets.org/snippets/718/<p>If you want to modify the length of a column of a contrib application, you can either
modify django (cumbersome) or you can run a post_syncdb signal hook.</p>
<p>Related: <a href="http://code.djangoproject.com/ticket/4748">Ticket 4748</a></p>
Freely redistributablePreSaveMiddleware2007-11-20T12:47:13-06:00pterkhttps://djangosnippets.org/snippets/476/<p>With this middleware in place (add it to the MIDDLEWARE_CLASSES in your settings) you can pass a request to the model via a pre_save method on the model.</p>
<p>I'm not sure if it is an improvement over the [threadlocals method] (http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser) but it may ...</p>
Freely redistributable