Skip to content

Commit

Permalink
Generické označenie namiesto označenia spam
Browse files Browse the repository at this point in the history
  • Loading branch information
mireq committed Jan 19, 2019
1 parent d0567d1 commit 4b6499e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 40 deletions.
23 changes: 0 additions & 23 deletions rating/migrations/0002_auto_20190113_1345.py

This file was deleted.

28 changes: 28 additions & 0 deletions rating/migrations/0002_auto_20190119_1302.py
@@ -0,0 +1,28 @@
# Generated by Django 2.1 on 2019-01-19 12:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('rating', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='rating',
name='comment',
field=models.TextField(blank=True, default=''),
),
migrations.AddField(
model_name='rating',
name='marked_flag',
field=models.CharField(blank=True, choices=[('s', 'Spam'), ('v', 'Vulgarizmus'), ('x', 'Iné')], default='', max_length=1),
),
migrations.AddField(
model_name='statistics',
name='flag_count',
field=models.IntegerField(default=0),
),
]
40 changes: 29 additions & 11 deletions rating/models.py
Expand Up @@ -29,17 +29,17 @@ def refresh_statistics(self):
.order_by('statistics')
.annotate(computed_value=Count('marked_solution', filter=Q(marked_solution=True)))
.values('computed_value')[:1])
spam_count = Subquery(Rating.objects
flag_count = Subquery(Rating.objects
.filter(statistics=OuterRef('pk'))
.values('statistics')
.order_by('statistics')
.annotate(computed_value=Count('marked_spam', filter=Q(marked_spam=True)))
.annotate(computed_value=Count('marked_flag', filter=~Q(marked_flag='')))
.values('computed_value')[:1])
return self.update(
rating_total=Coalesce(rating_total, 0),
rating_count=Coalesce(rating_count, 0),
solution_count=Coalesce(solution_count, 0),
spam_count=Coalesce(spam_count, 0),
flag_count=Coalesce(flag_count, 0),
)


Expand Down Expand Up @@ -68,8 +68,8 @@ class Statistics(models.Model):
"Počet označení ako riešenie",
default=0
)
spam_count = models.IntegerField(
"Počet označení ako spam",
flag_count = models.IntegerField(
"Počet označení ako flag",
default=0
)

Expand All @@ -80,7 +80,7 @@ class Meta:


class RatingManager(models.Manager):
def rate(self, instance, user, value=None, marked_solution=None, marked_spam=None):
def rate(self, instance, user, value=None, marked_solution=None, marked_flag=None):
defaults = {}
if value is not None:
if value is False:
Expand All @@ -89,8 +89,8 @@ def rate(self, instance, user, value=None, marked_solution=None, marked_spam=Non
defaults['value'] = value
if marked_solution is not None:
defaults['marked_solution'] = marked_solution
if marked_spam is not None:
defaults['marked_spam'] = marked_spam
if marked_flag is not None:
defaults['marked_flag'] = marked_flag

content_type = ContentType.objects.get_for_model(instance.__class__)
object_id = instance.pk
Expand All @@ -107,6 +107,16 @@ def rate(self, instance, user, value=None, marked_solution=None, marked_spam=Non
class Rating(models.Model):
objects = RatingManager()

FLAG_NONE = ''
FLAG_SPAM = 's'
FLAG_VULGARISM = 'v'
FLAG_OTHER = 'x'
FLAG_CHOICES = (
(FLAG_SPAM, "Spam"),
(FLAG_VULGARISM, "Vulgarizmus"),
(FLAG_OTHER, "Iné"),
)

statistics = models.ForeignKey(
Statistics,
verbose_name="Hodnotený objekt",
Expand All @@ -128,9 +138,17 @@ class Rating(models.Model):
"Označené hodnotenie",
default=False
)
marked_spam = models.BooleanField(
"Označené ako spam",
default=False
marked_flag = models.CharField(
"Označené",
blank=True,
default='',
max_length=1,
choices=FLAG_CHOICES
)
comment = models.TextField(
"Komentár",
blank=True,
default='',
)

class Meta:
Expand Down
12 changes: 6 additions & 6 deletions rating/tests.py
Expand Up @@ -24,15 +24,15 @@ def setUpTestData(cls):
for i in range(1, 3)
]

def add_rating(self, value=None, marked_solution=None, marked_spam=None, user=1, article=1):
def add_rating(self, value=None, marked_solution=None, marked_flag=None, user=1, article=1):
user = self.users[user - 1]
obj = self.articles[article - 1]
Rating.objects.rate(
instance=obj,
user=user,
value=value,
marked_solution=marked_solution,
marked_spam=marked_spam
marked_flag=marked_flag
)

def test_add_rating(self):
Expand Down Expand Up @@ -83,8 +83,8 @@ def test_statistics_mark_solution(self):
self.assertEquals(Rating.objects.count(), 2)
self.assertEquals(Statistics.objects.first().solution_count, 1)

def test_statistics_mark_spam(self):
self.add_rating(marked_spam=True, user=1)
self.add_rating(marked_spam=False, user=2)
def test_statistics_mark_flag(self):
self.add_rating(marked_flag=Rating.FLAG_SPAM, user=1)
self.add_rating(marked_flag=Rating.FLAG_NONE, user=2)
self.assertEquals(Rating.objects.count(), 2)
self.assertEquals(Statistics.objects.first().spam_count, 1)
self.assertEquals(Statistics.objects.first().flag_count, 1)

0 comments on commit 4b6499e

Please sign in to comment.