Skip to main content

Baseapp Rating

Project description

BaseApp Ratings

Reusable app to enable User's ratings on any model.

How to install:

Install in your environment:

pip install baseapp-ratings

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

How to use

Add baseapp_ratings to your project's INSTALLED_APPS

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

Add baseapp_ratings.permissions.RatingsPermissionsBackend to the AUTHENTICATION_BACKENDS list in your django settings file.

AUTHENTICATION_BACKENDS = [
    # ...
    "baseapp_ratings.permissions.RatingsPermissionsBackend",
    # ...
]

Now make sure all models you'd like to get ratings also inherits RatableModel, like:

from baseapp_ratings.models import RatableModel

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

Also make sure your GraphQL object types extends RatingsInterface interface:

from baseapp_ratings.graphql.object_types import RatingsInterface

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

Expose RatingsMutations and RatingsQueries in your GraphQL/graphene endpoint, like:

from baseapp_ratings.graphql.mutations import RatingsMutations
from baseapp_ratings.graphql.queries import RatingsQueries

class Query(graphene.ObjectType, RatingsQueries):
    pass

class Mutation(graphene.ObjectType, RatingsMutations):
    pass

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

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

Example:

{
    user(id: $id) {
        id
        ratingsCount
        ratingsSum
        ratingsAverage
        ratings(first: 10) {
            edges {
                node {
                    user {
                        name
                    }
                    value
                }
            }
        }
    }
}

How to to customize the Rating model

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

Start by creating a barebones django app:

mkdir my_project/ratings
touch my_project/ratings/__init__.py
touch my_project/ratings/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_ratings.models import AbstractBaseRate

class Rate(AbstractBaseRate):
    custom_field = models.CharField(null=True)

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-ratings what is your custom model for Rating:

BASEAPP_RATINGS_RATE_MODEL = 'ratings.Rate'

If you want to define a maximum value for your rating:

BASEAPP_MAX_RATING_VALUE = 5

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-ratings

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-ratings-0.3.0.tar.gz (12.7 kB view details)

Uploaded Source

File details

Details for the file baseapp-ratings-0.3.0.tar.gz.

File metadata

  • Download URL: baseapp-ratings-0.3.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for baseapp-ratings-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5a708fad17bca4182536822c80499d80d113cb22ed40118d1ac8398a3b16fda3
MD5 1c2023940a0b1ece4f6a3ac21369b34e
BLAKE2b-256 ccf356fef72a673729b64801ac14cfadd45d5ffa14c918f36a14633209d288be

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