Commit f2c81e44 authored by luxcem's avatar luxcem

count votes with username instead of django user

parent 4347ba9d
import factory
from .models import Article
class ArticleFactory(factory.django.DjangoModelFactory):
class Meta:
model = Article
url = factory.Faker("url")
lang = "en"
title = factory.Faker("sentence", nb_words=4)
extracts = factory.Faker("text")
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-04-22 19:06
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rp', '0003_auto_20170422_1432'),
]
operations = [
migrations.RemoveField(
model_name='undvotes',
name='user',
),
migrations.AddField(
model_name='undvotes',
name='username',
field=models.CharField(default='', max_length=255),
preserve_default=False,
),
]
......@@ -20,6 +20,8 @@ class Article(UnDVotedMixin):
tags = TaggableManager()
# TODO: adds links to user
class Meta:
verbose_name = _("Article")
verbose_name_plural = _("Articles")
......
......@@ -3,7 +3,6 @@ from django.db.models import Sum, F
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
......@@ -11,13 +10,12 @@ class UnDVotes(models.Model):
"""
Up and down vote model
"""
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
)
#: Upvote, True for upvote, false for downvote
score = models.IntegerField(default=True)
#: username
username = models.CharField(max_length=255, null=False)
#: Score of the vote
score = models.IntegerField(default=1)
# Django generic relation
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
......@@ -44,11 +42,11 @@ class UnDVotedMixin(models.Model):
class Meta:
abstract = True
def upvote(self, user):
def upvote(self, username):
diff_score = 0
try:
# Already voted content
vote = self.und_votes.get(user=user)
vote = self.und_votes.get(username=username)
if vote.score == 1:
# Cancel previous upvote
vote.delete()
......@@ -59,18 +57,18 @@ class UnDVotedMixin(models.Model):
vote.save()
diff_score = 2
except:
vote = UnDVotes(content_object=self, user=user, score=1)
vote = UnDVotes(content_object=self, username=username, score=1)
vote.save()
diff_score = 1
self.__class__.objects.filter(id=self.id).update(
und_score=F("und_score") + diff_score)
def downvote(self, user):
def downvote(self, username):
diff_score = 0
try:
# Already voted content
vote = self.und_votes.get(user=user)
vote = self.und_votes.get(username=username)
if vote.score == -1:
# Cancel previous downvote
vote.delete()
......@@ -81,7 +79,7 @@ class UnDVotedMixin(models.Model):
vote.save()
diff_score = -2
except:
vote = UnDVotes(content_object=self, user=user, score=-1)
vote = UnDVotes(content_object=self, username=username, score=-1)
vote.save()
diff_score = -1
......
from django.test import TestCase
# Create your tests here.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment