Package: baseapp-reactions
BaseApp ReactionsReusable 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 yourrequirements/base.txtfile: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-reactionsAnd run provision or manuallypip install -r requirements/base.extIf you want to develop,install using this other guide.How to useAddbaseapp_reactionsto your project'sINSTALLED_APPSNow make sure all models you'd like to get reactions also inheritsReactableModel, like:frombaseapp_reactions.modelsimportReactableModelclassComment(models.Model,ReactableModel):body=models.Textfield()Also make sure your GraphQL object types extendsReactionsNodeinterface:frombaseapp_reactions.graphql.object_typesimportReactionsNodeclassCommentNode(DjangoObjectType):classMeta:interfaces=(relay.Node,ReactionsNode)ExposeReactionsMutationsandReactionsQueryin your GraphQL/graphene endpoint, like:frombaseapp_reactions.graphql.mutationsimportReactionsMutationsfrombaseapp_reactions.graphql.queriesimportReactionsQueryclassQuery(graphene.ObjectType,ReactionsQuery):passclassMutation(graphene.ObjectType,ReactionsMutations):passschema=graphene.Schema(query=Query,mutation=Mutation)This will exposereactionTogglemutation and add fields and connections to all your GraphqlQL Object Types using interfaceReactionsNode.Example:{comment(id:$id){idreactionsCount{LIKEDISLIKEtotal}reactions(first:10){edges{node{reactionTypeuser{name}}}}}}How to to customize the Reaction modelIn 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.pyYourmodels.pywill look something like this:fromdjango.dbimportmodelsfromdjango.utils.translationimportgettext_lazyas_frombaseapp_reactions.modelsimportAbstractBaseReactionclassReaction(AbstractBaseReaction):custom_field=models.CharField(null=True)classReactionTypes(models.IntegerChoices):LIKE=1,_("like")DISLIKE=-1,_("dislike")@propertydefdescription(self):returnself.labelNow make your to add your new app to yourINSTALLED_APPSand runmakemigrationsandmigratelike any normal django app.Now in yoursettings/base.pymake sure to tell baseapp-reactions what is your custom model for Reaction:BASEAPP_REACTIONS_REACTION_MODEL='reactions.Reaction'Writing test cases in your projectThere is aAbstractReactionFactorywhich 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 usingCommentReactionFactory.How to developClone the project inside your project's backend dir:git clone git@github.com:silverlogic/baseapp-backend.gitAnd manually install the package:pip install -e baseapp-backend/baseapp-reactionsThe-eflag will make it like any change you make in the cloned repo files will effect into the project.