Skip to content
Automatic Webmention functionality for Django models
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
mentions
.gitignore
.travis.yml
LICENSE
MANIFEST.in
README.rst
runtests.py
setup.py

README.rst

Mentions

Mentions lets you add Webmention functionality to any Django model with minimal setup. There is an implementation running at https://django-wm.dev/ with source code available here.

Installation

PyPI: django-wm

pip install django-wm

Setup

Celery

Mentions uses Celery and RabbitMQ for running tasks asynchronously. If you do not use them already you will need to set them up first.

System:

sudo apt install rabbitmq-server

# Create user for celery service and make sure it can access your database
# e.g:
sudo useradd -N -M --system -s /bin/bash celery
sudo usermod -a -G www-data celery

# Run celery
sudo -u celery env/bin/celery -A projectname worker &

Project:

Add celery.py to your root project. For an example implementation see celery.py from the example project.

Project code

  1. Root project settings.py:

    • Set DOMAIN_NAME:

      DOMAIN_NAME = 'your.url.here'  # e.g. 'django-wm.dev'
      
    • Add "mentions" to INSTALLED_APPS:

      INSTALLED_APPS = [
          'django.contrib.auth',
          'django.contrib.contenttypes',
          'django.contrib.flatpages',
          'django.contrib.sites',
          ...
          'mentions',
      ]
      
    • Add :code:'mentions.middleware.WebmentionHeadMiddleware' to MIDDLEWARE:

      MIDDLEWARE = [
          ...
          'mentions.middleware.WebmentionHeadMiddleware',
      ]
      
  2. Root project urls.py

    urlpatterns = [
        ...
        path('webmentions/, include('mentions.urls')),
    ]
    
  3. Include MentionableMixin in the model(s) you want to support webmention functionality.

    IMPORTANT: Any models that include the mixin must also implement all_text and get_absolute_url methods:

    from mentions import MentionableMixin
    ...
    
    class MyModel(MentionableMixin, models.Model):
        ...
        def all_text(self) -> str:
            return f'{self.introduction} {self.content}'
    
        def get_absolute_url() -> str:
            return reverse('app.views.name', kwargs={'slug': self.slug})
    
  4. Update database tables:

    python manage.py makemigrations
    python manage.py migrate
    

Optional Settings

Add these keys to your project settings.py to alter default behaviour.

WEBMENTIONS_AUTO_APPROVE = bool (default: False)

  • True: Received webmentions are automatically approved and may be publicly visible.
  • False: Received webmentions require manual approval before they can be made public.
You can’t perform that action at this time.