]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/utils/userlinking.py
don't focus recaptcha field
[osqa.git] / forum / utils / userlinking.py
index 39f8bbbe6a3de28d385914f96f6e2ff5cda35c27..741a950775afe6d7ea7abc6714e4a904f94495d6 100644 (file)
@@ -1,9 +1,15 @@
+# -*- coding: utf-8 -*-
+
 import re
 
 import re
 
-from forum.models import User,  Question,  Answer,  Comment
+from django.utils.encoding import smart_unicode
+from forum.models.user import User
 
 def find_best_match_in_name(content,  uname,  fullname,  start_index):      
 
 def find_best_match_in_name(content,  uname,  fullname,  start_index):      
-    end_index = start_index + len(fullname)    
+    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():
     
     while end_index > start_index:
         if content[start_index : end_index].lower() == fullname.lower():
@@ -17,23 +23,11 @@ def find_best_match_in_name(content,  uname,  fullname,  start_index):
             
     return uname    
 
             
     return uname    
 
-APPEAL_PATTERN = re.compile(r'@\w+')
+APPEAL_PATTERN = re.compile(r'(?<!\w)@\w+', re.UNICODE)
 
 def auto_user_link(node, content):
 
 def auto_user_link(node, content):
-    
-    # We should find the root of the node tree (question) the current node belongs to.
-    if isinstance(node,  Question):
-        question = node
-    elif isinstance(node,  Answer):
-        question = node.question
-    elif isinstance(node,  Comment):
-        if node.question:
-            question = node.question
-        elif node.answer:
-            question = node.answer.question
-    
-    # Now we've got the root question. Let's get the list of active users.
-    active_users = question.get_active_users()
+
+    active_users = node.absolute_parent.get_active_users()
     
     appeals = APPEAL_PATTERN.finditer(content)
 
     
     appeals = APPEAL_PATTERN.finditer(content)
 
@@ -41,17 +35,17 @@ def auto_user_link(node, content):
 
     for appeal in appeals:
         # Try to find the profile URL
 
     for appeal in appeals:
         # Try to find the profile URL
-        username = appeal.group(0)[1:]
+        username = smart_unicode(appeal.group(0)[1:])
         
         matches = []
         
         for user in active_users:
         
         matches = []
         
         for user in active_users:
-            if user.username.lower().startswith(username.lower()):
+            if smart_unicode(user.username).lower().startswith(username.lower()):
                 matches.append(user)
                 
         if len(matches) == 1:
             replacements.append(
                 matches.append(user)
                 
         if len(matches) == 1:
             replacements.append(
-                                (find_best_match_in_name(content,  username,  matches[0].username,  appeal.start(0) + 1),  matches[0])
+                                (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)
                                 )                                
         elif len(matches) == 0:
             matches = User.objects.filter(username__istartswith=username)
@@ -63,16 +57,16 @@ def auto_user_link(node, content):
         final_match = ""
         
         for user in matches:
         final_match = ""
         
         for user in matches:
-            user_match = find_best_match_in_name(content,  username,  user.username,  appeal.start(0) + 1)
+            user_match = find_best_match_in_name(content,  username,  smart_unicode(user.username),  appeal.start(0) + 1)
             
             if (len(user_match) < len(final_match)): 
                 continue
                 
             if (len(user_match) == len(final_match)):
             
             if (len(user_match) < len(final_match)): 
                 continue
                 
             if (len(user_match) == len(final_match)):
-                if not (user.username.lower() == user_match.lower()):
+                if not (smart_unicode(user.username).lower() == user_match.lower()):
                     continue
                     
                     continue
                     
-                if (best_user_match and (best_user_match.username == final_match)):
+                if (best_user_match and (smart_unicode(best_user_match.username) == final_match)):
                     continue
                     
             best_user_match = user
                     continue
                     
             best_user_match = user
@@ -81,7 +75,7 @@ def auto_user_link(node, content):
         replacements.append((final_match,  best_user_match))            
     
     for replacement in replacements:
         replacements.append((final_match,  best_user_match))            
     
     for replacement in replacements:
-        to_replace = "@" + replacement[0]
+        to_replace = "@" + smart_unicode(replacement[0])
         profile_url = replacement[1].get_absolute_url()
         
         auto_link = '<a href="%s">%s</a>' % (profile_url, to_replace)
         profile_url = replacement[1].get_absolute_url()
         
         auto_link = '<a href="%s">%s</a>' % (profile_url, to_replace)