Commit 705915a4 authored by Nicolas Joyard's avatar Nicolas Joyard
Browse files

Add PositionScore model for position score decay

parent 391b04aa
......@@ -6,9 +6,10 @@ class RepresentativePositionsTest(RepresentativeBaseTest):
"""
- One for positions
- One for related themes
- One for position scores
- One for position themes
"""
queries = RepresentativeBaseTest.queries + 2
queries = RepresentativeBaseTest.queries + 3
def test_queries(self):
self.do_query_test()
......
......@@ -19,7 +19,8 @@ class RepresentativeDetailPositions(RepresentativeDetailBase):
queryset=Position.objects.filter(published=True)
.order_by('-datetime', 'pk')
),
'positions__themes'
'positions__themes',
'positions__positionscore'
)
return qs
......
......@@ -8,7 +8,8 @@ class ThemeDetailPositions(ThemeDetailBase):
def get_queryset(self):
qs = super(ThemeDetailPositions, self).get_queryset()
qs = qs.prefetch_related('positions__representative')
qs = qs.prefetch_related('positions__representative',
'positions__positionscore')
return qs
def get_context_data(self, **kwargs):
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('representatives_positions', '0005_set_title'),
('representatives_recommendations', '0007_fix_underflow'),
]
operations = [
migrations.CreateModel(
name='PositionScore',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('score', models.FloatField(default=0)),
],
options={
'db_table': 'representatives_positions_positionscore',
'managed': False,
},
),
migrations.RunSQL(
"""
CREATE OR REPLACE VIEW "representatives_positions_positionscore"
AS SELECT
"representatives_positions_position"."id" AS "id",
"representatives_positions_position"."id" AS "position_id",
"representatives_positions_position"."representative_id" AS "representative_id",
decay_score(
"representatives_positions_position"."score",
"representatives_positions_position"."datetime",
"decay_num"."value",
"decay_denom"."value",
"exponent"."value",
"decimals"."value"
) AS "score"
FROM "representatives_positions_position"
JOIN (SELECT CAST(TO_NUMBER(value, '99999') AS NUMERIC) AS value FROM memopol_settings_setting WHERE key = 'SCORE_DECAY_NUM') decay_num ON 1=1
JOIN (SELECT CAST(TO_NUMBER(value, '99999') AS NUMERIC) AS value FROM memopol_settings_setting WHERE key = 'SCORE_DECAY_DENOM') decay_denom ON 1=1
JOIN (SELECT CAST(TO_NUMBER(value, '99999') AS NUMERIC) AS value FROM memopol_settings_setting WHERE key = 'SCORE_EXPONENT') exponent ON 1=1
JOIN (SELECT CAST(TO_NUMBER(value, '99999') AS INTEGER) AS value FROM memopol_settings_setting WHERE key = 'SCORE_DECIMALS') decimals ON 1=1;
"""
),
]
......@@ -37,3 +37,15 @@ class Position(models.Model):
def unpublish(self):
self.published = False
class PositionScore(models.Model):
position = models.OneToOneField(Position, related_name='positionscore',
on_delete=models.DO_NOTHING)
representative = models.ForeignKey(Representative,
related_name='positionscores')
score = models.FloatField(default=0)
class Meta:
managed = False
db_table = 'representatives_positions_positionscore'
......@@ -45,7 +45,7 @@
<div class="text-center">{{ position.datetime|naturalday }}</div>
{% include "blocks/_themetags.html" with themes=position.themes.all exclude=theme.pk %}
{{ position.score|score_badge }}
{{ position.positionscore.score|score_badge }}
</button>
<div class="modal fade" id="position-modal-{{ position.pk }}" tabindex="-1" role="dialog" aria-labelledby="position-button-{{ position.pk }}">
......@@ -90,7 +90,7 @@
<dt>{% trans "Score" %}</dt>
<dd>
{{ position.score|score_badge }}
{{ position.positionscore.score|score_badge }}
</dd>
</dl>
</div>
......
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