Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
Changelog
=========

2.0.3
-----
#. Django 5.2 compatibility.
#. Removed deprecated `providing_args` parameter from signals.
#. Fixed compatibility issues with `request.headers` vs `request.META`.
#. Fixed compatibility issues with `module_name` vs `model_name`.

2.0.2
-----
#. Python 3.8, 3.9, 3.10, 3.11 support.
#. Django 3.1, 3.2, 4.0, 4.1 compatibility.

2.0.1
-----
#. Python 3.7 support.
Expand Down
17 changes: 8 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ This app utilizes `Django Secretballot <http://pypi.python.org/pypi/django-secre
Requirements
------------

#. Python 2.7, 3.5-3.7
#. Python 3.6-3.12

#. Django 1.11, 2.0, 2.1
#. Django 2.2, 3.0, 3.1, 3.2, 4.0, 4.1, 4.2, 5.0, 5.1, 5.2

#. django-secretballot 1.0.0
#. django-secretballot 2.0.0

Installation
------------
Expand All @@ -35,14 +35,14 @@ Installation

#. Add likes url include to your project's ``urls.py`` file::

url('likes/', include('likes.urls')),
path('likes/', include('likes.urls')),

#. Add ``likes.middleware.SecretBallotUserIpUseragentMiddleware`` to your ``MIDDLEWARE_CLASSES`` setting, i.e.::
#. Add ``likes.middleware.SecretBallotUserIpUseragentMiddleware`` to your ``MIDDLEWARE`` setting, i.e.::

MIDDLEWARE_CLASSES = (
MIDDLEWARE = [
...other middleware classes...
"likes.middleware.SecretBallotUserIpUseragentMiddleware",
)
]

#. Make sure ``django.template.context_processors.request`` is in your ``TEMPLATES['OPTIONS']['context_processors']`` setting.

Expand Down Expand Up @@ -82,5 +82,4 @@ To determine whether or not liking/voting should be enabled on an object, connec

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.

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.
7 changes: 6 additions & 1 deletion likes/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ def generate_token(self, request):
return request.user.username
else:
try:
s = "".join((request.META["REMOTE_ADDR"], request.headers['User-Agent']))
s = "".join(
(
request.META["REMOTE_ADDR"],
request.META.get("HTTP_USER_AGENT", ""),
)
)
return md5(s.encode("utf-8")).hexdigest()
except KeyError:
return None
8 changes: 5 additions & 3 deletions likes/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import django
import django.dispatch

likes_enabled_test = django.dispatch.Signal(providing_args=["instance", "request"])
can_vote_test = django.dispatch.Signal(providing_args=["instance", "user", "request"])
# В Django 5.2 параметр providing_args больше не поддерживается
likes_enabled_test = django.dispatch.Signal()
can_vote_test = django.dispatch.Signal()

# signal that is sent when an object is liked
object_liked = django.dispatch.Signal(providing_args=["instance", "request"])
object_liked = django.dispatch.Signal()
3 changes: 2 additions & 1 deletion likes/templatetags/likes_inclusion_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def likes(context, obj, template=None):
try:
model_name = obj._meta.model_name
except AttributeError:
model_name = obj._meta.module_name
# Fallback для очень старых версий Django
model_name = getattr(obj._meta, "module_name", obj._meta.model_name)
context.update(
{
"template": template,
Expand Down
22 changes: 18 additions & 4 deletions likes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@


def can_vote_test(request, content_type, object_id, vote):
return can_vote(content_type.get_object_for_this_type(id=object_id), request.user, request)
return can_vote(
content_type.get_object_for_this_type(id=object_id), request.user, request
)


def like(request, content_type, id, vote, template_name="likes/inclusion_tags/likes.html", can_vote_test=can_vote_test):
def like(
request,
content_type,
id,
vote,
template_name="likes/inclusion_tags/likes.html",
can_vote_test=can_vote_test,
):
# Crawlers will follow the like link if anonymous liking is enabled. They
# typically do not have referrer set.
if "HTTP_REFERER" not in request.META:
Expand Down Expand Up @@ -46,7 +55,10 @@ def like(request, content_type, id, vote, template_name="likes/inclusion_tags/li
else:
# Redirect to referer but append unique number (determined
# from global vote count) to end of URL to bypass local cache.
redirect_url = "%s?v=%s" % (request.headers['Referer'], random.randint(0, 10))
redirect_url = "%s?v=%s" % (
request.META.get("HTTP_REFERER", "/"),
random.randint(0, 10),
)
response = views.vote(
request,
content_type=content_type,
Expand All @@ -57,6 +69,8 @@ def like(request, content_type, id, vote, template_name="likes/inclusion_tags/li
)

signals.object_liked.send(
sender=content_type.model_class(), instance=content_type.get_object_for_this_type(id=id), request=request
sender=content_type.model_class(),
instance=content_type.get_object_for_this_type(id=id),
request=request,
)
return response
12 changes: 11 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = django-likes
version = 2.0.1
version = 2.0.3
description = Django app providing view interface to django-secretballot.
long_description = file: README.rst, CHANGES.rst
long_description_content_type = text/x-rst
Expand All @@ -23,11 +23,20 @@ classifiers =
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12
Framework :: Django
Framework :: Django :: 2.2
Framework :: Django :: 3.0
Framework :: Django :: 3.1
Framework :: Django :: 3.2
Framework :: Django :: 4.0
Framework :: Django :: 4.1
Framework :: Django :: 4.2
Framework :: Django :: 5.0
Framework :: Django :: 5.1
Framework :: Django :: 5.2

[options]
zip_safe = False
Expand Down Expand Up @@ -70,3 +79,4 @@ exclude =

[tool:pytest]
DJANGO_SETTINGS_MODULE = likes.tests.settings

12 changes: 11 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[tox]
distribute = False
envlist =
py{36,37,38,39}-dj{22,30,31,32}
# https://docs.djangoproject.com/en/4.1/faq/install/ python - django matrix
py36-dj{22,31,32},py37-dj{22,31,32},py38-dj{22,31,32,40,41},py39-dj{22,31,32,40,41},py310-dj{32,40,41,42},py311-dj{41,42,50,51,52},py312-dj{52}
skip_missing_interpreters = True

[travis]
Expand All @@ -10,6 +11,9 @@ python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310
3.11: py311
3.12: py312

[testenv]
usedevelop = True
Expand All @@ -21,4 +25,10 @@ deps =
dj30: Django>=3.0,<3.1
dj31: Django>=3.1,<3.2
dj32: Django>=3.2,<3.3
dj40: Django>=4.0,<4.1
dj41: Django>=4.1,<4.2
dj42: Django>=4.2,<5.0
dj50: Django>=5.0,<5.1
dj51: Django>=5.1,<5.2
dj52: Django>=5.2,<5.3
commands = pytest --cov --cov-append --cov-report=