finish template question API
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 2.2.24 on 2021-08-23 16:43
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('webapp', '0075_auto_20210114_2155'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='templatequestion',
|
||||
old_name='question',
|
||||
new_name='questions',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 2.2.24 on 2021-08-23 17:12
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('webapp', '0076_auto_20210823_1943'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='templatequestion',
|
||||
name='deleted',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
+3
-3
@@ -83,8 +83,7 @@ class Event(BaseFeed):
|
||||
|
||||
class TemplateQuestion(models.Model):
|
||||
"""
|
||||
NOT IMPLEMENTED!!!
|
||||
Stores template questions for signup forms as JSON format. Used in signup form creation
|
||||
Stores template questions for signup forms as JSON format. Used in signup form creation.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
@@ -92,7 +91,8 @@ class TemplateQuestion(models.Model):
|
||||
verbose_name_plural = _('Template questions')
|
||||
|
||||
name = models.CharField(max_length=255)
|
||||
question = JSONField()
|
||||
questions = JSONField()
|
||||
deleted = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
return _('Template questions: {}').format(self.name)
|
||||
|
||||
@@ -106,11 +106,11 @@ class EventSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class SavedQuestionsSerializer(serializers.ModelSerializer):
|
||||
question = serializers.JSONField()
|
||||
questions = serializers.JSONField()
|
||||
|
||||
class Meta:
|
||||
model = TemplateQuestion
|
||||
fields = ('id', 'name', 'question')
|
||||
fields = ('id', 'name', 'questions')
|
||||
|
||||
|
||||
class TagSerializer(serializers.ModelSerializer):
|
||||
|
||||
@@ -14,11 +14,11 @@ class TemplateQuestionCase(APITestCase):
|
||||
self.questions = [
|
||||
TemplateQuestion.objects.create(
|
||||
name="Testi1",
|
||||
question=ALL_QUESTION_TYPES
|
||||
questions=ALL_QUESTION_TYPES
|
||||
),
|
||||
TemplateQuestion.objects.create(
|
||||
name="Testi2",
|
||||
question=ALL_QUESTION_TYPES
|
||||
questions=ALL_QUESTION_TYPES
|
||||
)
|
||||
]
|
||||
|
||||
@@ -36,7 +36,7 @@ class TemplateQuestionCase(APITestCase):
|
||||
def test_post(self):
|
||||
new = {
|
||||
"name": "testi3",
|
||||
"question": json.dumps(ALL_QUESTION_TYPES)
|
||||
"questions": json.dumps(ALL_QUESTION_TYPES)
|
||||
}
|
||||
response = self.client.post("/api/questions/", new, format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
@@ -51,7 +51,7 @@ class TemplateQuestionCase(APITestCase):
|
||||
def test_update(self):
|
||||
new = {
|
||||
"name": "uusi testi2",
|
||||
"question": json.dumps({})
|
||||
"questions": json.dumps({})
|
||||
}
|
||||
response = self.client.put(f"/api/questions/{self.questions[0].id}/", new, format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
@@ -69,12 +69,13 @@ class TemplateQuestionCase(APITestCase):
|
||||
)
|
||||
|
||||
def test_delete(self):
|
||||
response = self.client.delete(f"/api/questions/{self.questions[0].id}/", format="json")
|
||||
id = self.questions[0].id
|
||||
response = self.client.delete(f"/api/questions/{id}/", format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
self.assertEqual(TemplateQuestion.objects.count(), 2)
|
||||
|
||||
# Authenticate
|
||||
self.client.force_authenticate(user=self.authClient)
|
||||
response = self.client.delete(f"/api/questions/{self.questions[0].id}/", format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
||||
self.assertEqual(TemplateQuestion.objects.count(), 1)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(TemplateQuestion.objects.get(id=id).deleted, True)
|
||||
|
||||
+10
-1
@@ -215,10 +215,19 @@ class SignupViewSet(ModelViewSet):
|
||||
|
||||
|
||||
class SavedQuestionsViewSet(ModelViewSet):
|
||||
queryset = TemplateQuestion.objects.all()
|
||||
queryset = TemplateQuestion.objects.filter(deleted=False)
|
||||
serializer_class = SavedQuestionsSerializer
|
||||
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||
|
||||
def destroy(self, request, pk=None, *args, **kwargs):
|
||||
try:
|
||||
question = self.get_object()
|
||||
question.deleted = True
|
||||
question.save()
|
||||
return JsonResponse(status=200, data={"message": "OK"})
|
||||
except ObjectDoesNotExist:
|
||||
return JsonResponse(status=404, data={"error": f"Template question {pk} not found"})
|
||||
|
||||
|
||||
class FeedViewSet(ModelViewSet):
|
||||
queryset = Feed.objects.filter(deleted=False)
|
||||
|
||||
Reference in New Issue
Block a user