]> git.openstreetmap.org Git - osqa.git/blob - forum/utils/userlinking.py
fd7a66dff0b16bd0d894b22aa375f2c30b7a44be
[osqa.git] / forum / utils / userlinking.py
1 import re
2
3 from forum.models.user import User
4
5 def auto_user_link(node, content):
6     patern = r'@\w+'
7     appeals = re.findall(patern, content)
8
9     for appeal in appeals:
10         # Try to find the profile URL
11         username = appeal[1:]
12         profile_url = None
13
14         try:
15             user = User.objects.get(username__iexact=username)
16             profile_url = user.get_absolute_url()
17         except User.DoesNotExist:
18             """If we don't find the user from the first time, the interesting part
19                begins. We look through all the authors (looking through question,
20                comments, answers, and if it matches some of the -- we link him."""
21             
22             # We should find the root of the node tree (question) the current node belongs to.
23             if node.node_type == "question":
24                 question = node
25             elif node.node_type == "answer":
26                 question = node.question
27             elif node.node_type == "comment":
28                 if not node.question:
29                     question = node
30                 else:
31                     question = node.question
32             
33             # Now we've got the root question. Let's get the list of active users.
34             active_users = question.get_active_users()
35             
36             for active_user in active_users:
37                 if active_user.username.lower().startswith(username.lower()):
38                     profile_url = active_user.get_absolute_url()
39         
40         if (profile_url is not None) and (appeal is not None):
41             auto_link = '<a href="%s">%s</a>' % (profile_url, appeal)
42             content = content.replace(appeal, auto_link)
43     
44     return content