Skip to main content

BaseApp Reactions

Project description

BaseApp Reactions

Reusable app to enable User's reactions on any model, features like like/dislike or any other reactions type, customizable for project's needs.

How to install:

Install in your environment:

pip install baseapp-reactions

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

How to use

Add baseapp_reactions to your project's INSTALLED_APPS

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

Add baseapp_reactions.permissions.ReactionsPermissionsBackend to the AUTHENTICATION_BACKENDS list in your django settings file.

AUTHENTICATION_BACKENDS = [
    # ...
    "baseapp_reactions.permissions.ReactionsPermissionsBackend",
    # ...
]

Now make sure all models you'd like to get reactions also inherits ReactableModel, like:

from baseapp_reactions.models import ReactableModel

class Comment(models.Model, ReactableModel):
    body = models.Textfield()

Also make sure your GraphQL object types extends ReactionsInterface interface:

from baseapp_reactions.graphql.object_types import ReactionsInterface

class CommentNode(DjangoObjectType):
    class Meta:
        interfaces = (relay.Node, ReactionsInterface)

Expose ReactionsMutations and ReactionsQueries in your GraphQL/graphene endpoint, like:

from baseapp_reactions.graphql.mutations import ReactionsMutations
from baseapp_reactions.graphql.queries import ReactionsQueries

class Query(graphene.ObjectType, ReactionsQueries):
    pass

class Mutation(graphene.ObjectType, ReactionsMutations):
    pass

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

This will expose reactionToggle mutation and add fields and connections to all your GraphqlQL Object Types using interface ReactionsInterface.

Example:

{
    comment(id: $id) {
        id
        reactionsCount {
            LIKE
            DISLIKE
            total
        }
        reactions(first: 10) {
            edges {
                node {
                    reactionType
                    user {
                        name
                    }
                }
            }
        }
    }
}

How to to customize the Reaction model

In some cases you may need to extend Reaction model, and we can do it following the next steps:

Start by creating a barebones django app:

mkdir my_project/reactions
touch my_project/reactions/__init__.py
touch my_project/reactions/models.py

Your models.py will look something like this:

from django.db import models
from django.utils.translation import gettext_lazy as _

from baseapp_reactions.models import AbstractBaseReaction

class Reaction(AbstractBaseReaction):
    custom_field = models.CharField(null=True)

    class ReactionTypes(models.IntegerChoices):
        LIKE = 1, _("like")
        DISLIKE = -1, _("dislike")

        @property
        def description(self):
            return self.label

Now make your to add your new app to your INSTALLED_APPS and run makemigrations and migrate like any normal django app.

Now in your settings/base.py make sure to tell baseapp-reactions what is your custom model for Reaction:

BASEAPP_REACTIONS_REACTION_MODEL = 'reactions.Reaction'

How to develop

Clone the project inside your project's backend dir:

git clone git@github.com:silverlogic/baseapp-backend.git

And manually install the package:

pip install -e baseapp-backend/baseapp-reactions

The -e flag will make it like any change you make in the cloned repo files will effect into the project.

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-reactions-0.3.3.tar.gz (13.6 kB view details)

Uploaded Source

File details

Details for the file baseapp-reactions-0.3.3.tar.gz.

File metadata

  • Download URL: baseapp-reactions-0.3.3.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for baseapp-reactions-0.3.3.tar.gz
Algorithm Hash digest
SHA256 93f1b958ee06676a1c66b499975c914ae0b446b3b67381da68dabb262ff476c5
MD5 b83d6bc12dda349f35d91b9d325c676b
BLAKE2b-256 3cbb2610ce2839fc089a7ffd4f8d70f1231660d61e0ef70006a925811a19aa85

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