3 from forum.models.user import User
5 def auto_user_link(node, content):
7 appeals = re.findall(patern, content)
10 # Try to find the profile URL
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."""
22 # We should find the root of the node tree (question) the current node belongs to.
23 if node.node_type == "question":
25 elif node.node_type == "answer":
26 question = node.question
27 elif node.node_type == "comment":
31 question = node.question
33 # Now we've got the root question. Let's get the list of active users.
34 active_users = question.get_active_users()
36 for active_user in active_users:
37 if active_user.username.lower().startswith(username.lower()):
38 profile_url = active_user.get_absolute_url()
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)