def is_subscribed(user, path): """ Checks to see if a user is subscribed to the given path. """ if isinstance(path, models.Model): path = model_sig(path) return Subscription.objects.filter(user=user, path=path, toggle=True).count() > 0
def send_event(actor, type, path, **context): """ Log an event performed by actor, of the type, on the path, and any other context arguments needed to render notification templates. """ # Todo, make sure notifications aren't sent too quickly one after another. if isinstance(path, models.Model): object = path path = model_sig(path) else: object = get_instance_from_sig(path) # Build a context to render templates context = context.copy() context['actor'] = actor context['path'] = path context['type'] = type context['object'] = object context['settings'] = settings context['DOMAIN'] = settings.DOMAIN # Find users to be notified subscriptions = Subscription.objects.filter(path=path, toggle=True) users = set([s.user for s in subscriptions]) users.discard( actor ) logger.info("Event: %s %s %s", actor, type, path) # Create the event, yo e = Event( actor = actor, type = type, path = path ) streams = set([actor]) # Trigger event signal # Receivers are expected to alter notify or context. for reciever, response in event.send(sender=e, notify=users, streams=streams, **context): if isinstance(response, dict): context.update(response) e.save() for stream in streams: e.add_to_stream(stream) # Notify all the ya'lls messages = [] for user in users: notice = Notice.objects.create(user=user) notice.events = [e] try: msg = render_mail(user.email, type, context) messages.append(msg) except Exception, x: print "Error sending email:", x
def subscribe(user, path): """ Subscribe a user to a path, thereon whenever an update occurs on the path, the user will be notified of the event. """ if isinstance(path, models.Model): path = model_sig(path) Subscription.objects.get_or_create(user=user, path=path)
def subscribe(user, path, force=False): """ Subscribe a user to a path, thereon whenever an update occurs on the path, the user will be notified of the event. """ if isinstance(path, models.Model): path = model_sig(path) sub, _ = Subscription.objects.get_or_create(user=user, path=path) if force: sub.toggle = True sub.save()
def unsubscribe(user, path): """ Unsubscribe a user from a path, blocking notices of updates on it. This is not as simple as deleting the subscription object, rather it marks them unsubscribed so that futher subscriptions are ignored. """ if isinstance(path, models.Model): path = model_sig(path) hits = Subscription.objects.filter(user=user, path=path).update(toggle=False) if hits == 0: Subscription.objects.create(user=user, path=path, toggle=False) return True
def get_subscriptions(path): if isinstance(path, models.Model): path = model_sig(path) return Subscription.objects.filter(path=path, toggle=True)
def get_subscribers(path): if isinstance(path, models.Model): path = model_sig(path) subscriptions = Subscription.objects.filter(path=path, toggle=True) return set([s.user for s in subscriptions])