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.add_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.2.7.tar.gz (26.8 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: baseapp_comments-0.2.7.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for baseapp_comments-0.2.7.tar.gz
Algorithm Hash digest
SHA256 442a2a59faa6afc183aee817882a8369715c4ded8c2c29959579eb556ee5c56f
MD5 930e24bb519d9f7b40311030e9707cd0
BLAKE2b-256 d1d4eadad305410449a18ac6496ed3ac21cb92564af388399a7fa5d29123d7d7

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