Welcome to django-mentions’s documentation!

Contents:

django-mentions

https://badge.fury.io/py/django-mentions.svg https://travis-ci.org/marfyl/django-mentions.svg?branch=master https://codecov.io/gh/marfyl/django-mentions/branch/master/graph/badge.svg

Documentation

The purpose of this package is to handle in some way mentions to users in a text field of a model. You can choose the model you want, the field you want to listen to mentions, the pattern you use to codify the mention and the callback to notify to your app.

The package will notify to callback function each time there is a mention in this field of the model. Then you can act accordingly on your application requisites.

The full documentation is at https://django-mentions.readthedocs.io.

Quickstart

Install django-mentions:

pip install django-mentions

Usage

To use django-mentions in a project, add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'mentions',
    ...
)

Define a signal for the callback

from django.dispatch import receiver
from django.core.signals import request_finished

@receiver(request_finished)
def post_detect_mention_callback(sender, **kwargs):
    """ You will receive information of the mention
    user_from: kwargs.get('user_from')
        User that mentions
    object_pk: kwargs.get('object_pk')
        User's Pk that has been mentioned
    target: kwargs.get('target')
        The object where the mention was made
    """

    # Your code here

Register a model and field in which you want to detect mentions. You can override the pattern if you want.

from django.apps import AppConfig
from mentions.registry import register

class MyAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        model = Post
        field = 'description'
        callback = post_detect_mention_callback

        register(model, field, callback)

At this point the library will notify to the callback each time there is a mention in the field of the registered model. Thats all! :)

def register(model, field, callback, pattern):
"""
This method handles the mentions about the model in the field and notify to the callback when there is any mention

Parameters
----------
model : Models
    The model to register for detect mentions
field : str
    Field of the model to detect mentions
callback : function
    Callback function to notify when there are mentions
pattern : regular expression
    The pattern to codify the mentions (default r'class="mention" data-user=[\'"]?([^\'" >]+)')

"""

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Installation

At the command line:

$ mkvirtualenv django-mentions
$ pip install django-mentions

Usage

To use django-mentions in a project, add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'mentions',
    ...
)

Define a signal for the callback

from django.dispatch import receiver
from django.core.signals import request_finished

@receiver(request_finished)
def post_detect_mention_callback(sender, **kwargs):
    """ You will receive information of the mention
    user_from: kwargs.get('user_from')
        User that mentions
    object_pk: kwargs.get('object_pk')
        User's Pk that has been mentioned
    target: kwargs.get('target')
        The object where the mention was made
    """

    # Your code here

Register a model and field in which you want to detect mentions. You can override the pattern if you want.

from django.apps import AppConfig
from mentions.registry import register

class MyAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        model = Post
        field = 'description'
        callback = post_detect_mention_callback

        register(model, field, callback)

At this point the library will notify to the callback each time there is a mention in the field of the registered model. Thats all! :)

def register(model, field, callback, pattern):
"""
This method handles the mentions about the model in the field and notify to the callback when there is any mention

Parameters
----------
model : Models
    The model to register for detect mentions
field : str
    Field of the model to detect mentions
callback : function
    Callback function to notify when there are mentions
pattern : regular expression
    The pattern to codify the mentions (default r'class="mention" data-user=[\'"]?([^\'" >]+)')

"""

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

You can contribute in many ways:

Types of Contributions

Report Bugs

Report bugs at https://github.com/marfyl/django-mentions/issues.

If you are reporting a bug, please include:

  • Your operating system name and version.
  • Any details about your local setup that might be helpful in troubleshooting.
  • Detailed steps to reproduce the bug.

Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with “bug” is open to whoever wants to implement it.

Implement Features

Look through the GitHub issues for features. Anything tagged with “feature” is open to whoever wants to implement it.

Write Documentation

django-mentions could always use more documentation, whether as part of the official django-mentions docs, in docstrings, or even on the web in blog posts, articles, and such.

Submit Feedback

The best way to send feedback is to file an issue at https://github.com/marfyl/django-mentions/issues.

If you are proposing a feature:

  • Explain in detail how it would work.
  • Keep the scope as narrow as possible, to make it easier to implement.
  • Remember that this is a volunteer-driven project, and that contributions are welcome :)

Get Started!

Ready to contribute? Here’s how to set up django-mentions for local development.

  1. Fork the django-mentions repo on GitHub.

  2. Clone your fork locally:

    $ git clone git@github.com:your_name_here/django-mentions.git
    
  3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:

    $ mkvirtualenv django-mentions
    $ cd django-mentions/
    $ python setup.py develop
    
  4. Create a branch for local development:

    $ git checkout -b name-of-your-bugfix-or-feature
    

    Now you can make your changes locally.

  5. When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:

    $ flake8 mentions tests
    $ python setup.py test
    $ tox
    

    To get flake8 and tox, just pip install them into your virtualenv.

  6. Commit your changes and push your branch to GitHub:

    $ git add .
    $ git commit -m "Your detailed description of your changes."
    $ git push origin name-of-your-bugfix-or-feature
    
  7. Submit a pull request through the GitHub website.

Pull Request Guidelines

Before you submit a pull request, check that it meets these guidelines:

  1. The pull request should include tests.
  2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
  3. The pull request should work for Python 2.6, 2.7, and 3.3, and for PyPy. Check https://travis-ci.org/marfyl/django-mentions/pull_requests and make sure that the tests pass for all supported Python versions.

Tips

To run a subset of tests:

$ python -m unittest tests.test_mentions

Credits

Development Lead

Contributors

None yet. Why not be the first?

History

1.0.0 (2018-09-25)

  • First release on PyPI.