-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from HE-Arc/mbu-19-question-view
Mbu 19 question view
- Loading branch information
Showing
10 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 5.0.2 on 2024-03-14 16:38 | ||
|
||
from django.db import migrations | ||
|
||
def add_community_category(apps, schema_editor): | ||
category_model = apps.get_model('masteriqapp', 'Category') | ||
category_model.objects.create(name="Community", image_path='data\\images\\community.jpg') | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('masteriqapp', '0004_auto_20240308_0923'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(add_community_category) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
api/masteriqapp/serializers/QuestionAndOptionsSerializer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from rest_framework import serializers | ||
|
||
from django.apps import apps | ||
|
||
|
||
class QuestionAndOptionsSerializer(serializers.ModelSerializer): | ||
options = serializers.StringRelatedField(many=True, read_only=True) | ||
category = serializers.SlugRelatedField(many=False, read_only=True, slug_field='name') | ||
|
||
class Meta: | ||
model = apps.get_app_config("masteriqapp").get_model("Question") | ||
fields = ['id', 'text', 'category', 'options'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from rest_framework import serializers | ||
|
||
from django.apps import apps | ||
|
||
|
||
class QuestionSerializer(serializers.ModelSerializer): | ||
category = serializers.SlugRelatedField(many=False, read_only=True, slug_field='name') | ||
class Meta: | ||
model = apps.get_app_config("masteriqapp").get_model("Question") | ||
fields = ['id', 'text', 'category'] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import io | ||
import random | ||
|
||
from rest_framework import viewsets | ||
from rest_framework.decorators import action | ||
from django.apps import apps | ||
from rest_framework import status | ||
from rest_framework.generics import get_object_or_404 | ||
from rest_framework.parsers import JSONParser | ||
from rest_framework.renderers import JSONRenderer | ||
from rest_framework.response import Response | ||
|
||
from masteriqapp.serializers.QuestionAndOptionsSerializer import QuestionAndOptionsSerializer | ||
from masteriqapp.serializers.QuestionSerializer import QuestionSerializer | ||
|
||
masteriq = apps.get_app_config("masteriqapp") | ||
|
||
class QuestionView(viewsets.ViewSet): | ||
category_model = masteriq.get_model("Category") | ||
question_model = masteriq.get_model("Question") | ||
queryset = category_model.objects.all() | ||
|
||
@action(detail=True, methods=["GET"]) | ||
def new(self, request, pk): | ||
category = get_object_or_404(self.queryset, pk=pk) | ||
if 'question' in request.session: | ||
actual_question = self.question_model.objects.get(pk=request.session['question']) | ||
if actual_question.category == category: | ||
serializer = QuestionSerializer(actual_question) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
questions = self.question_model.objects.filter(category=category) | ||
new_question = random.choice(questions) | ||
request.session['question'] = new_question.id | ||
print(request.session['question']) | ||
serializer = QuestionSerializer(new_question) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
@action(detail=False, methods=["POST"]) | ||
def new_community(self, request): | ||
datas = request.data | ||
if not ('question' in datas and 'answer' in datas and 'options' in datas): | ||
print("bad 1") | ||
return Response(status=status.HTTP_400_BAD_REQUEST) | ||
if len(datas['options']) < 1: | ||
print("bad 2") | ||
return Response(status=status.HTTP_400_BAD_REQUEST) | ||
|
||
category = self.category_model.objects.get(name="Community") | ||
question = self.question_model.objects.create(text=datas['question'], category=category) | ||
options = [] | ||
for option in datas['options']: | ||
options.append(masteriq.get_model('Option').objects.create(text=option, is_correct=False, question=question)) | ||
options.append(masteriq.get_model('Option').objects.create(text=datas['answer'], is_correct=True, question=question)) | ||
|
||
question_serializer = QuestionAndOptionsSerializer(question) | ||
|
||
return Response(question_serializer.data, status=status.HTTP_201_CREATED) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .IQView import IQView | ||
from .QuestionView import QuestionView |