Commit d8d8adfd authored by GuiΩ's avatar GuiΩ
Browse files

fix(QuestionList): correct order from position

parent cd9ef07c
......@@ -172,9 +172,9 @@ class Theme(models.Model):
next_position = next_question.order_by('questionorder__position').first().questionorder_set.get(theme__id=self.id).position
else:
if before:
next_position = position - 200
next_position = position - 512
else:
next_position = position + 200
next_position = position + 512
return (position + next_position)/2
......
import os
from django.db.models import Q
from django.db.models import Q, Min
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import CharField, ModelSerializer, ValidationError
......@@ -78,6 +78,8 @@ class QuestionSerializer(ModelSerializer):
questionOrders = validated_data.pop('questionorder_set')
question = Question.objects.create(**validated_data)
for questionOrder in questionOrders:
if not questionOrder['position']:
questionOrder['position'] = QuestionOrder.objects.filter(theme=questionOrder['theme']).aggregate(Min('position'))['position__min'] - 256
QuestionOrder.objects.create(question=question, **questionOrder)
return question
......@@ -90,6 +92,8 @@ class QuestionSerializer(ModelSerializer):
instance.save()
QuestionOrder.objects.filter(question=instance.id).delete()
for qo in questionOrders:
if not qo['position']:
qo['position'] = QuestionOrder.objects.filter(theme=qo['theme']).aggregate(Min('position'))['position__min'] - 256
QuestionOrder.objects.create(question=instance, theme=qo['theme'], position=qo['position']).save()
return instance
......
......@@ -355,7 +355,7 @@ class ThemeViewSet(ModelViewSet):
@action(methods=['get'], detail=True)
def question_list(self, request, pk):
questions = Question.objects.filter(themes__id=pk)
questions = Question.objects.filter(themes__id=pk).order_by('questionorder__position')
return Response(questions.values_list('id', flat=True))\
@action(methods=['get'], detail=True)
......
Supports Markdown
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