Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #33 #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
*.pyc
dist/
build/
django_updown.egg-info/
*.egg-info/
env/
.cache/
.tox/
*backup*
17 changes: 9 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ language: python
cache: pip

python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"

sudo: false

env:
- DJANGO=1.10
- DJANGO=1.11
- DJANGO=2.0
- DJANGO=3.0
- DJANGO=3.1

matrix:
fast_finish: true

include:
- { python: "3.6", env: DJANGO=1.11 }
- { python: "3.6", env: DJANGO=2.0 }

exclude:
- { python: "2.7", env: DJANGO=2.0 }
- { python: "3.4", env: DJANGO=3.0 }
- { python: "3.4", env: DJANGO=3.1 }
- { python: "3.5", env: DJANGO=3.0 }
- { python: "3.5", env: DJANGO=3.1 }

install:
- pip install tox tox-travis
Expand Down
39 changes: 0 additions & 39 deletions CHANGELOG.md

This file was deleted.

88 changes: 65 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,86 @@
# django-updown
# django-updown-ratings

> Simple Django application for adding Youtube like up and down voting.
> Simple Django application for adding Youtube like up and down voting. \
> This [`django-updown-ratings`][1] is forked from [`django-updown`][2] to support the newest django version.

[![Build Status](https://secure.travis-ci.org/weluse/django-updown.png?branch=master)](http://travis-ci.org/weluse/django-updown)
[![build status][3]][4]
[![django version][5]][6]
[![python version][7]][8]

## Install

```
pip install django-updown
pip install django-updown-ratings
```

## Usage

Add `"updown"` to your `INSTALLED_APPS`. Then just add a `RatingField` to
your existing model:
Add `"updown"` to your `INSTALLED_APPS`. Then just add a `RatingField` to your existing model:

from django.db import models
from updown.fields import RatingField
```
from django.db import models
from updown.fields import RatingField

class Video(models.Model):
# ...other fields...
rating = RatingField()
class Post(models.Model):
# ...other fields...
rating = RatingField()
```

You can also allow the user to change his vote:

class Video(models.Model):
# ...other fields...
rating = RatingField(can_change_vote=True)
```
class Post(models.Model):
# ...other fields...
rating = RatingField(can_change_vote=True)
```

Now you can write your own view to submit ratings or use the predefined:

from updown.views import AddRatingFromModel
```
from updown.views import AddRatingFromModel

urlpatterns = [
....

urlpatterns = patterns("",
url(r"^(?P<object_id>\d+)/rate/(?P<score>[\d\-]+)$", AddRatingFromModel(), {
'app_label': 'video',
'model': 'Video',
'field_name': 'rating',
}, name="video_rating"),
)
path('<int:object_id>/rate/<str:score>', AddRatingFromModel(), {
'app_label': 'blogapp',
'model': 'Post',
'field_name': 'rating'
}, name='post_rating'),
]
```

To submit a vote just go to ``video/<id>/rate/(1|-1)``. If you allowed users to
To submit a vote just go to `post/<id>/rate/(1|-1)`. If you allowed users to
change they're vote, they can do it with the same url.

#### Template

You can directly use this properties in the template, e.g:

```
{{ post.rating.likes }}
{{ post.rating.dislikes }}
{{ post.rating.get_difference }}
{{ post.rating.get_quotient }}
```

**Properties:**

- `.likes` show total likes
- `.dislikes` show total dislikes
- `.get_difference` get diff between _likes_ and _dislikes_ (`likes - dislikes`).
- `.get_quotient` get quotient between _likes_ and _dislikes_ (`likes / max(dislikes)`).



[1]: https://github.com/agusmakmun/django-updown-ratings
[2]: https://github.com/weluse/django-updown

[3]: https://secure.travis-ci.org/agusmakmun/django-updown-ratings.png?branch=master
[4]: http://travis-ci.org/agusmakmun/django-updown-ratings

[5]: https://img.shields.io/badge/Django-2.0%20%3E=%203.1-green.svg
[6]: https://www.djangoproject.com

[7]: https://img.shields.io/pypi/pyversions/django-updown-ratings.svg
[8]: https://pypi.python.org/pypi/django-updown-ratings
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[wheel]
universal = 1
[metadata]
description-file = README.md
19 changes: 12 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ def get_version(package):


setup(
name='django-updown',
name='django-updown-ratings',
version=version,
description='Reusable Django application for youtube \
like up and down voting.',
author='Daniel Banck',
description='Reusable Django application for youtube like up and down voting.',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
author='Daniel Banck, Agus Makmun',
author_email='[email protected]',
url='http://github.com/weluse/django-updown/tree/master',
packages=find_packages(),
url='http://github.com/agusmakmun/django-updown-ratings/tree/master',
packages=find_packages(exclude=["*demo"]),
zip_safe=False,
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand All @@ -34,7 +35,11 @@ def get_version(package):
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Internet :: WWW/HTTP',
]
)
6 changes: 3 additions & 3 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ def test_basic_vote(self):
self.instance.rating.add(SCORE_TYPES['LIKE'], self.user,
'192.168.0.1')

self.assertEquals(self.instance.rating_likes, 1)
self.assertEqual(self.instance.rating_likes, 1)

def test_change_vote(self):
self.instance.rating.add(SCORE_TYPES['LIKE'], self.user,
'192.168.0.1')
self.instance.rating.add(SCORE_TYPES['DISLIKE'], self.user,
'192.168.0.1')

self.assertEquals(self.instance.rating_likes, 0)
self.assertEquals(self.instance.rating_dislikes, 1)
self.assertEqual(self.instance.rating_likes, 0)
self.assertEqual(self.instance.rating_dislikes, 1)

def test_change_vote_disallowed(self):
self.instance.rating2.add(SCORE_TYPES['LIKE'], self.user,
Expand Down
12 changes: 6 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ addopts=--tb=short

[tox]
envlist =
{py27,py34,py35}-django110,
{py27,py34,py35,py36}-django111,
{py34,py35,py36}-django20,
{py34,py35,py36,py37,py38}-django30,
{py34,py35,py36,py37,py38}-django31,

[travis:env]
DJANGO =
1.10: django110
1.11: django111
2.0: django20
3.0: django30
3.1: django31

[testenv]
commands = py.test tests/
Expand All @@ -20,8 +20,8 @@ setenv =
PYTHONDONTWRITEBYTECODE=1
PYTHONWARNINGS=once
deps =
django110: Django>=1.10,<1.11
django111: Django>=1.11,<2.0
django20: Django>=2.0,<2.1
django30: Django>=3.0,<3.1
django31: Django>=3.1,<4.1
pytest
pytest-django
2 changes: 1 addition & 1 deletion updown/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.0.2'
__version__ = '1.0.1'

# Version synonym
VERSION = __version__
4 changes: 2 additions & 2 deletions updown/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Fields needed for the updown ratings
"""
from __future__ import unicode_literals
from django.contrib.contenttypes.models import ContentType

from django.db.models import IntegerField, PositiveIntegerField
from django.conf import settings
Expand All @@ -15,7 +16,6 @@
raise ImportError("django-updown requires django.contrib.contenttypes "
"in your INSTALLED_APPS")

from django.contrib.contenttypes.models import ContentType

__all__ = ('Rating', 'RatingField', 'AnonymousRatingField')

Expand Down Expand Up @@ -50,7 +50,7 @@ def is_authenticated(self, user):
return user.is_authenticated()
return user.is_authenticated

def get_rating_for_user(self, user, ip_address=None):
def get_rating_for_user(self, user=None, ip_address=None):
kwargs = {
'content_type': self.get_content_type(),
'object_id': self.instance.pk,
Expand Down
2 changes: 1 addition & 1 deletion updown/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Vote',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.BigAutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('object_id', models.PositiveIntegerField()),
('key', models.CharField(max_length=32)),
('score', models.SmallIntegerField(choices=[(-1, 'DISLIKE'), (1, 'LIKE')])),
Expand Down
7 changes: 3 additions & 4 deletions updown/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.utils.encoding import python_2_unicode_compatible
from django.conf import settings
from django.utils import timezone

Expand All @@ -18,9 +17,9 @@
SCORE_TYPES = dict((value, key) for key, value in _SCORE_TYPE_CHOICES)


@python_2_unicode_compatible
class Vote(models.Model):
content_type = models.ForeignKey(ContentType, related_name="updown_votes",
id = models.BigAutoField(primary_key=True)
content_type = models.ForeignKey(ContentType, related_name="updown_votes",
on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
key = models.CharField(max_length=32)
Expand All @@ -31,7 +30,7 @@ class Vote(models.Model):
date_added = models.DateTimeField(default=timezone.now, editable=False)
date_changed = models.DateTimeField(default=timezone.now, editable=False)

content_object = GenericForeignKey()
content_object = GenericForeignKey('content_type', 'object_id')

class Meta:
unique_together = (('content_type', 'object_id', 'key', 'user',
Expand Down
2 changes: 2 additions & 0 deletions updown/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


class AddRatingView(object):

def __call__(self, request, content_type_id, object_id, field_name, score):
"""__call__(request, content_type_id, object_id, field_name, score)

Expand Down Expand Up @@ -90,6 +91,7 @@ def get_instance(self, content_type_id, object_id):


class AddRatingFromModel(AddRatingView):

def __call__(self, request, model, app_label, object_id, field_name,
score, **kwargs):
"""__call__(request, model, app_label, object_id, field_name, score)
Expand Down
1 change: 1 addition & 0 deletions updown_demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db.sqlite3
Empty file added updown_demo/app/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions updown_demo/app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from app.models import Post

admin.site.register(Post)
5 changes: 5 additions & 0 deletions updown_demo/app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AppConfig(AppConfig):
name = 'app'
2 changes: 2 additions & 0 deletions updown_demo/app/migrations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[^.]*
!__init__.py
Empty file.
10 changes: 10 additions & 0 deletions updown_demo/app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.db import models
from updown.fields import RatingField


class Post(models.Model):
title = models.CharField(max_length=200)
rating = RatingField(can_change_vote=True)

def __str__(self):
return self.title
Loading