Created
November 29, 2011 20:47
-
-
Save gerardo/1406434 to your computer and use it in GitHub Desktop.
post_save hook and models
This file contains 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
import sys, traceback | |
from django.db import models | |
import django_monitor | |
import xmlrpclib | |
class TVShow(models.Model): | |
active = models.BooleanField(default=False) | |
name = models.CharField(blank=False, max_length=20) | |
description = models.TextField(blank=False) | |
terms = models.CharField(blank=False, max_length=50) | |
def __unicode__(self): | |
return "%s%s" % (self.name, '(Activo)' if self.active else '') | |
class Update(models.Model): | |
username = models.CharField(blank=False, max_length=20) | |
message = models.TextField(blank=False) | |
show = models.ForeignKey('TVShow', related_name='updates') | |
created_at = models.DateTimeField(auto_now_add=True) | |
def __unicode__(self): | |
return "From @%s: %s" % (self.username, self.message) | |
class Meta: | |
pass | |
django_monitor.nq(Update) | |
django_monitor.nq(TVShow) | |
def show_post_save(sender, instance, **kwargs): | |
stopped = '' | |
started = '' | |
print "Post-save hook" | |
try: | |
s = xmlrpclib.ServerProxy('http://django:djangosupervctl@localhost:9001') | |
stopped = s.supervisor.stopProcessGroup('twitterstream', True) | |
print "Stopped: " , stopped | |
actives = TVShow.objects.filter(active=True) | |
if len(actives) > 0 or kwargs.get('instance').active == True: | |
started = s.supervisor.startProcess('twitterstream', True) | |
print "Started: " , started | |
except: | |
print "Exception" | |
print "Started: " , started | |
print "Stopped: " , stopped | |
traceback.print_exc(file=sys.stdout) | |
models.signals.post_save.connect(show_post_save, sender=TVShow) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment