Skip to main content

Django likes app

Project description

Build status Code health Python versions Requirements Status Software license PyPI downloads Code coverage

django-ok-likes is a liking app for Django, which allows users “like” and “unlike” any model instance. All functionality provides through django-rest-framework API views. Template tags and jinja’s global functions provide the ability to see who liked an object, which objects were liked by current user and count of likes for a given object.

Installation

Install with pip:

pip install django-ok-likes

Update INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'likes',
    'rest_framework',
    ...
]

Make migrations:

python manage.py migrate

Add likes.api.urls to your project urlpatterns:

urlpatterns = [
    ...
    path('api/v1/', include('likes.api.urls')),
    ...
]

Available settings

Add the models that you want to like to LIKES_MODELS in your settings file:

LIKES_MODELS = {
    "app.Model": {
        'serializer': 'app.api.serializer.YourModelSerializer'
    },
}

You can set any pagination class for ListAPIView:

LIKES_REST_PAGINATION_CLASS = 'core.api.pagination.MyResponsePagination'

Usage

API

Base endpoints

  1. /api/v1/likes/list/ - List API View to return all likes for authenticated user.

    You can set serializer for each model in LIKES_MODELS setting to use it for content object serialization, otherwise, you will get an id of content object.

    For example:

    LIKE_MODELS = {
        "article.Article": {
            'serializer': 'article.api.serializers.ArticleSerializer'
        },
    }

    Use GET parameter search to filter by a content type’s model: /api/v1/likes/list/?search=article

  2. /api/v1/likes/count/ - API View to return count of likes for authenticated user.

  3. /api/v1/likes/is/ - API View to check is given elements are liked by authenticated user. As result, you will get a list of ids.

    Possible payload:

    {
        "type": 'app_label.model',  # '.'.join(object_ct.natural_key())
        "ids": [1,2,3]
    }

    Possible result:

    {
        "ids": [1,3]
    }
  4. /api/v1/likes/toggle/ - API View to like-unlike a given object by authenticated user.

    Possible payload:

    {
        "type": 'app_label.model',  # '.'.join(object_ct.natural_key())
        "id": 1
    }

    Possible result:

    {
        "is_liked": true
    }

Filters

likes_count

Returns a count of likes for a given object:

{{ object|likes_count }}

Template Tags

who_liked

Returns a queryset of users, who liked a given object:

{% who_liked object as fans %}

{% for user in fans %}
    <div class="like">{{ user.get_full_name }} likes {{ object }}</div>
{% endfor %}

likes

Returns a queryset of likes for a given user:

{% likes request.user as user_likes %}
{% for like in user_likes %}
    <div>{{ like }}</div>
{% endfor %}

is_liked

Returns a bool value, which says is a given object liked by a given user:

{% is_liked object request.user as liked %}

Jinja global functions

get_likes_count

The same as the likes_count filter.

Usage:

{{ get_likes_count(object) }}

get_who_liked

The same as the who_liked tag.

Usage:

{{ get_who_liked(object) }}

get_likes

The same as the likes tag.

Usage:

{{ get_likes(request.user) }}

get_is_liked

The same as the is_liked tag.

Usage:

{{ get_is_liked(object, request.user) }}

Signals

likes.signals.object_liked

A signal, which sents immediately after the object was liked and provides the single kwarg of created Like instance.

likes.signals.object_unliked

A signal, which sents immediately after the object was unliked and provides the single kwarg of an object.

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

django-ok-likes-0.7.3.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

django_ok_likes-0.7.3-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file django-ok-likes-0.7.3.tar.gz.

File metadata

  • Download URL: django-ok-likes-0.7.3.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1

File hashes

Hashes for django-ok-likes-0.7.3.tar.gz
Algorithm Hash digest
SHA256 17fb8b7996c3586a2b93fbea8d801a0a7e52391b37e1c93cfdfff762471e64fe
MD5 91fbb8494b35eb3965d92e5f49f99ded
BLAKE2b-256 6eadc22eea0287e61f770f1fde21d97725cf87f82ef56ddbf391e8723439bbad

See more details on using hashes here.

Provenance

File details

Details for the file django_ok_likes-0.7.3-py3-none-any.whl.

File metadata

  • Download URL: django_ok_likes-0.7.3-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1

File hashes

Hashes for django_ok_likes-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9c1c5e849f18fb33836f10ca46d4adc010ba58c2937c59c1f85885e7c9fd5895
MD5 92bf2a3a6d00be4857e1524537b95ee5
BLAKE2b-256 d024492520330e3082b6e64379ba633abb5da2f0069204eac73355f91340e57d

See more details on using hashes here.

Provenance

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