king iyk
king iyk

Reputation: 63

how to mention/tag users with '@' on a django developed project

I am trying to implement the "@" feature used on social sites such as twitter to tag or mention users on my django project. Say for example, "@stack" when clicked should go to stack's profile.

How to do that would be helpful to me.

Upvotes: 1

Views: 3035

Answers (1)

binpy
binpy

Reputation: 4194

The mention system handled on editor right? Here is django-markdown-editor who providing to direct mention user @[username] => @username

see also about function markdown_find_mentions, useful if you need to implement the notification system for user mentioned by another users, something like stackoverflow.

def markdown_find_mentions(markdown_text):
    """
    To find the users that mentioned
    on markdown content using `BeautifulShoup`.

    input  : `markdown_text` or markdown content.
    return : `list` of usernames.
    """
    mark = markdownify(markdown_text)
    soup = BeautifulSoup(mark, 'html.parser')
    return list(set(
        username.text[1::] for username in
        soup.findAll('a', {'class': 'direct-mention-link'})
    ))

and this a simply flow process to do;

  1. When create a comment/post/etc, find all users mentioned and create a notification.
  2. When edit a commemnt/post/etc, find all new users mentioned and create a notification.

Makesure the Notification have a sender and receiver.

class Notification(TimeStampedModel):
    sender = models.ForeignKey(User, related_name='sender_n')
    receiver = models.ForeignKey(User, related_name='receiver_n')
    content_type = models.ForeignKey(ContentType, related_name='n', on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    read = models.BooleanField(default=False)

    ....

Upvotes: 6

Related Questions