Skip to main content

BaseApp Comments

Project description

BaseApp Comments

Reusable app to handle comments threads.

Whats missing

  • DRF views and serializers

How to install:

This package requires to following packages to be installed:

And install the package with pip install baseapp-comments.

If you want to develop, install using this other guide.

How to setup:

1 - Make sure to have GraphQL websocket enabled in your project

2 - Add baseapp_comments to your project's INSTALLED_APPS and run ./manage.py migrate as any other django model:

INSTALLED_APPS = [
    # ...
    "baseapp_comments",
    # ...
]

3 - Add baseapp_comments.permissions.CommentsPermissionsBackend to the AUTHENTICATION_BACKENDS list in your django settings file.

AUTHENTICATION_BACKENDS = [
    # ...
    "baseapp_comments.permissions.CommentsPermissionsBackend",
    # ...
]

4 - Make sure to add the task routing for the notifications:

CELERY_TASK_ROUTES = {
    "baseapp_comments.notifications.send_reply_created_notification": {
        "exchange": "default",
        "routing_key": "default",
    },
    "baseapp_comments.notifications.send_comment_created_notification": {
        "exchange": "default",
        "routing_key": "default",
    }
}

5 - Expose CommentsMutations, CommentsQueries and CommentsSubscriptions in your GraphQL/graphene endpoint, like:

from baseapp_comments.graphql.mutations import CommentsMutations
from baseapp_comments.graphql.queries import CommentsQueries
from baseapp_comments.graphql.subscriptions import CommentsSubscriptions

class Query(graphene.ObjectType, CommentsQueries):
    pass

class Mutation(graphene.ObjectType, CommentsMutations):
    pass

class Subscription(graphene.ObjectType, CommentsSubscriptions):
    pass

schema = graphene.Schema(query=Query, mutation=Mutation, subscription=Subscription)

How to use

You can customize some settings, bellow are the default values:

BASEAPP_COMMENTS_CAN_ANONYMOUS_VIEW_COMMENTS = True
BASEAPP_COMMENTS_MAX_PINS_PER_THREAD = None
BASEAPP_COMMENTS_ENABLE_GRAPHQL_SUBSCRIPTIONS = True
BASEAPP_COMMENTS_ENABLE_NOTIFICATIONS = True

You need to inherit CommentableModel in your model and make sure to add CommentsInterface to your ObjectType's interfaces like:

CommentableModel

from django.db import models
from baseapp_comments.models import CommentableModel

class MyModel(models.Model, CommentableModel):
    pass

This will add the following fields to your model:

  • comments a reverse relation to Comment model
  • comments_count a JSON field that stores the count of comments for the object like: {total: 5, main: 3, replies: 2, pinned: 1}
  • is_comments_enabled a boolean fields that stores if comments are enabled for the object

Don't forget to run ./manage.py makemigrations and ./manage.py migrate after adding CommentableModel to your model.

CommentsInterface

CommentsInterface is a GraphQL interface that can be used to query for comments. It has the following fields:

  • isCommentsEnabled return the active URLPath
  • commentsCount return CommentsCount objectType with the following fields:
    • total
    • main
    • replies
    • pinned
  • comments list of comments attached to this object
from baseapp_core.graphql import DjangoObjectType
from baseapp_comments.graphql import CommentsInterface


class MyModelObjectType(DjangoObjectType):
    class Meta:
        model = MyModel
        interfaces = (relay.Node, CommentsInterface)

Signals

There are some signals that handles stuff like updating comments count, notify users when a comment is created and send a GraphQL subscription when a comment is created, updated or deleted. You can find all signals in baseapp_comments.signals.

You could disconnect the signals and connect your own handlers if you want to customize the behavior.

import swapper
from django.db.models.signals import post_save
from baseapp_comments.signals import notify_on_comment_created

Comment = swapper.load_model("baseapp_comments", "Comment")

post_save.disconnect(notify_on_comment_created, dispatch_uid="notify_on_comment_created")


def my_custom_notify_on_comment_created(sender, instance, created, **kwargs):
    # your custom code here
    pass
post_save.connect(notify_on_comment_created, sender=Comment, dispatch_uid="notify_on_comment_created")

Permissions

In can inherit baseapp_comments.permissions.CommentsPermissionsBackend to customize the permissions.

from baseapp_comments.permissions import CommentsPermissionsBackend


class MyCommentsPermissionsBackend(CommentsPermissionsBackend):
    def has_perm(self, user_obj, perm, obj=None):
        if perm == "baseapp_comments.comment_with_profile":
            return obj.owner_id == user.id  # only the profile's owner can use

        return super().has_perm(user_obj, perm, obj)

And add it to your AUTHENTICATION_BACKENDS list in your django settings file.

AUTHENTICATION_BACKENDS = [
    # ...
    "myapp.permissions.MyCommentsPermissionsBackend",
    # ...
]

How to develop

General development instructions can be found in main README.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

baseapp_comments-0.1.1.tar.gz (22.7 kB view details)

Uploaded Source

File details

Details for the file baseapp_comments-0.1.1.tar.gz.

File metadata

  • Download URL: baseapp_comments-0.1.1.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for baseapp_comments-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bad115f5c3a77c1ad28c6c0f12f1603ee4163f39794a5dbb94986d6596a68028
MD5 d93866ef7ae3627903b15d4fa731fc31
BLAKE2b-256 db7a1b29a858d0f5ab88918de66f30651ab2b57d74286c9d57871d46cbea1ba6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page