-
Notifications
You must be signed in to change notification settings - Fork 49
Swappable Vote model #85
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
base: master
Are you sure you want to change the base?
Changes from all commits
ce54810
b04b311
00549fc
c43e6bd
85e15b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ __pycache__/ | |
htmlcov | ||
.coverage | ||
.eggs/ | ||
.tox/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python | ||
import sys | ||
|
||
from django.conf import settings | ||
from django.core.management import execute_from_command_line | ||
from runtests import configure | ||
|
||
if not settings.configured: | ||
configure() | ||
|
||
|
||
def runtests_swapped(): | ||
argv = sys.argv[:1] + ["test", "test.tests.VoteTest.test_vote_up"] + sys.argv[1:] | ||
execute_from_command_line(argv) | ||
configure(VOTE_VOTE_MODEL='test.MyVote') | ||
from test.models import Comment, MyVote | ||
from vote.models import VotableManager | ||
from vote.utils import _reset_vote_model | ||
_reset_vote_model() | ||
# The VoteModel's votes manager has to be updated for the new Vote model | ||
Comment.votes = VotableManager(MyVote) | ||
argv = sys.argv[:1] + ["test"] + sys.argv[1:] | ||
execute_from_command_line(argv) | ||
|
||
|
||
if __name__ == "__main__": | ||
runtests_swapped() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[tox] | ||
envlist = py39-django{22,32,40},py39-django{22,32,40}-swapper | ||
|
||
[testenv-django{22,32,40}] | ||
deps = | ||
django22: Django==2.2 | ||
django32: Django==3.2 | ||
django40: Django==4.0 | ||
djangorestframework | ||
commands = ./runtests.py | ||
|
||
[testenv:py39-django{22,32,40}-swapper] | ||
deps = | ||
django22: Django==2.2 | ||
django32: Django==3.2 | ||
django40: Django==4.0 | ||
djangorestframework | ||
swapper: swapper==1.3.0 | ||
commands = ./runtests_swapped.py | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from django.db import models | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.contrib.contenttypes.fields import GenericForeignKey | ||
|
||
UP = 0 | ||
DOWN = 1 | ||
|
||
|
||
class VoteManager(models.Manager): | ||
|
||
def filter(self, *args, **kwargs): | ||
if 'content_object' in kwargs: | ||
content_object = kwargs.pop('content_object') | ||
content_type = ContentType.objects.get_for_model(content_object) | ||
kwargs.update({ | ||
'content_type': content_type, | ||
'object_id': content_object.pk | ||
}) | ||
|
||
return super(VoteManager, self).filter(*args, **kwargs) | ||
|
||
|
||
class AbstractVote(models.Model): | ||
ACTION_FIELD = { | ||
UP: 'num_vote_up', | ||
DOWN: 'num_vote_down' | ||
} | ||
|
||
user_id = models.BigIntegerField() | ||
content_type = models.ForeignKey( | ||
ContentType, | ||
related_name='+', | ||
on_delete=models.CASCADE | ||
) | ||
object_id = models.PositiveIntegerField() | ||
content_object = GenericForeignKey() | ||
action = models.PositiveSmallIntegerField(default=UP) | ||
create_at = models.DateTimeField(auto_now_add=True) | ||
|
||
objects = VoteManager() | ||
|
||
class Meta: | ||
abstract = True | ||
index_together = ('content_type', 'object_id') | ||
|
||
@classmethod | ||
def votes_for(cls, model, instance=None, action=UP): | ||
ct = ContentType.objects.get_for_model(model) | ||
kwargs = { | ||
"content_type": ct, | ||
"action": action | ||
} | ||
if instance is not None: | ||
kwargs["object_id"] = instance.pk | ||
|
||
return cls.objects.filter(**kwargs) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,9 @@ def __init__(self, through, model, instance, field_name='votes'): | |
self.field_name = field_name | ||
|
||
def vote(self, user_id, action): | ||
'''Returns the Vote object on success. None on failure.''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change for users who are using this API, why do you change this? |
||
try: | ||
vote = None | ||
with transaction.atomic(): | ||
self.instance = self.model.objects.select_for_update().get( | ||
pk=self.instance.pk) | ||
|
@@ -75,20 +77,22 @@ def vote(self, user_id, action): | |
setattr(self.instance, voted_field, | ||
getattr(self.instance, voted_field) - 1) | ||
except self.through.DoesNotExist: | ||
self.through.objects.create(user_id=user_id, | ||
content_type=content_type, | ||
object_id=self.instance.pk, | ||
action=action) | ||
vote = self.through.objects.create( | ||
user_id=user_id, | ||
content_type=content_type, | ||
object_id=self.instance.pk, | ||
action=action | ||
) | ||
|
||
statistics_field = self.through.ACTION_FIELD.get(action) | ||
setattr(self.instance, statistics_field, | ||
getattr(self.instance, statistics_field) + 1) | ||
|
||
self.instance.save() | ||
|
||
return True | ||
return vote | ||
except (OperationalError, IntegrityError): | ||
return False | ||
return None | ||
|
||
@instance_required | ||
def up(self, user_id): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,21 @@ | ||
from django.db import models | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.contrib.contenttypes.fields import GenericForeignKey | ||
from vote.base_models import AbstractVote | ||
from vote.managers import VotableManager | ||
try: | ||
from swapper import swappable_setting | ||
except ImportError: | ||
swappable_setting = None | ||
|
||
UP = 0 | ||
DOWN = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is also a breaking change that users' code could be broken after upgrading if they use this variable. |
||
|
||
|
||
class VoteManager(models.Manager): | ||
|
||
def filter(self, *args, **kwargs): | ||
if 'content_object' in kwargs: | ||
content_object = kwargs.pop('content_object') | ||
content_type = ContentType.objects.get_for_model(content_object) | ||
kwargs.update({ | ||
'content_type': content_type, | ||
'object_id': content_object.pk | ||
}) | ||
|
||
return super(VoteManager, self).filter(*args, **kwargs) | ||
|
||
|
||
class Vote(models.Model): | ||
ACTION_FIELD = { | ||
UP: 'num_vote_up', | ||
DOWN: 'num_vote_down' | ||
} | ||
|
||
user_id = models.BigIntegerField() | ||
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) | ||
object_id = models.PositiveIntegerField() | ||
content_object = GenericForeignKey() | ||
action = models.PositiveSmallIntegerField(default=UP) | ||
create_at = models.DateTimeField(auto_now_add=True) | ||
|
||
objects = VoteManager() | ||
class Vote(AbstractVote): | ||
|
||
class Meta: | ||
abstract = False | ||
unique_together = ('user_id', 'content_type', 'object_id', 'action') | ||
index_together = ('content_type', 'object_id') | ||
|
||
@classmethod | ||
def votes_for(cls, model, instance=None, action=UP): | ||
ct = ContentType.objects.get_for_model(model) | ||
kwargs = { | ||
"content_type": ct, | ||
"action": action | ||
} | ||
if instance is not None: | ||
kwargs["object_id"] = instance.pk | ||
|
||
return cls.objects.filter(**kwargs) | ||
swappable = None | ||
if swappable_setting: | ||
swappable = swappable_setting('vote', 'Vote') | ||
|
||
|
||
class VoteModel(models.Model): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems there are many duplications with the current Github CI setup, could we use GitHub action to do this?