]> git.openstreetmap.org Git - osqa.git/commitdiff
Working on the @username convention. We can call user even if using only the first...
authorjordan <jordan@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Wed, 29 Dec 2010 20:12:18 +0000 (20:12 +0000)
committerjordan <jordan@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Wed, 29 Dec 2010 20:12:18 +0000 (20:12 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@650 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/markdownext/mdx_urlize.py
forum/models/node.py
forum/models/question.py
forum/utils/userlinking.py [new file with mode: 0644]

index 071e6b25a422af25d0b2ed4ad77e50a40597b041..94fa516d4549508c15488135eff8eaae03fa3b31 100644 (file)
@@ -1,40 +1,3 @@
-"""A more liberal autolinker
-
-Inspired by Django's urlize function.
-
-Positive examples:
-
->>> import markdown
->>> md = markdown.Markdown(extensions=['urlize'])
-
->>> md.convert('http://example.com/')
-u'<p><a href="http://example.com/">http://example.com/</a></p>'
-
->>> md.convert('go to http://example.com')
-u'<p>go to <a href="http://example.com">http://example.com</a></p>'
-
->>> md.convert('example.com')
-u'<p><a href="http://example.com">example.com</a></p>'
-
->>> md.convert('example.net')
-u'<p><a href="http://example.net">example.net</a></p>'
-
->>> md.convert('www.example.us')
-u'<p><a href="http://www.example.us">www.example.us</a></p>'
-
->>> md.convert('(www.example.us/path/?name=val)')
-u'<p>(<a href="http://www.example.us/path/?name=val">www.example.us/path/?name=val</a>)</p>'
-
->>> md.convert('go to <http://example.com> now!')
-u'<p>go to <a href="http://example.com">http://example.com</a> now!</p>'
-
-Negative examples:
-
->>> md.convert('del.icio.us')
-u'<p>del.icio.us</p>'
-
-"""
-
 import markdown
 
 # Global Vars
index 65eb2dc66857460e5d9714188a9a91188c0fa44c..0dfe1d2cea73ebe7e90ed70bb325c263900250a9 100644 (file)
@@ -6,6 +6,7 @@ import markdown
 from django.utils.translation import ugettext as _
 from django.utils.safestring import mark_safe
 from django.utils.html import strip_tags
+from forum.utils.userlinking import auto_user_link
 from forum.utils.html import sanitize_html
 from utils import PickledObjectField
 
@@ -337,7 +338,7 @@ class Node(BaseModel, NodeContent):
     def activate_revision(self, user, revision, extensions=['urlize']):
         self.title = revision.title
         self.tagnames = revision.tagnames
-        self.body = self._as_markdown(revision.body, *extensions)
+        self.body = auto_user_link(self, self._as_markdown(revision.body, *extensions))
 
         self.active_revision = revision
         self.update_last_activity(user)
index a185e026bd623d56e0edb4d392ab9f2af95571db..010647cd3e9ccc736e2e14b77af6f6c2389fb66d 100644 (file)
@@ -61,6 +61,29 @@ class Question(Node):
             cache.set(cache_key, related_list, 60 * 60)
 
         return [Question.objects.get(id=r['id']) for r in related_list]
+    
+    def get_active_users(self):
+        active_users = []
+        
+        active_users.append(self.author)
+        
+        for answer in self.answers:
+            active_users.append(answer.author)
+        
+        for child in self.children.all():
+            active_users.append(child.author)
+            for grandchild in child.children.all():
+                active_users.append(grandchild.author)
+        
+        # Remove duplicates
+        unique_active_users = []
+        for user in active_users:
+            if user not in unique_active_users:
+                unique_active_users.append(user)
+        active_users = unique_active_users
+        del unique_active_users
+        
+        return active_users
 
 
 def question_viewed(instance, **kwargs):
diff --git a/forum/utils/userlinking.py b/forum/utils/userlinking.py
new file mode 100644 (file)
index 0000000..fd7a66d
--- /dev/null
@@ -0,0 +1,44 @@
+import re
+
+from forum.models.user import User
+
+def auto_user_link(node, content):
+    patern = r'@\w+'
+    appeals = re.findall(patern, content)
+
+    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
+            
+            # Now we've got the root question. Let's get the list of active users.
+            active_users = question.get_active_users()
+            
+            for active_user in active_users:
+                if active_user.username.lower().startswith(username.lower()):
+                    profile_url = active_user.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)
+    
+    return content
\ No newline at end of file