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

后端 未结 1 975
小鲜肉
小鲜肉 2020-12-29 17:48

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

相关标签:
1条回答
  • 2020-12-29 17:57

    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)
    
        ....
    
    0 讨论(0)
提交回复
热议问题