-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
109 lines (101 loc) · 4.37 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import re
import json
import urllib
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.db.utils import IntegrityError
from django.utils.encoding import force_str, force_text, force_bytes
from hashtags.models import HashTag, HashTagged_Item
from picture_comments.models import PictureComment
#from larb.models import Post
from notifications import notify
from notifications.models import Notification
mention_pattern = re.compile(r'[@]+([-_a-zA-Z0-9]+)')
hashtag_pattern = re.compile(r'[#]+([-_a-zA-Z0-9]+)')
def notify_on_mention(text, object, user):
# parsing text looking for mentions (@) to be linked with the object
# user should always exist
# TODO : fail-safe perhaps in case a user deletes their profile
# at the exact moment that they are mentioned?
user = User.objects.get(id=user.id)
mention_list = []
for username in mention_pattern.findall(force_str(text)):
mention_list.append(username)
for mention in mention_list:
recipient = User.objects.get(username=mention)
# make sure the recipient exists first, else continue the loop
# TODO: create front-end search mechanism for users when they start a word with (@)
if recipient is not None :
verb = ''
if isinstance(object, PictureComment):
verb = u'mentioned you in a comment'
#content_object = Post.objects.get(id=object.object_pk)
else:
verb = u'mentioned you in a post'
#content_object = object
notify.send(user, recipient=recipient, verb=verb,
action_object=object, description=u'', target=recipient)
else:
continue
def notify_on_friend_request(friend_request):
# parsing text looking for mentions (@) to be linked with the object
# TODO : fail-safe perhaps in case a user deletes their profile
# at the exact moment that they are mentioned?
user = User.objects.get(id=friend_request.from_user.id)
verb = u'wants to be friends'
notify.send(friend_request.from_user, recipient=friend_request.to_user, verb=verb,
action_object=None, description=u'', target=friend_request.to_user)
def notify_on_friend_accepted(friend_request):
# parsing text looking for mentions (@) to be linked with the object
user = User.objects.get(id=friend_request.from_user.id)
verb = u'and you are now friends'
notify.send(friend_request.from_user, recipient=friend_request.to_user, verb=verb,
action_object=None, description=u'', target=friend_request.to_user)
notify.send(friend_request.to_user, recipient=friend_request.from_user, verb=verb,
action_object=None, description=u'', target=friend_request.from_user)
def urlize_mentions(text):
"""
Converts m in plain text into clickable links.
For example, if value of ``text`` is "This is a #test.", the output will be::
This is a
<a href="[reversed url for profile(request, mention='test')]">
#test</a>.
Note that if the value of ``text`` already contains HTML markup, things
won't work as expected. Prefer use this with plain text.
"""
def repl(m):
mention = m.group(1)
url = reverse('profile', kwargs={'username':mention})
return '<a class="lo-green" href="%s">@%s</a>' % (url, mention)
return mention_pattern.sub(repl, force_str(text))
def link_hashtags_to_model(text, object, user):
# parsing text looking for hashtags to be linked with the object
user = User.objects.get(id=user.id)
hashtag_list = []
for hname in hashtag_pattern.findall(force_str(text)):
hashtag, created = HashTag.objects.get_or_create(name=hname)
if created:
hashtag.save()
hashtag_list.append(hashtag)
# linking object to the new hashtags
#ct=ContentType.objects.get_for_model(object)
for hashtag in hashtag_list:
object_tag = object.tags.filter(id=hashtag__id)
if object_tag is None:
object.tags.add(hashtag)
def urlize_hashtags(text):
"""
Converts hashtags in plain text into clickable links.
For example, if value of ``text`` is "This is a #test.", the output will be::
This is a
<a href="[reversed url for hashtagged_item_list(request, hashtag='test')]">
#test</a>.
Note that if the value of ``text`` already contains HTML markup, things
won't work as expected. Prefer use this with plain text.
"""
def repl(m):
hashtag = m.group(1)
url = reverse('hashtagged_item_list', kwargs={'hashtag': hashtag})
return '<a class="lo-yellow" href="%s">#%s</a>' % (url, hashtag)
return hashtag_pattern.sub(repl, force_str(text))