• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a liking app for Django

Pinax Likes

CircleCi Codecov

Table of Contents

About Pinax

Pinax is an open-source platform built on the Django Web Framework. It is an ecosystem of reusable Django apps, themes, and starter project templates. This collection can be found at http://pinaxproject.com.

Important Links

Where you can find what you need:

pinax-likes

pinax-likes is a liking app for Django, allowing users to "like" and "unlike" any model instance in your project. Template tags provide the ability to see who liked an object, what objects a user liked, and more.

pinax-likes is not a karma system. It does not have down-voting.

Overview

Supported Django and Python Versions

Django / Python 3.6 3.7 3.8
2.2 * * *
3.0 * * *

Documentation

Installation

To install pinax-likes:

    $ pip install pinax-likes

Add pinax.likes to your INSTALLED_APPS setting:

    INSTALLED_APPS = [
        # other apps
        "pinax.likes",
    ]

Add the models that you want to be likable to PINAX_LIKES_LIKABLE_MODELS in your settings file:

    PINAX_LIKES_LIKABLE_MODELS = {
        "app.Model": {}  # override default config settings for each model in this dict
    }

Add pinax.likes.auth_backends.CanLikeBackend to your AUTHENTICATION_BACKENDS (or use your own custom version checking against the pinax.likes.can_like permission):

    AUTHENTICATION_BACKENDS = [
        # other backends
        "pinax.likes.auth_backends.CanLikeBackend",
    ]

Add pinax.likes.urls to your project urlpatterns:

    urlpatterns = [
        # other urls
        url(r"^likes/", include("pinax.likes.urls", namespace="pinax_likes")),
    ]

Usage

Add each model that you want to be likable to the PINAX_LIKES_LIKABLE_MODELS setting:

    PINAX_LIKES_LIKABLE_MODELS = {
        "profiles.Profile": {},
        "videos.Video": {},
        "biblion.Post": {},
    }

Display "like" widgets in your Django templates. Suppose you have a detail page for a blog post. First load the template tags:

    {% load pinax_likes_tags %}

In the body where you want the liking widget to go, add:

    {% likes_widget request.user post %}

Finally, ensure you have eldarion-ajax installed:

Eldarion AJAX

The likes_widget templatetag above and the "toggle like" view both conform to an AJAX response that eldarion-ajax understands. Furthermore, the templates that ship with this project will work seemlessly with eldarion-ajax. Include the eldarion-ajax.min.js Javascript package in your base template:

    {% load staticfiles %}
    <script src="{% static "js/eldarion-ajax.min.js" %}"></script>

and include eldarion-ajax in your site JavaScript:

    require('eldarion-ajax');

Using Eldarion AJAX is optional. You can roll your own JavaScript handling as the view also returns data in addition to rendered HTML. Furthermore, if you don't want ajax at all the view will handle a regular POST and perform a redirect.

Signals

Both of these signals are sent from the Like model in the view that processes the toggling of likes and unlikes.

pinax.likes.signals.object_liked

This signal is sent immediately after the object is liked and provides the single kwarg of like which is the created Like instance.

pinax.likes.signals.object_unliked

This signal is sent immediately after the object is unliked and provides the single kwarg of object which is the object that was just unliked.

Filters

likes_count

Returns the number of likes for a given object:

    {{ obj|likes_count }}

Template Tags

who_likes

An assignment tag that fetches a list of likes for a given object:

    {% who_likes car as car_likes %}

    {% for like in car_likes %}
        <div class="like">{{ like.sender.get_full_name }} likes {{ car }}</div>
    {% endfor %}

likes

The likes tag will fetch into a context variable a list of objects that the given user likes. This tag has two forms:

  1. Obtain likes of every model listed in settings.PINAX_LIKES_LIKABLE_MODELS:
    {% likes user as objs %}
  1. Obtain likes for specific models:
    {% likes user "app.Model" as objs %}
Example:
    {% likes request.user "app.Model" as objs %}
    {% for obj in objs %}
        <div>{{ obj }}</div>
    {% endfor %}

render_like

This renders a like. It combines well with the likes templatetag for showing a list of likes:

    {% likes user as like_list %}
    <ul>
        {% for like in like_list %}
            <li>{% render_like like %}</li>
        {% endfor %}
    </ul>

The render_like tag looks in the following places for the template to render. Any of them can be overwritten as needed, allowing you to customize the rendering of the like on a per model and per application basis:

  • pinax/likes/app_name/model.html
  • pinax/likes/app_name/like.html
  • pinax/likes/_like.html

likes_widget

This renders a fragment of HTML the user clicks on to unlike or like objects. It only has two required parameters, the user and the object:

    {% likes_widget user object %}

It renders pinax/likes/_widget.html. A second form for this templatetag specifies the template to be rendered:

    {% likes_widget request.user post "pinax/likes/_widget_brief.html" %}

liked

This template tag decorates an iterable of objects with a liked boolean indicating whether or not the specified user likes each object in the iterable:

    {% liked objects by request.user as varname %}
    {% for obj in varname %
        <div>{% if obj.liked %}* {% endif %}{{ obj.title }}</div>
    {% endfor %}

Settings

PINAX_LIKES_LIKABLE_MODELS

A dictionary keyed by "<appname.model>". Each model value is a dictionary containing context keys and values.

Context value keys are CSS element names used in template rendering for each model:

"count_text_singular"
"count_text_plural"
"css_class_off"
"css_class_on"
"like_text_off"
"like_text_on"

Here is an example from the test settings used on this project found in runtests.py.

    PINAX_LIKES_LIKABLE_MODELS = {
        "auth.User": {
            "like_text_on": "unlike",
            "css_class_on": "fa-heart",
            "like_text_off": "like",
            "css_class_off": "fa-heart-o",
            "allowed": lambda user, obj: True
        },
        "tests.Demo": {
            "like_text_on": "unlike",
            "css_class_on": "fa-heart",
            "like_text_off": "like",
            "css_class_off": "fa-heart-o"
        }
    },

Templates

pinax-likes uses minimal template snippets rendered with template tags.

Default templates are provided by the pinax-templates app in the likes section of that project.

Reference pinax-templates installation instructions to include these templates in your project.

View live pinax-templates examples and source at Pinax Templates!

Customizing Templates

Override the default pinax-templates templates by copying them into your project subdirectory pinax/likes/ on the template path and modifying as needed.

For example if your project doesn't use Bootstrap, copy the desired templates then remove Bootstrap and Font Awesome class names from your copies. Remove class references like class="btn btn-success" and class="icon icon-pencil" as well as bootstrap from the {% load i18n bootstrap %} statement. Since bootstrap template tags and filters are no longer loaded, you'll also need to update {{ form|bootstrap }} to {{ form }} since the "bootstrap" filter is no longer available.

_like.html

_widget.html

_widget_brief.html

Change Log

4.0.0

  • Drop Django 1.11, 2.0, and 2.1, and Python 2,7, 3.4, and 3.5 support
  • Add Django 2.2 and 3.0, and Python 3.6, 3.7, and 3.8 support
  • Update packaging configs
  • Direct users to community resources

3.0.3

  • Change receiver_object_id type to BigIntegerField

3.0.2

  • Add makemigrations.py file

3.0.1

  • Add django>=1.11 to requirements
  • Improve documentation markup
  • Remove doc build support
  • Update CI config
  • Replace pinax-theme-bootstrap with pinax-templates for testing

3.0.0

  • Drop Django 1.8 and 1.10 support
  • Improve documentation

2.2.1

  • Correct LONG_DESCRIPTION app title

2.2.0

  • Add Django 2.0 compatibility testing
  • Drop Django 1.9 and Python 3.3 support
  • Move documentation into README
  • Convert CI and coverage to CircleCi and CodeCov
  • Add PyPi-compatible long description

2.1.0

  • adds context and request to the likes_widget template tag

2.0.4

  • Improve documentation

2.0.1

  • Converted documentation to Markdown format

2.0.0

  • Converted to Django class-based generic views.
  • Added URL namespace."pinax_likes"
  • Added tests.
  • Dropped support for Django 1.7
  • Added compat.py in order to remove django-user-accounts dependency.

1.2

  • like_text_off and css_class_off are passed into widget even if can_like is False.
  • PINAX_LIKES_LIKABLE_MODELS entries now take an optional extra value allowed whose value should be a callable taking user and obj and returning True or False depending on whether the user is allowed to like that particular object

1.1.1

  • Fixed regression causing error when widget displayed while unauth'd

1.1

  • Fixed urls.py deprecation warnings
  • Fixed unicode string
  • Added support for custom User models
  • Documentation updates

1.0

  • Added an admin.py

0.6

  • Added a likes\_widget\_brief to display a brief widget template (likes/\_widget\_brief.html)

0.5

  • Added a who\_likes template tag that returns a list of Like objects for given object

0.4.1

  • Made the link in the default widget template a bootstrap button

0.4

  • Fixed isinstance check to check models.Model instead of models.base.ModelBase
  • Added permission checking
  • Added rendering of HTML in the ajax response to liking
  • Got rid of all the js/css cruft; up to site owner now but ships with bootstrap/bootstrap-ajax enabled templates
  • Updated use of datetime.datetime.now to timezone.now

Backward Incompatibilities

  • Added an auth\_backend to check permissions, you can just add the likes.auth\_backends.PermCheckBackend and do nothing else, or you can implement your own backend checking the likes.can\_like permission against the object and user according to your own business logic.
  • No more likes_css, likes_js, or likes_widget_js tags.
  • PINAX_LIKES_LIKABLE_MODELS has changed from a list to a dict
  • likes_widget optional parameters have been removed and instead put into per model settings

0.3

  • Renamed likes\_css and likes\_widget to likes\_css and likes\_widget
  • Turned the JavaScript code in to a jQuery plugin, removed most of the initialization code from the individual widget templates to a external JavaScript file, and added a {% likes\_js %} tag to load this plugin.
  • Each like button gets a unique ID, so multiple like buttons can appear on a single page
  • The like form works without JavaScript.
  • Likable models need to be added to PINAX\_LIKES\_LIKABLE\_MODELS setting. This prevents users from liking anything and everything, which could potentially lead to security problems (eg. liking entries in permission tables, and thus seeing their content; liking administrative users and thus getting their username).
  • Added request objects to both object\_liked and object\_unliked signals.

Backward Incompatibilities

  • Pretty much all the template tags have been renamed and work slightly differently

0.2

  • Made it easier to get rolling with a like widget using default markup and JavaScript
  • Added returning the like counts for an object when it is liked or unliked so that the widget (either your own or using the one that ships with likes) can update via AJAX

Backward Incompatibilities

  • Removed likes\_ajax and likes\_form template tags so if you were using them and had written custom overrides in \_ajax.js and \_form.html you'll need to plan your upgrade accordingly.
  • Changed the url pattern, likes\_like\_toggle, for likes to not require the user pk, instead, the view handling the POST to this url, uses request.user.
  • Changed the ajax returned by the like\_toggle view so that it now just returns a single element: {"likes\_count": \<some-number\>}

0.1

  • Initial release

Contribute

Contributing information can be found in the Pinax community health file repo.

Code of Conduct

In order to foster a kind, inclusive, and harassment-free community, the Pinax Project has a Code of Conduct. We ask you to treat everyone as a smart human programmer that shares an interest in Python, Django, and Pinax with you.

Connect with Pinax

For updates and news regarding the Pinax Project, please follow us on Twitter @pinaxproject and check out our Pinax Project blog.

License

Copyright (c) 2012-present James Tauber and contributors under the MIT license.

More Repositories

1

pinax

a Django-based platform for rapidly developing websites
Python
2,624
star
2

django-user-accounts

User accounts for Django
Python
1,109
star
3

django-mailer

mail queuing and management for the Django web framework
Python
721
star
4

pinax-stripe-light

a payments Django app for Stripe
Python
677
star
5

pinax-blog

a blog app for Django
Python
461
star
6

pinax-badges

a badges app for Django
Python
316
star
7

symposion

a Django project for conference websites
Python
300
star
8

pinax-theme-bootstrap

A theme for Pinax based on Twitter's Bootstrap
HTML
269
star
9

pinax-referrals

a referrals app for Django
Python
208
star
10

pinax-messages

a Django app for allowing users of your site to send messages to each other
Python
197
star
11

django-forms-bootstrap

Bootstrap filter and templates for use with Django forms
Python
175
star
12

pinax-webanalytics

analytics and metrics integration for Django
Python
122
star
13

pinax-project-account

a starter project that incorporates account features from django-user-accounts
119
star
14

pinax-eventlog

An event logger
Python
108
star
15

pinax-invitations

a site invitation app for Django
Python
108
star
16

pinax-comments

a comments app for Django
Python
97
star
17

pinax-models

Provide Support for Logical Deletes on Models and in the Django Admin
Python
90
star
18

pinax-ratings

a ratings app for Django
Python
88
star
19

pinax-starter-projects

Pinax Starter Projects
Shell
79
star
20

django-flag

flagging of inapproriate/spam content
Python
73
star
21

django-waitinglist

DEPRECATED - please see https://github.com/pinax/pinax-waitinglist
Python
64
star
22

pinax-points

a points, positions and levels app for Django
Python
61
star
23

pinax-calendars

Django utilities for publishing events as a calendar
Python
61
star
24

pinax-documents

a document management app for Django
Python
59
star
25

pinax-teams

an app for Django sites that supports open, by invitation, and by application teams
Python
48
star
26

pinax-boxes

database-driven regions on webpages for Django
Python
48
star
27

pinax-starter-app

A starter app template for Pinax apps
Python
36
star
28

pinax-wiki

Easily add a wiki to your Django site
Python
32
star
29

pinax-project-symposion

a starter project demonstrating a minimal symposion instance
HTML
31
star
30

code.pinaxproject.com

the site behind code.pinaxproject.com
Python
29
star
31

pinax-forums

an extensible forums app for Django and Pinax
Python
27
star
32

pinax-project-zero

the foundation for other Pinax starter projects
25
star
33

pinax-waitinglist

a Django waiting list app
Python
25
star
34

pinax-testimonials

a testimonials app for Django
Python
24
star
35

pinax-project-teams

a starter project that has account management, profiles, teams and basic collaborative content.
18
star
36

pinax-images

an app for managing collections of images associated with a content object
Python
17
star
37

pinax-phone-confirmation

An app to provide phone confirmation via Twilio
Python
16
star
38

pinax-templates

semantic templates for pinax apps
HTML
15
star
39

pinax-project-socialauth

a starter project that supports social authentication
CSS
15
star
40

pinax-api

RESTful API adhering to the JSON:API specification
Python
13
star
41

pinax-submissions

an app for proposing and reviewing submissions
Python
12
star
42

pinax-project-blog

a blog starter project
10
star
43

pinax-project-forums

an out-of-the-box Pinax starter project implementing forums
HTML
10
star
44

pinax-images-panel

a React component for uploading and managing images with the pinax-images Django reusable app
JavaScript
9
star
45

pinax-cohorts

Create cohorts to invite to join your private beta site. Depends on pinax-waitinglist.
Python
9
star
46

pinax-pages

A light weight CMS born out of Symposion
Python
8
star
47

pinax-project-lms

A starter project to bring together components of the Pinax Learning Management System
CSS
7
star
48

pinax-cli

a tool for easily instantiating Pinax starter projects (django templates)
Python
7
star
49

django-site-access

an app to provide utilities via middleware to control access to your django site
Python
7
star
50

cloudspotting

a starter project allowing you to create collections of cloud images, view other peopleโ€™s collections, โ€œlikeโ€ a collection, etc.
CSS
7
star
51

pinax-calendars-demo

a demo project for pinax-calendars
Python
7
star
52

pinax-lms-activities

framework and base learning activities for Pinax LMS
Python
7
star
53

atom-format

Python
6
star
54

PinaxCon

PinaxCon is a project that demonstrates how Symposion can be hooked up for a conference site
HTML
6
star
55

pinax-events

a simple app for publishing events on your site
Python
5
star
56

pinax-news

a simple app for publishing links to news articles on your site
Python
5
star
57

pinax-utils

an app for Pinax utilities
Python
5
star
58

blog.pinaxproject.com

Python
5
star
59

pinax-types

Python
4
star
60

cloudspotting2

Pinax demo application showing many Pinax apps
CSS
4
star
61

pinax-project-static

3
star
62

pinax-cart

Python
3
star
63

pinax-identity

OpenID Connect with pinax-api helpers
Python
3
star
64

pinax-theme-classic

the original Pinax theme
HTML
3
star
65

pinax_theme_tester

a project based on the zero starter project used to test and build the pinax theme
Python
3
star
66

screencasts

2
star
67

pinax-project-wiki

a demo starter project for a single wiki site, featuring integration of pinax-wiki
2
star
68

lms-activities-demo

a Django site for demoing Pinax LMS Activities
Python
1
star
69

dashboard.pinaxproject.com

a collection of metrics and information about how the Pinax Project is going
Python
1
star
70

.github

Pinax Default Community Health Files
1
star
71

mytweets

a demo of pinax-types periods
Python
1
star
72

pinax-theme-pinaxproject

a Pinax theme for pinaxproject.com website
HTML
1
star
73

patch-game

guess the Pinax patch
CSS
1
star