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:
Add dependencies to your requirements/base.txt
file:
baseapp-core @ git+https://github.com/silverlogic/baseapp-backend.git@v0.1#subdirectory=baseapp-core
baseapp-reactions @ git+https://github.com/silverlogic/baseapp-backend.git@v0.1#subdirectory=baseapp-reactions
And run provision or manually pip install -r requirements/base.ext
If you want to develop, install using this other guide.
How to use
Add baseapp_reactions
to your project's INSTALLED_APPS
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 ReactionsNode
interface:
from baseapp_reactions.graphql.object_types import ReactionsNode
class CommentNode(DjangoObjectType):
class Meta:
interfaces = (relay.Node, ReactionsNode)
Expose ReactionsMutations
and ReactionsQuery
in your GraphQL/graphene endpoint, like:
from baseapp_reactions.graphql.mutations import ReactionsMutations
from baseapp_reactions.graphql.queries import ReactionsQuery
class Query(graphene.ObjectType, ReactionsQuery):
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 ReactionsNode
.
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'
Writing test cases in your project
There is a AbstractReactionFactory
which helps you write other factories:
import factory
from baseapp_reactions.tests.factories import AbstractReactionFactory
class CommentFactory(factory.django.DjangoModelFactory):
class Meta:
model = "comments.Comment"
class CommentReactionFactory(AbstractReactionFactory):
target = factory.SubFactory(CommentFactory)
class Meta:
model = "baseapp_reactions.Reaction"
# OR if you have a custom model, point to it:
model = "reactions.Reaction"
In the above example we have a easy way to make reactions to any comment into the database for testing proporses using CommentReactionFactory
.
How to delevop
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file baseapp-reactions-0.1.3.tar.gz
.
File metadata
- Download URL: baseapp-reactions-0.1.3.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 240d3dc535d2ba489ff9abdc3478224b8f4a2a1430471e222bdaac805a6bb2fa |
|
MD5 | 42ba2e12bf6400f4e7459d068d04de35 |
|
BLAKE2b-256 | aa87657993046973f27fdf270018dcf438393bd853e9b997c04da9c530e13d88 |