Giter Site home page Giter Site logo

django-likes's Introduction

Django Likes

Django app providing view interface to django-secretballot.

image

image

image

This app utilizes Django Secretballot to provide Facebook or Google+1 style item liking of Django model objects. Authenticated or anonymous users are allowed to like any given object only once.

Contents

Requirements

  1. Python 2.7, 3.5-3.7
  2. Django 1.11, 2.0, 2.1
  3. django-secretballot 1.0.0

Installation

  1. Install or add django-likes to your Python path.
  2. Configure django-secretballot as described here.
  3. Add likes to your INSTALLED_APPS setting.
  4. Add likes url include to your project's urls.py file:

    url('likes/', include('likes.urls')),
  5. Add likes.middleware.SecretBallotUserIpUseragentMiddleware to your MIDDLEWARE_CLASSES setting, i.e.:

    MIDDLEWARE_CLASSES = (
        ...other middleware classes...
        "likes.middleware.SecretBallotUserIpUseragentMiddleware",
    )
  6. Make sure django.template.context_processors.request is in your TEMPLATES['OPTIONS']['context_processors'] setting.

Usage

Template Tags

{% likes object %} +++++++++++++++++ django-likes provides an inclusion tag called likes which renders a like button for any given object, displaying the number of likes and allowing users to like the object. The tag accepts as first argument the object for which to display and on which to apply likes, i.e.:

{% load likes_inclusion_tags %}

...some html...

{% likes object %}

...some more html...

object here is any Django model object for which django-secretballot voting has been enabled. In the background the like is uniquely addressed to the object using its content type and object id.

Note

In order for the likes tag to work the request object needs to be available within the template's context. Thus you have to use RequestContext in your views to construct context, which, combined with the django.core.context_processors.request context processor, will ensure the request object is available as part of the context.

The template tag supports AJAX style liking. To enable it you need ensure django-likes' static media is accessible, see managing static files. You also need to load jQuery somewhere in your template, e.g.:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>

Signals

likes.signals.likes_enabled_test

To determine whether or not liking/voting should be enabled on an object, connect a signal handler to the likes.signals.likes_enabled_test signal, raising a likes.exceptions.LikesNotEnabledException if liking should be disabled. The default behaviour is that liking is enabled for all secretballot enabled objects.

likes.signals.can_vote_test

To determine whether or not the current requesting user can vote, connect a signal handler to the likes.signals.can_vote_test signal, raising a likes.exceptions.CannotVoteException if the current user should not be allowed to vote (the handler receives a request object). The default behaviour is that all users can vote except if they have previously voted on the object in question.

django-likes's People

Contributors

bashu avatar cblignaut avatar core2duo avatar fireinthehole avatar hedleyroos avatar nschlemm avatar petrdlouhy avatar pivolan avatar rizziepit avatar smn avatar zniper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-likes's Issues

like button not render

i put this
{% like object %}

it doesn't show like button
but instead it shows
{% like object %}

Custom user model

Hello :)
I made a custom user model named Customer
And I would like to hook the likes app up to my Customer model in stead of the User model.
How is that possible?
Thank You :)
Michael

HOW?

How do you perform this?

In order for the likes tag to work the request object needs to be available within the template's context. Thus you have to use RequestContext in your views to construct context, which, combined with the django.core.context_processors.request context processor, will ensure the request object is available as part of the context.

can you give me an example? Thanks

didn't work

hello
why i cant install this
i did all of steps
i got this error:
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class secretballot.models.Vote doesn't declare an explicit a
pp_label and isn't in an application in INSTALLED_APPS.

views.py method assumes HTTP_REFERER presence

In views.py line 14 the code assumes there that request.META['HTTP_REFERER'] exists. This is not always the case - specifically the Google Bot has started following the 'like" links and this fills the error log with lots of entries.

If referer is not present the method should return an empty response or 404, not a 502. We don't want bots to potentially vote on objects.

object() takes no parameters

when I am adding likes.middleware.SecretBallotUserIpUseragentMiddleware to my MIDDLEWARE it shows
object() takes no parameters
but when I renamed MIDDLEWARE_CLASSES TO MIDDLEWARE it works fine. But django 1.10 allows MIDDLEWARE only

Django 1.6 compatibility

from django.conf.urls.defaults import patterns, url

the urls.defaults has been deprecated in django 1.6...

This project needs a maintainer.

Hi,

This repository seems to lack maintenance, I propose myself as a new maintainer.

Could you give me the right to push in your repository or just transfer it on my account in github?

Thank you

This project needs a maintainer

Hi,

This repository seems to lack maintenance, I propose myself as a new maintainer.

Could you give me the right to push in your repository or just transfer it on my account in github?

Thank you!

Moving from jQuery to HTMX

I started to use HTMX in my Django site.
HTMX is guite often used with lazy loading with paginated querysets.
This leads to fact that the first page has the Jquery, and as the lazy loaded objects are insterted into DOM the event listeners are not added. (Or atleast I could get them working)

The solution for me was to make django-likes to work with HTMX. Changes are quite small and remove totally depency to JQuery - which I suppose is good. :-)

I tried to keep the changes at minimum and I am sure that with further work the implementation could be nicer.

as example the button.html is changed to:

<div 
hx-post="{% url 'add_or_remove_hx' target_object_id target_model %}"
hx-target="this"
hx-swap="outerHTML"

data-toggle="tooltip" title="{{like_names}}" class="like float-end m-2 lead" model="{{ target_model }}" id="target_{{ target_object_id }}" style="color: red;">
    	<i class="like-{{ target_object_id }}  {% if not undo %} far {% else %} fas {% endif %}fa-heart"></i>
  		<span class="like-count-{{ target_object_id }}"> {{ like_count }}</span>
</div>

and add_or_remove in views is changed to

@login_required
def add_or_remove_hx(request, target_object_id, target_model):

    user = request.user

    try:
        app_model = target_model
        obj_id = target_object_id
    except (KeyError, ValueError):
        return HttpResponseBadRequest()

    like = Like.objects.get_like(user, obj_id, model=app_model)

    undo = False
    if like is None:
        Like.objects.create(user, obj_id, app_model)
        status = 'added'
        undo = True
    else:
        like.delete()
        status = 'deleted'
        undo = False

    likeCount=Like.objects.for_object(obj_id, app_model).count()


    context ={
        "target_model" : app_model,
        "target_object_id": obj_id,
        "like_count": likeCount,
        "undo": undo
    }


    return render(request, "likes/button.html", context)

And likes.js is removed.
And htmx needs to be included in the base.html (or on page-by-page basis)

<script src="https://unpkg.com/[email protected]"></script>

rest is untouched! :-)

Quite small changes and JQuery is gone!

Should I make a PR on this?

AttributeError: 'Currency' object has no attribute 'id'

Traceback (most recent call last):
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/django/template/library.py", line 214, in render
    _dict = self.func(*resolved_args, **resolved_kwargs)
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/likes/templatetags/likes_inclusion_tags.py", line 27, in likes
    'can_vote': can_vote(obj, request.user, request),
  File "/Users/bashu/.virtualenvs/django-secretballot/lib/python3.8/site-packages/likes/utils.py", line 38, in can_vote
    object_id=obj.id,
AttributeError: 'Currency' object has no attribute 'id'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.