From bdb70243eb9189c92189c05afad9599ab1453946 Mon Sep 17 00:00:00 2001 From: jordan Date: Wed, 29 Dec 2010 20:12:18 +0000 Subject: [PATCH] Working on the @username convention. We can call user even if using only the first part of his username, for example, we can call "Jordan Jambazov" only using @Jordan. The supplied module forum.utils.userlinking deals with the active users (the one that took part in the discussion writing question, comment or answer). git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@650 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/markdownext/mdx_urlize.py | 37 --------------------------- forum/models/node.py | 3 ++- forum/models/question.py | 23 +++++++++++++++++ forum/utils/userlinking.py | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 forum/utils/userlinking.py diff --git a/forum/markdownext/mdx_urlize.py b/forum/markdownext/mdx_urlize.py index 071e6b2..94fa516 100644 --- a/forum/markdownext/mdx_urlize.py +++ b/forum/markdownext/mdx_urlize.py @@ -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'

http://example.com/

' - ->>> md.convert('go to http://example.com') -u'

go to http://example.com

' - ->>> md.convert('example.com') -u'

example.com

' - ->>> md.convert('example.net') -u'

example.net

' - ->>> md.convert('www.example.us') -u'

www.example.us

' - ->>> md.convert('(www.example.us/path/?name=val)') -u'

(www.example.us/path/?name=val)

' - ->>> md.convert('go to now!') -u'

go to http://example.com now!

' - -Negative examples: - ->>> md.convert('del.icio.us') -u'

del.icio.us

' - -""" - import markdown # Global Vars diff --git a/forum/models/node.py b/forum/models/node.py index 65eb2dc..0dfe1d2 100644 --- a/forum/models/node.py +++ b/forum/models/node.py @@ -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) diff --git a/forum/models/question.py b/forum/models/question.py index a185e02..010647c 100644 --- a/forum/models/question.py +++ b/forum/models/question.py @@ -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 index 0000000..fd7a66d --- /dev/null +++ b/forum/utils/userlinking.py @@ -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 = '%s' % (profile_url, appeal) + content = content.replace(appeal, auto_link) + + return content \ No newline at end of file -- 2.39.5