def follow(user, actor, send_action=True): """ Creates a ``User`` -> ``Actor`` follow relationship such that the actor's activities appear in the user's stream. Also sends the ``<user> started following <actor>`` action signal. Returns the created ``Follow`` instance. If ``send_action`` is false, no "started following" signal will be created Syntax:: follow(<user>, <actor>) Example:: follow(request.user, group) """ from actstream.models import Follow, action check_actionable_model(actor) follow, created = Follow.objects.get_or_create(user=user, object_id=actor.pk, content_type=ContentType.objects.get_for_model(actor)) if send_action and created: action.send(user, verb=_('started following'), target=actor) return follow
def action_handler(verb, **kwargs): """ Handler function to create Action instance upon action signal call. """ from actstream.models import Action kwargs.pop('signal', None) actor = kwargs.pop('sender') check_actionable_model(actor) # We must store the unstranslated string # If verb is an ugettext_lazyed string, fetch the original string if hasattr(verb, '_proxy____args'): verb = verb._proxy____args[0] newaction = Action( actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), public=bool(kwargs.pop('public', True)), description=kwargs.pop('description', None), timestamp=kwargs.pop('timestamp', now()) ) for opt in ('target', 'action_object'): obj = kwargs.pop(opt, None) if not obj is None: check_actionable_model(obj) setattr(newaction, '%s_object_id' % opt, obj.pk) setattr(newaction, '%s_content_type' % opt, ContentType.objects.get_for_model(obj)) if settings.USE_JSONFIELD and len(kwargs): newaction.data = kwargs newaction.save() return newaction
def unfollow(user, obj, send_action=False, request=None): """ Removes a "follow" relationship. Set ``send_action`` to ``True`` (``False is default) to also send a ``<user> stopped following <object>`` action signal. Example:: unfollow(request.user, other_user) """ from actstream.models import Follow, action check_actionable_model(obj) Follow.objects.filter( user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj)).delete() if send_action: if request: from django.utils import simplejson as json from agora_site.misc.utils import geolocate_ip action.send(user, verb=_('stopped following'), target=obj, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps( geolocate_ip(request.META.get('REMOTE_ADDR')))) else: action.send(user, verb=_('stopped following'), target=obj)
def action_handler(verb, **kwargs): """ Handler function to create Action instance upon action signal call. """ from actstream.models import Action kwargs.pop('signal', None) actor = kwargs.pop('sender') check_actionable_model(actor) newaction = Action( actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), public=bool(kwargs.pop('public', True)), description=kwargs.pop('description', None), timestamp=kwargs.pop('timestamp', now())) for opt in ('target', 'action_object'): obj = kwargs.pop(opt, None) if not obj is None: check_actionable_model(obj) setattr(newaction, '%s_object_id' % opt, obj.pk) setattr(newaction, '%s_content_type' % opt, ContentType.objects.get_for_model(obj)) if len(kwargs): newaction.data = kwargs newaction.save()
def follow(user, actor, send_action=True): """ Creates a ``User`` -> ``Actor`` follow relationship such that the actor's activities appear in the user's stream. Also sends the ``<user> started following <actor>`` action signal. Returns the created ``Follow`` instance. If ``send_action`` is false, no "started following" signal will be created Syntax:: follow(<user>, <actor>) Example:: follow(request.user, group) """ from actstream.models import Follow check_actionable_model(actor) follow, created = Follow.objects.get_or_create( user=user, object_id=actor.pk, content_type=ContentType.objects.get_for_model(actor)) if send_action and created: action.send(user, verb=_('started following'), target=actor) return follow
def follow(user, obj, send_action=True, actor_only=True): """ Creates a relationship allowing the object's activities to appear in the user's stream. Returns the created ``Follow`` instance. If ``send_action`` is ``True`` (the default) then a ``<user> started following <object>`` action signal is sent. If ``actor_only`` is ``True`` (the default) then only actions where the object is the actor will appear in the user's activity stream. Set to ``False`` to also include actions where this object is the action_object or the target. Example:: follow(request.user, group, actor_only=False) """ from actstream.models import Follow, action check_actionable_model(obj) follow, created = Follow.objects.get_or_create(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj), actor_only=actor_only) if send_action and created: action.send(user, verb=_('started following'), target=obj) return follow
def action_handler(verb, **kwargs): """ Handler function to create Action instance upon action signal call. """ from actstream.models import Action kwargs.pop('signal', None) actor = kwargs.pop('sender') check_actionable_model(actor) # We must store the unstranslated string # If verb is an ugettext_lazyed string, fetch the original string if hasattr(verb, '_proxy____args'): verb = verb._proxy____args[0] newaction = Action( actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), public=bool(kwargs.pop('public', True)), description=kwargs.pop('description', None), timestamp=kwargs.pop('timestamp', now()) ) for opt in ('target', 'action_object'): obj = kwargs.pop(opt, None) if not obj is None: check_actionable_model(obj) setattr(newaction, '%s_object_id' % opt, obj.pk) setattr(newaction, '%s_content_type' % opt, ContentType.objects.get_for_model(obj)) if settings.USE_JSONFIELD and len(kwargs): newaction.data = kwargs newaction.save()
def action_handler(verb, **kwargs): """ Handler function to create Action instance upon action signal call. """ from actstream.models import Action kwargs.pop('signal', None) actor = kwargs.pop('sender') check_actionable_model(actor) newaction = Action( actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), public=bool(kwargs.pop('public', True)), description=kwargs.pop('description', None), timestamp=kwargs.pop('timestamp', now()), data=kwargs.pop('data', None), ) for opt in ('target', 'action_object'): obj = kwargs.pop(opt, None) if not obj is None: check_actionable_model(obj) setattr(newaction, '%s_object_id' % opt, obj.pk) setattr(newaction, '%s_content_type' % opt, ContentType.objects.get_for_model(obj)) newaction.save()
def place_action_handler(verb, **kwargs): """ Handler function to create Action instance upon action signal call. """ kwargs.pop("signal", None) actor = kwargs.pop("sender", None) if not actor: actor = User.objects.get(pk=settings.ACTIVITY_STREAM_DEFAULT_ACTOR_PK) check_actionable_model(actor) newaction = Action( actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), place=kwargs.pop("place", None), # TODO get automatically from target obj? public=bool(kwargs.pop("public", True)), description=kwargs.pop("description", None), timestamp=kwargs.pop("timestamp", now()), ) for opt in ("target", "action_object"): obj = kwargs.pop(opt, None) if not obj is None: check_actionable_model(obj) setattr(newaction, "%s_object_id" % opt, obj.pk) setattr(newaction, "%s_content_type" % opt, ContentType.objects.get_for_model(obj)) if actstream_settings.USE_JSONFIELD and len(kwargs): newaction.data = kwargs newaction.save()
def action_handler(verb, **kwargs): """ Handler function to create Action instance upon action signal call. """ from actstream.models import Action kwargs.pop("signal", None) actor = kwargs.pop("sender") if actor is None: ctype, oid = None, None else: check_actionable_model(actor) ctype = ContentType.objects.get_for_model(actor) oid = actor.pk newaction = Action( actor_content_type=ctype, actor_object_id=oid, verb=unicode(verb), public=bool(kwargs.pop("public", True)), description=kwargs.pop("description", None), timestamp=kwargs.pop("timestamp", datetime.now()), ) for opt in ("target", "action_object"): obj = kwargs.pop(opt, None) if not obj is None: check_actionable_model(obj) setattr(newaction, "%s_object_id" % opt, obj.pk) setattr(newaction, "%s_content_type" % opt, ContentType.objects.get_for_model(obj)) newaction.save()
def place_action_handler(verb, **kwargs): """ Handler function to create Action instance upon action signal call. """ kwargs.pop('signal', None) actor = kwargs.pop('sender', None) if not actor: actor = User.objects.get(pk=settings.ACTIVITY_STREAM_DEFAULT_ACTOR_PK) check_actionable_model(actor) newaction = Action( actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), place=kwargs.pop('place', None), # TODO get automatically from target obj? public=bool(kwargs.pop('public', True)), description=kwargs.pop('description', None), timestamp=kwargs.pop('timestamp', now())) for opt in ('target', 'action_object'): obj = kwargs.pop(opt, None) if not obj is None: check_actionable_model(obj) setattr(newaction, '%s_object_id' % opt, obj.pk) setattr(newaction, '%s_content_type' % opt, ContentType.objects.get_for_model(obj)) if actstream_settings.USE_JSONFIELD and len(kwargs): newaction.data = kwargs newaction.save()
def follow(user, obj, send_action=True, actor_only=True): """ Creates a relationship allowing the object's activities to appear in the user's stream. Returns the created ``Follow`` instance. If ``send_action`` is ``True`` (the default) then a ``<user> started following <object>`` action signal is sent. If ``actor_only`` is ``True`` (the default) then only actions where the object is the actor will appear in the user's activity stream. Set to ``False`` to also include actions where this object is the action_object or the target. Example:: follow(request.user, group, actor_only=False) """ from actstream.models import Follow, action check_actionable_model(obj) follow, created = Follow.objects.get_or_create(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj), actor_only=actor_only) if send_action and created: action.send(user, verb=_('started following'), target=obj) return follow
def unfollow(user, obj, send_action=False, request=None): """ Removes a "follow" relationship. Set ``send_action`` to ``True`` (``False is default) to also send a ``<user> stopped following <object>`` action signal. Example:: unfollow(request.user, other_user) """ from actstream.models import Follow, action check_actionable_model(obj) Follow.objects.filter(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj)).delete() if send_action: if request: from django.utils import simplejson as json from agora_site.misc.utils import geolocate_ip action.send(user, verb=_('stopped following'), target=obj, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) else: action.send(user, verb=_('stopped following'), target=obj)
def is_following(user, obj): """ Checks if a "follow" relationship exists. Returns True if exists, False otherwise. Example:: is_following(request.user, group) """ from actstream.models import Follow check_actionable_model(obj) return bool(Follow.objects.filter(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj)).count())
def is_following(user, obj): """ Checks if a "follow" relationship exists. Returns True if exists, False otherwise. Example:: is_following(request.user, group) """ from actstream.models import Follow check_actionable_model(obj) return bool(Follow.objects.filter(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj)).count())
def unfollow(user, obj, send_action=False): """ Removes a "follow" relationship. Set ``send_action`` to ``True`` (``False is default) to also send a ``<user> stopped following <object>`` action signal. Example:: unfollow(request.user, other_user) """ from actstream.models import Follow, action check_actionable_model(obj) Follow.objects.filter(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj)).delete() if send_action: action.send(user, verb=_("stopped following"), target=obj)
def unfollow(user, obj, send_action=False): """ Removes a "follow" relationship. Set ``send_action`` to ``True`` (``False is default) to also send a ``<user> stopped following <object>`` action signal. Example:: unfollow(request.user, other_user) """ from actstream.models import Follow, action check_actionable_model(obj) Follow.objects.filter(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj)).delete() if send_action: action.send(user, verb=_('stopped following'), target=obj)
def is_following(user, actor): """ Checks if a ``User`` -> ``Actor`` relationship exists. Returns True if exists, False otherwise. Syntax:: is_following(<user>, <actor>) Example:: is_following(request.user, group) """ from actstream.models import Follow check_actionable_model(actor) return bool(Follow.objects.filter(user=user, object_id=actor.pk, content_type=ContentType.objects.get_for_model(actor)).count())
def follow(user, obj, send_action=True, actor_only=False, request=None): """ Creates a relationship allowing the object's activities to appear in the user's stream. Returns the created ``Follow`` instance. If ``send_action`` is ``True`` (the default) then a ``<user> started following <object>`` action signal is sent. If ``actor_only`` is ``True`` (the default) then only actions where the object is the actor will appear in the user's activity stream. Set to ``False`` to also include actions where this object is the action_object or the target. Example:: follow(request.user, group, actor_only=False) """ from actstream.models import Follow, action check_actionable_model(obj) follow, created = Follow.objects.get_or_create( user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj), actor_only=actor_only) if send_action and created: if request: from django.utils import simplejson as json from agora_site.misc.utils import geolocate_ip action.send(user, verb=_('started following'), target=obj, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps( geolocate_ip(request.META.get('REMOTE_ADDR')))) else: action.send(user, verb=_('started following'), target=obj) return follow
def unfollow(user, actor, send_action=False): """ Removes ``User`` -> ``Actor`` follow relationship. Optionally sends the ``<user> stopped following <actor>`` action signal. Syntax:: unfollow(<user>, <actor>) Example:: unfollow(request.user, other_user) """ from actstream.models import Follow check_actionable_model(actor) Follow.objects.filter(user = user, object_id = actor.pk, content_type = ContentType.objects.get_for_model(actor)).delete() if send_action: action.send(user, verb=_('stopped following'), target=actor)
def action_create(actor, verb, action_object, target, description=None, public=True): """ Handler function to create Action instance upon action signal call. """ # Prevent AnonymousUser from not passing the checks if actor.is_anonymous(): actor = User.objects.get(id=settings.ANONYMOUS_USER_ID) check_actionable_model(actor) check_actionable_model(action_object) check_actionable_model(target) newaction = Action( actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), target_object_id=target.id, target_content_type=ContentType.objects.get_for_model(target), action_object_object_id=action_object.id, action_object_content_type=ContentType.objects.get_for_model(action_object), public=bool(public), description=description, timestamp=datetime.datetime.now(), ) newaction.save() return newaction
def action_create(actor, verb, action_object, target, description=None, public=True): """ Handler function to create Action instance upon action signal call. """ # Prevent AnonymousUser from not passing the checks if actor.is_anonymous(): actor = User.objects.get(id=settings.ANONYMOUS_USER_ID) check_actionable_model(actor) check_actionable_model(action_object) check_actionable_model(target) newaction = Action( actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), target_object_id=target.id, target_content_type=ContentType.objects.get_for_model(target), action_object_object_id=action_object.id, action_object_content_type=ContentType.objects.get_for_model( action_object), public=bool(public), description=description, timestamp=datetime.datetime.now(), ) newaction.save() return newaction
def is_following(user, actor): """ Checks if a ``User`` -> ``Actor`` relationship exists. Returns True if exists, False otherwise. Syntax:: is_following(<user>, <actor>) Example:: is_following(request.user, group) """ from actstream.models import Follow check_actionable_model(actor) return bool( Follow.objects.filter( user=user, object_id=actor.pk, content_type=ContentType.objects.get_for_model(actor)).count())
def unfollow(user, actor, send_action=False): """ Removes ``User`` -> ``Actor`` follow relationship. Optionally sends the ``<user> stopped following <actor>`` action signal. Syntax:: unfollow(<user>, <actor>) Example:: unfollow(request.user, other_user) """ from actstream.models import Follow check_actionable_model(actor) Follow.objects.filter( user=user, object_id=actor.pk, content_type=ContentType.objects.get_for_model(actor)).delete() if send_action: action.send(user, verb=_('stopped following'), target=actor)
def follow(user, obj, send_action=True, actor_only=False, request=None): """ Creates a relationship allowing the object's activities to appear in the user's stream. Returns the created ``Follow`` instance. If ``send_action`` is ``True`` (the default) then a ``<user> started following <object>`` action signal is sent. If ``actor_only`` is ``True`` (the default) then only actions where the object is the actor will appear in the user's activity stream. Set to ``False`` to also include actions where this object is the action_object or the target. Example:: follow(request.user, group, actor_only=False) """ from actstream.models import Follow, action check_actionable_model(obj) follow, created = Follow.objects.get_or_create(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj), actor_only=actor_only) if send_action and created: if request: from django.utils import simplejson as json from agora_site.misc.utils import geolocate_ip action.send(user, verb=_('started following'), target=obj, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) else: action.send(user, verb=_('started following'), target=obj) return follow