1
|
|
|
from django.apps import apps |
2
|
|
|
from django.utils.translation import ugettext_lazy as _ |
3
|
|
|
from django.utils.six import text_type |
4
|
|
|
from django.contrib.contenttypes.models import ContentType |
5
|
|
|
|
6
|
|
|
from actstream import settings |
7
|
|
|
from actstream.signals import action |
8
|
|
|
from actstream.registry import check |
9
|
|
|
|
10
|
|
|
try: |
11
|
|
|
from django.utils import timezone |
12
|
|
|
|
13
|
|
|
now = timezone.now |
14
|
|
|
except ImportError: |
15
|
|
|
import datetime |
16
|
|
|
now = datetime.datetime.now |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def follow(user, obj, send_action=True, actor_only=True, flag='', **kwargs): |
20
|
|
|
""" |
21
|
|
|
Creates a relationship allowing the object's activities to appear in the |
22
|
|
|
user's stream. |
23
|
|
|
|
24
|
|
|
Returns the created ``Follow`` instance. |
25
|
|
|
|
26
|
|
|
If ``send_action`` is ``True`` (the default) then a |
27
|
|
|
``<user> started following <object>`` action signal is sent. |
28
|
|
|
Extra keyword arguments are passed to the action.send call. |
29
|
|
|
|
30
|
|
|
If ``actor_only`` is ``True`` (the default) then only actions where the |
31
|
|
|
object is the actor will appear in the user's activity stream. Set to |
32
|
|
|
``False`` to also include actions where this object is the action_object or |
33
|
|
|
the target. |
34
|
|
|
|
35
|
|
|
If ``flag`` not an empty string then the relationship would marked by this flag. |
36
|
|
|
|
37
|
|
|
Example:: |
38
|
|
|
|
39
|
|
|
follow(request.user, group, actor_only=False) |
40
|
|
|
follow(request.user, group, actor_only=False, flag='liking') |
41
|
|
|
""" |
42
|
|
|
check(obj) |
43
|
|
|
instance, created = apps.get_model('actstream', 'follow').objects.get_or_create( |
44
|
|
|
user=user, object_id=obj.pk, flag=flag, |
45
|
|
|
content_type=ContentType.objects.get_for_model(obj), |
46
|
|
|
actor_only=actor_only |
47
|
|
|
) |
48
|
|
|
if send_action and created: |
49
|
|
|
if not flag: |
50
|
|
|
action.send(user, verb=_('started following'), target=obj, **kwargs) |
51
|
|
|
else: |
52
|
|
|
action.send(user, verb=_('started %s' % flag), target=obj, **kwargs) |
53
|
|
|
return instance |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def unfollow(user, obj, send_action=False, flag=''): |
57
|
|
|
""" |
58
|
|
|
Removes a "follow" relationship. |
59
|
|
|
|
60
|
|
|
Set ``send_action`` to ``True`` (``False is default) to also send a |
61
|
|
|
``<user> stopped following <object>`` action signal. |
62
|
|
|
|
63
|
|
|
Pass a string value to ``flag`` to determine which type of "follow" relationship you want to remove. |
64
|
|
|
|
65
|
|
|
Example:: |
66
|
|
|
|
67
|
|
|
unfollow(request.user, other_user) |
68
|
|
|
unfollow(request.user, other_user, flag='watching') |
69
|
|
|
""" |
70
|
|
|
check(obj) |
71
|
|
|
qs = apps.get_model('actstream', 'follow').objects.filter( |
72
|
|
|
user=user, object_id=obj.pk, |
73
|
|
|
content_type=ContentType.objects.get_for_model(obj) |
74
|
|
|
) |
75
|
|
|
|
76
|
|
|
if flag: |
77
|
|
|
qs = qs.filter(flag=flag) |
78
|
|
|
qs.delete() |
79
|
|
|
|
80
|
|
|
if send_action: |
81
|
|
|
if not flag: |
82
|
|
|
action.send(user, verb=_('stopped following'), target=obj) |
83
|
|
|
else: |
84
|
|
|
action.send(user, verb=_('stopped %s' % flag), target=obj) |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
def is_following(user, obj, flag=''): |
88
|
|
|
""" |
89
|
|
|
Checks if a "follow" relationship exists. |
90
|
|
|
|
91
|
|
|
Returns True if exists, False otherwise. |
92
|
|
|
|
93
|
|
|
Pass a string value to ``flag`` to determine which type of "follow" relationship you want to check. |
94
|
|
|
|
95
|
|
|
Example:: |
96
|
|
|
|
97
|
|
|
is_following(request.user, group) |
98
|
|
|
is_following(request.user, group, flag='liking') |
99
|
|
|
""" |
100
|
|
|
check(obj) |
101
|
|
|
|
102
|
|
|
qs = apps.get_model('actstream', 'follow').objects.filter( |
103
|
|
|
user=user, object_id=obj.pk, |
104
|
|
|
content_type=ContentType.objects.get_for_model(obj) |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
if flag: |
108
|
|
|
qs = qs.filter(flag=flag) |
109
|
|
|
|
110
|
|
|
return qs.exists() |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def action_handler(verb, **kwargs): |
114
|
|
|
""" |
115
|
|
|
Handler function to create Action instance upon action signal call. |
116
|
|
|
""" |
117
|
|
|
kwargs.pop('signal', None) |
118
|
|
|
actor = kwargs.pop('sender') |
119
|
|
|
|
120
|
|
|
# We must store the unstranslated string |
121
|
|
|
# If verb is an ugettext_lazyed string, fetch the original string |
122
|
|
|
if hasattr(verb, '_proxy____args'): |
123
|
|
|
verb = verb._proxy____args[0] |
124
|
|
|
|
125
|
|
|
newaction = apps.get_model('actstream', 'action')( |
126
|
|
|
actor_content_type=ContentType.objects.get_for_model(actor), |
127
|
|
|
actor_object_id=actor.pk, |
128
|
|
|
verb=text_type(verb), |
129
|
|
|
public=bool(kwargs.pop('public', True)), |
130
|
|
|
description=kwargs.pop('description', None), |
131
|
|
|
timestamp=kwargs.pop('timestamp', now()) |
132
|
|
|
) |
133
|
|
|
|
134
|
|
|
for opt in ('target', 'action_object'): |
135
|
|
|
obj = kwargs.pop(opt, None) |
136
|
|
|
if obj is not None: |
137
|
|
|
check(obj) |
138
|
|
|
setattr(newaction, '%s_object_id' % opt, obj.pk) |
139
|
|
|
setattr(newaction, '%s_content_type' % opt, |
140
|
|
|
ContentType.objects.get_for_model(obj)) |
141
|
|
|
if settings.USE_JSONFIELD and len(kwargs): |
142
|
|
|
newaction.data = kwargs |
143
|
|
|
newaction.save(force_insert=True) |
144
|
|
|
return newaction |
145
|
|
|
|