]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/utils/userlinking.py
Stick with CC-BY-SA 2.0
[osqa.git] / forum / utils / userlinking.py
index fd7a66dff0b16bd0d894b22aa375f2c30b7a44be..741a950775afe6d7ea7abc6714e4a904f94495d6 100644 (file)
@@ -1,44 +1,84 @@
+# -*- coding: utf-8 -*-
+
 import re
 
+from django.utils.encoding import smart_unicode
 from forum.models.user import User
 
+def find_best_match_in_name(content,  uname,  fullname,  start_index):      
+    uname = smart_unicode(uname)
+    fullname = smart_unicode(fullname)
+
+    end_index = start_index + len(fullname)
+    
+    while end_index > start_index:
+        if content[start_index : end_index].lower() == fullname.lower():
+            return content[start_index : end_index]
+            
+        while len(fullname) and fullname[-1] != ' ':
+            fullname = fullname[:-1]
+            
+        fullname = fullname.rstrip()
+        end_index = start_index + len(fullname)
+            
+    return uname    
+
+APPEAL_PATTERN = re.compile(r'(?<!\w)@\w+', re.UNICODE)
+
 def auto_user_link(node, content):
-    patern = r'@\w+'
-    appeals = re.findall(patern, content)
+
+    active_users = node.absolute_parent.get_active_users()
+    
+    appeals = APPEAL_PATTERN.finditer(content)
+
+    replacements = []
 
     for appeal in appeals:
         # Try to find the profile URL
-        username = appeal[1:]
-        profile_url = None
-
-        try:
-            user = User.objects.get(username__iexact=username)
-            profile_url = user.get_absolute_url()
-        except User.DoesNotExist:
-            """If we don't find the user from the first time, the interesting part
-               begins. We look through all the authors (looking through question,
-               comments, answers, and if it matches some of the -- we link him."""
-            
-            # We should find the root of the node tree (question) the current node belongs to.
-            if node.node_type == "question":
-                question = node
-            elif node.node_type == "answer":
-                question = node.question
-            elif node.node_type == "comment":
-                if not node.question:
-                    question = node
-                else:
-                    question = node.question
+        username = smart_unicode(appeal.group(0)[1:])
+        
+        matches = []
+        
+        for user in active_users:
+            if smart_unicode(user.username).lower().startswith(username.lower()):
+                matches.append(user)
+                
+        if len(matches) == 1:
+            replacements.append(
+                                (find_best_match_in_name(content,  username, smart_unicode(matches[0].username),  appeal.start(0) + 1),  matches[0])
+                                )                                
+        elif len(matches) == 0:
+            matches = User.objects.filter(username__istartswith=username)
             
-            # Now we've got the root question. Let's get the list of active users.
-            active_users = question.get_active_users()
+        if (len(matches) == 0):
+                continue
+        
+        best_user_match = None
+        final_match = ""
+        
+        for user in matches:
+            user_match = find_best_match_in_name(content,  username,  smart_unicode(user.username),  appeal.start(0) + 1)
             
-            for active_user in active_users:
-                if active_user.username.lower().startswith(username.lower()):
-                    profile_url = active_user.get_absolute_url()
+            if (len(user_match) < len(final_match)): 
+                continue
+                
+            if (len(user_match) == len(final_match)):
+                if not (smart_unicode(user.username).lower() == user_match.lower()):
+                    continue
+                    
+                if (best_user_match and (smart_unicode(best_user_match.username) == final_match)):
+                    continue
+                    
+            best_user_match = user
+            final_match = user_match
+        
+        replacements.append((final_match,  best_user_match))            
+    
+    for replacement in replacements:
+        to_replace = "@" + smart_unicode(replacement[0])
+        profile_url = replacement[1].get_absolute_url()
         
-        if (profile_url is not None) and (appeal is not None):
-            auto_link = '<a href="%s">%s</a>' % (profile_url, appeal)
-            content = content.replace(appeal, auto_link)
+        auto_link = '<a href="%s">%s</a>' % (profile_url, to_replace)
+        content = content.replace(to_replace, auto_link)        
     
-    return content
\ No newline at end of file
+    return content