Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Political Memory
memopol
Commits
c85418c1
Commit
c85418c1
authored
May 15, 2016
by
Nicolas Joyard
Browse files
Add DossierScore view model, publish in API
parent
394fbec7
Changes
5
Hide whitespace changes
Inline
Side-by-side
memopol/api.py
View file @
c85418c1
...
...
@@ -14,6 +14,7 @@ from representatives_votes.api import (
)
from
representatives_recommendations.api
import
(
DossierScoreViewSet
,
RecommendationViewSet
,
RepresentativeScoreViewSet
,
ScoredVoteViewSet
...
...
@@ -24,6 +25,7 @@ router = routers.DefaultRouter()
router
.
register
(
r
'constituencies'
,
ConstituencyViewSet
)
router
.
register
(
r
'dossiers'
,
DossierViewSet
)
router
.
register
(
r
'dossier_scores'
,
DossierScoreViewSet
)
router
.
register
(
r
'groups'
,
GroupViewSet
)
router
.
register
(
r
'mandates'
,
MandateViewSet
)
router
.
register
(
r
'proposals'
,
ProposalViewSet
)
...
...
representatives_recommendations/api.py
View file @
c85418c1
...
...
@@ -6,18 +6,42 @@ from rest_framework import (
from
representatives.api
import
DefaultWebPagination
from
.models
import
(
DossierScore
,
Recommendation
,
RepresentativeScore
,
ScoredVote
)
from
.serializers
import
(
DossierScoreSerializer
,
RecommendationSerializer
,
RepresentativeScoreSerializer
,
ScoredVoteSerializer
)
class
DossierScoreViewSet
(
viewsets
.
ReadOnlyModelViewSet
):
"""
API endpoint to view representative score contribution for each dossier
"""
queryset
=
DossierScore
.
objects
.
all
()
filter_backends
=
(
filters
.
DjangoFilterBackend
,
filters
.
SearchFilter
,
filters
.
OrderingFilter
)
filter_fields
=
{
'id'
:
[
'exact'
],
'dossier'
:
[
'exact'
],
'representative'
:
[
'exact'
],
'score'
:
[
'exact'
,
'gte'
,
'lte'
]
}
search_fields
=
(
'dossier'
,
'representative'
)
ordering_fields
=
(
'representative'
,
'dossier'
)
pagination_class
=
DefaultWebPagination
serializer_class
=
DossierScoreSerializer
class
RecommendationViewSet
(
viewsets
.
ReadOnlyModelViewSet
):
"""
API endpoint that allows recommendations to be viewed.
...
...
representatives_recommendations/migrations/0002_dossierscore.py
0 → 100644
View file @
c85418c1
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'representatives_recommendations'
,
'0001_initial'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'DossierScore'
,
fields
=
[
(
'id'
,
models
.
AutoField
(
verbose_name
=
'ID'
,
serialize
=
False
,
auto_created
=
True
,
primary_key
=
True
)),
(
'score'
,
models
.
IntegerField
(
default
=
0
)),
],
options
=
{
'db_table'
:
'representatives_recommendations_dossierscores'
,
'managed'
:
False
,
},
),
migrations
.
RunSQL
(
"""
CREATE VIEW "representatives_recommendations_dossierscores"
AS SELECT
"representatives_votes_vote"."representative_id" || ':' || "representatives_votes_proposal"."dossier_id" AS "id",
"representatives_votes_vote"."representative_id",
"representatives_votes_proposal"."dossier_id",
SUM(CASE WHEN "representatives_votes_vote"."position" = ("representatives_recommendations_recommendation"."recommendation")
THEN "representatives_recommendations_recommendation"."weight"
ELSE (0 - "representatives_recommendations_recommendation"."weight")
END) AS "score"
FROM "representatives_votes_vote"
INNER JOIN "representatives_votes_proposal"
ON ( "representatives_votes_vote"."proposal_id" = "representatives_votes_proposal"."id" )
LEFT OUTER JOIN "representatives_recommendations_recommendation"
ON ( "representatives_votes_proposal"."id" = "representatives_recommendations_recommendation"."proposal_id" )
WHERE "representatives_recommendations_recommendation"."id" IS NOT NULL
GROUP BY
"representatives_votes_vote"."representative_id",
"representatives_votes_proposal"."dossier_id"
"""
),
]
representatives_recommendations/models.py
View file @
c85418c1
...
...
@@ -11,6 +11,18 @@ from representatives_votes.models import Dossier, Proposal, Vote
from
representatives.models
import
Representative
class
DossierScore
(
models
.
Model
):
id
=
models
.
CharField
(
max_length
=
255
,
primary_key
=
True
)
representative
=
models
.
ForeignKey
(
Representative
,
on_delete
=
models
.
DO_NOTHING
)
dossier
=
models
.
ForeignKey
(
Dossier
,
on_delete
=
models
.
DO_NOTHING
)
score
=
models
.
IntegerField
(
default
=
0
)
class
Meta
:
managed
=
False
db_table
=
'representatives_recommendations_dossierscores'
class
RepresentativeScore
(
models
.
Model
):
representative
=
models
.
OneToOneField
(
'representatives.representative'
,
primary_key
=
True
,
related_name
=
'score'
)
...
...
representatives_recommendations/serializers.py
View file @
c85418c1
from
rest_framework
import
serializers
from
.models
import
(
DossierScore
,
Recommendation
,
RepresentativeScore
,
ScoredVote
)
class
DossierScoreSerializer
(
serializers
.
HyperlinkedModelSerializer
):
class
Meta
:
model
=
DossierScore
fields
=
(
'representative'
,
'dossier'
,
'score'
)
class
RecommendationSerializer
(
serializers
.
HyperlinkedModelSerializer
):
class
Meta
:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment