Skip to content

Commit

Permalink
Merge branch 'main' into 1829-add_poll_filter_to_stats
Browse files Browse the repository at this point in the history
  • Loading branch information
GresilleSiffle committed Jan 4, 2024
2 parents c2ec2a3 + c6419a8 commit 4fee4ba
Show file tree
Hide file tree
Showing 35 changed files with 617 additions and 331 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/browser-extension-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: '18'

- name: Check extension version in the manifest
run: |
ext_version=$(python -c 'import json; print(json.load(open("src/manifest.json"))["version"])')
ext_version=$(python -c 'import json; print(json.load(open("package.json"))["version"])')
tag_exist=$(git tag -l "browser-extension-v$ext_version" | wc -l)
echo "ext_version=$ext_version" >> $GITHUB_ENV
echo "tag_exist=$tag_exist" >> $GITHUB_ENV
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ jobs:
with:
node-version: '18'

- name: Prepare extension
working-directory: browser-extension
run: |
node prepareExtension.js
- uses: cypress-io/github-action@v5
with:
working-directory: tests
Expand Down
9 changes: 7 additions & 2 deletions backend/tournesol/models/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,20 @@ def entity_has_unsafe_context(self, entity_metadata) -> tuple:

return False, None

def get_entity_contexts(self, entity_metadata) -> list:
def get_entity_contexts(self, entity_metadata, prefetched_contexts=None) -> list:
"""
Return a list of all enabled contexts matching the given entity
metadata.
"""
contexts = []

if prefetched_contexts is not None:
available_contexts = prefetched_contexts
else:
available_contexts = self.all_entity_contexts.all()

# The entity contexts are expected to be already prefetched.
for entity_context in self.all_entity_contexts.all():
for entity_context in available_contexts:
if not entity_context.enabled:
continue

Expand Down
68 changes: 53 additions & 15 deletions backend/tournesol/serializers/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from tournesol.models import Comparison, ComparisonCriteriaScore
from tournesol.serializers.entity import RelatedEntitySerializer
from tournesol.serializers.entity_context import EntityContextSerializer


class ComparisonCriteriaScoreSerializer(ModelSerializer):
Expand All @@ -22,6 +23,11 @@ def validate_criteria(self, value):


class ComparisonSerializerMixin:
def format_entity_contexts(self, poll, contexts, metadata):
return EntityContextSerializer(
poll.get_entity_contexts(metadata, contexts), many=True
).data

def reverse_criteria_scores(self, criteria_scores):
opposite_scores = criteria_scores.copy()
for index, score in enumerate(criteria_scores):
Expand All @@ -35,9 +41,7 @@ def validate_criteria_scores(self, value):
score["criteria"] for score in value
)
if missing_criterias:
raise ValidationError(
f"Missing required criteria: {','.join(missing_criterias)}"
)
raise ValidationError(f"Missing required criteria: {','.join(missing_criterias)}")
return value


Expand All @@ -53,12 +57,23 @@ class ComparisonSerializer(ComparisonSerializerMixin, ModelSerializer):

entity_a = RelatedEntitySerializer(source="entity_1")
entity_b = RelatedEntitySerializer(source="entity_2")
entity_a_contexts = EntityContextSerializer(read_only=True, many=True, default=[])
entity_b_contexts = EntityContextSerializer(read_only=True, many=True, default=[])

criteria_scores = ComparisonCriteriaScoreSerializer(many=True)
user = serializers.HiddenField(default=serializers.CurrentUserDefault())

class Meta:
model = Comparison
fields = ["user", "entity_a", "entity_b", "criteria_scores", "duration_ms"]
fields = [
"user",
"entity_a",
"entity_b",
"entity_a_contexts",
"entity_b_contexts",
"criteria_scores",
"duration_ms",
]

def to_representation(self, instance):
"""
Expand All @@ -69,8 +84,17 @@ def to_representation(self, instance):

if self.context.get("reverse", False):
ret["entity_a"], ret["entity_b"] = ret["entity_b"], ret["entity_a"]
ret["criteria_scores"] = self.reverse_criteria_scores(
ret["criteria_scores"]
ret["criteria_scores"] = self.reverse_criteria_scores(ret["criteria_scores"])

poll = self.context.get("poll")
ent_contexts = self.context.get("entity_contexts")

if poll is not None:
ret["entity_a_contexts"] = self.format_entity_contexts(
poll, ent_contexts, ret["entity_a"]["metadata"]
)
ret["entity_b_contexts"] = self.format_entity_contexts(
poll, ent_contexts, ret["entity_b"]["metadata"]
)

return ret
Expand All @@ -91,9 +115,7 @@ def create(self, validated_data):
)

for criteria_score in criteria_scores:
ComparisonCriteriaScore.objects.create(
comparison=comparison, **criteria_score
)
ComparisonCriteriaScore.objects.create(comparison=comparison, **criteria_score)

return comparison

Expand All @@ -111,10 +133,19 @@ class ComparisonUpdateSerializer(ComparisonSerializerMixin, ModelSerializer):
criteria_scores = ComparisonCriteriaScoreSerializer(many=True)
entity_a = RelatedEntitySerializer(source="entity_1", read_only=True)
entity_b = RelatedEntitySerializer(source="entity_2", read_only=True)
entity_a_contexts = EntityContextSerializer(read_only=True, many=True, default=[])
entity_b_contexts = EntityContextSerializer(read_only=True, many=True, default=[])

class Meta:
model = Comparison
fields = ["criteria_scores", "duration_ms", "entity_a", "entity_b"]
fields = [
"criteria_scores",
"duration_ms",
"entity_a",
"entity_b",
"entity_a_contexts",
"entity_b_contexts",
]

def to_representation(self, instance):
"""
Expand All @@ -128,8 +159,17 @@ def to_representation(self, instance):

if self.context.get("reverse", False):
ret["entity_a"], ret["entity_b"] = ret["entity_b"], ret["entity_a"]
ret["criteria_scores"] = self.reverse_criteria_scores(
ret["criteria_scores"]
ret["criteria_scores"] = self.reverse_criteria_scores(ret["criteria_scores"])

poll = self.context.get("poll")
ent_contexts = self.context.get("entity_contexts")

if poll is not None:
ret["entity_a_contexts"] = self.format_entity_contexts(
poll, ent_contexts, ret["entity_a"]["metadata"]
)
ret["entity_b_contexts"] = self.format_entity_contexts(
poll, ent_contexts, ret["entity_b"]["metadata"]
)

ret.move_to_end("entity_b", last=False)
Expand All @@ -144,9 +184,7 @@ def to_internal_value(self, data):
ret = super().to_internal_value(data)

if self.context.get("reverse", False):
ret["criteria_scores"] = self.reverse_criteria_scores(
ret["criteria_scores"]
)
ret["criteria_scores"] = self.reverse_criteria_scores(ret["criteria_scores"])
return ret

@transaction.atomic
Expand Down
Loading

0 comments on commit 4fee4ba

Please sign in to comment.