Skip to content

Commit

Permalink
feat: add function for duplicating and adding paper to state
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Oct 16, 2024
1 parent 697de09 commit 420c8b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
12 changes: 7 additions & 5 deletions apps/fsm/models/question_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ def correct_answer(self):
correct_choices = self.choices.all().filter(is_correct=True)

if not correct_answer_object:
correct_answer_serializer = MultiChoiceAnswerSerializer(data={
'answer_type': 'MultiChoiceAnswer',
'problem': self,
'is_correct': True,
})
correct_answer_serializer = MultiChoiceAnswerSerializer(
data={
'answer_type': 'MultiChoiceAnswer',
'problem': self,
'is_correct': True,
}
)
correct_answer_serializer.is_valid(raise_exception=True)
correct_answer_object = correct_answer_serializer.save()

Expand Down
52 changes: 31 additions & 21 deletions apps/fsm/views/fsm_state_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,9 @@ def add_paper(self, request, pk=None):
except Paper.DoesNotExist:
return Response({"error": "Invalid paper ID"}, status=status.HTTP_400_BAD_REQUEST)

try:
with transaction.atomic():
# Get the lowest order number
max_order = StatePaper.objects.filter(
state=state).aggregate(Max('order'))['order__max']
new_order = max_order + 1 if max_order is not None else 0

# Create new StatePaper
StatePaper.objects.create(
state=state, paper=paper, order=new_order)
add_paper_to_fsm_state(paper, state)

return Response({"message": "Paper added successfully"}, status=status.HTTP_201_CREATED)
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response({"message": "Paper added successfully"}, status=status.HTTP_201_CREATED)

@action(detail=True, methods=['post'])
def remove_paper(self, request, pk=None):
Expand Down Expand Up @@ -122,15 +111,36 @@ def create_and_add_paper(self, request, pk=None):
# Create new Paper
paper = Paper.objects.create()

# Get the highest order number
max_order = StatePaper.objects.filter(
state=state).aggregate(Max('order'))['order__max']
new_order = max_order + 1 if max_order is not None else 0

# Create new StatePaper
StatePaper.objects.create(
state=state, paper=paper, order=new_order)
add_paper_to_fsm_state(paper, state)

return Response(status=status.HTTP_204_NO_CONTENT)
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

@action(detail=True, methods=['post'])
def duplicate_and_add_paper(self, request, pk=None):
state = self.get_object()
paper_id = request.data.get('paper_id')
paper = get_object_or_404(Paper, id=paper_id)

cloned_paper = paper.clone()

add_paper_to_fsm_state(cloned_paper, state)

return Response(status=status.HTTP_204_NO_CONTENT)


def add_paper_to_fsm_state(paper, state):
try:
with transaction.atomic():
# Get the highest order number
max_order = StatePaper.objects.filter(
state=state).aggregate(Max('order'))['order__max']
new_order = max_order + 1 if max_order is not None else 0

# Create new StatePaper
StatePaper.objects.create(
state=state, paper=paper, order=new_order)

except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

0 comments on commit 420c8b0

Please sign in to comment.