Skip to content

Commit

Permalink
Add date question and answer
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Mar 19, 2019
1 parent ce225a9 commit 5e6e53e
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 209 deletions.
41 changes: 41 additions & 0 deletions caluma/form/migrations/0008_auto_20190319_1720.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-03-19 17:20
from __future__ import unicode_literals

import django.contrib.postgres.fields.jsonb
import django.core.serializers.json
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [("form", "0007_auto_20190208_1232")]

operations = [
migrations.AlterField(
model_name="answer",
name="value",
field=django.contrib.postgres.fields.jsonb.JSONField(
blank=True,
encoder=django.core.serializers.json.DjangoJSONEncoder,
null=True,
),
),
migrations.AlterField(
model_name="question",
name="type",
field=models.CharField(
choices=[
("multiple_choice", "multiple_choice"),
("integer", "integer"),
("float", "float"),
("date", "date"),
("choice", "choice"),
("textarea", "textarea"),
("text", "text"),
("table", "table"),
],
max_length=15,
),
),
]
5 changes: 4 additions & 1 deletion caluma/form/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.postgres.fields import JSONField
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django.db.models.signals import post_init
from django.dispatch import receiver
Expand Down Expand Up @@ -39,6 +40,7 @@ class Question(SlugModel):
TYPE_MULTIPLE_CHOICE = "multiple_choice"
TYPE_INTEGER = "integer"
TYPE_FLOAT = "float"
TYPE_DATE = "date"
TYPE_CHOICE = "choice"
TYPE_TEXTAREA = "textarea"
TYPE_TEXT = "text"
Expand All @@ -48,6 +50,7 @@ class Question(SlugModel):
TYPE_MULTIPLE_CHOICE,
TYPE_INTEGER,
TYPE_FLOAT,
TYPE_DATE,
TYPE_CHOICE,
TYPE_TEXTAREA,
TYPE_TEXT,
Expand Down Expand Up @@ -156,7 +159,7 @@ class Answer(UUIDModel):
question = models.ForeignKey(
"form.Question", on_delete=models.DO_NOTHING, related_name="answers"
)
value = JSONField(null=True, blank=True)
value = JSONField(null=True, blank=True, encoder=DjangoJSONEncoder)
meta = JSONField(default=dict)
document = models.ForeignKey(
Document, on_delete=models.CASCADE, related_name="answers"
Expand Down
36 changes: 36 additions & 0 deletions caluma/form/schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import date

import graphene
from graphene import relay
from graphene.types import generic
Expand Down Expand Up @@ -64,6 +66,7 @@ def resolve_type(cls, instance, info):
models.Question.TYPE_INTEGER: IntegerQuestion,
models.Question.TYPE_MULTIPLE_CHOICE: MultipleChoiceQuestion,
models.Question.TYPE_TEXTAREA: TextareaQuestion,
models.Question.TYPE_DATE: DateQuestion,
models.Question.TYPE_TABLE: TableQuestion,
}

Expand Down Expand Up @@ -115,6 +118,14 @@ class Meta:
interfaces = (Question, graphene.Node)


class DateQuestion(QuestionQuerysetMixin, DjangoObjectType):
class Meta:
model = models.Question
exclude_fields = ("type", "configuration", "options", "answers", "row_form")
use_connection = False
interfaces = (Question, graphene.Node)


class ChoiceQuestion(QuestionQuerysetMixin, DjangoObjectType):
options = DjangoFilterConnectionField(
Option, filterset_class=filters.OptionFilterSet
Expand Down Expand Up @@ -245,6 +256,12 @@ class Meta:
return_field_type = Question


class SaveDateQuestion(SaveQuestion):
class Meta:
serializer_class = serializers.SaveDateQuestionSerializer
return_field_type = Question


class SaveChoiceQuestion(SaveQuestion):
class Meta:
serializer_class = serializers.SaveChoiceQuestionSerializer
Expand Down Expand Up @@ -310,6 +327,7 @@ def resolve_type(cls, instance, info):
str: StringAnswer,
float: FloatAnswer,
int: IntegerAnswer,
date: DateAnswer,
type(None): TableAnswer,
}

Expand Down Expand Up @@ -344,6 +362,16 @@ class Meta:
interfaces = (Answer, graphene.Node)


class DateAnswer(AnswerQuerysetMixin, DjangoObjectType):
value = graphene.types.datetime.Date(required=True)

class Meta:
model = models.Answer
exclude_fields = ("document", "documents")
use_connection = False
interfaces = (Answer, graphene.Node)


class StringAnswer(AnswerQuerysetMixin, DjangoObjectType):
value = graphene.String(required=True)

Expand Down Expand Up @@ -436,6 +464,12 @@ class Meta:
return_field_type = Answer


class SaveDocumentDateAnswer(SaveDocumentAnswer):
class Meta:
serializer_class = serializers.SaveDocumentDateAnswerSerializer
return_field_type = Answer


class SaveDocumentTableAnswer(SaveDocumentAnswer):
class Meta:
serializer_class = serializers.SaveDocumentTableAnswerSerializer
Expand All @@ -456,6 +490,7 @@ class Mutation(object):
copy_question = CopyQuestion().Field()
save_text_question = SaveTextQuestion().Field()
save_textarea_question = SaveTextareaQuestion().Field()
save_date_question = SaveDateQuestion().Field()
save_choice_question = SaveChoiceQuestion().Field()
save_multiple_choice_question = SaveMultipleChoiceQuestion().Field()
save_float_question = SaveFloatQuestion().Field()
Expand All @@ -466,6 +501,7 @@ class Mutation(object):
save_document_string_answer = SaveDocumentStringAnswer().Field()
save_document_integer_answer = SaveDocumentIntegerAnswer().Field()
save_document_float_answer = SaveDocumentFloatAnswer().Field()
save_document_date_answer = SaveDocumentDateAnswer().Field()
save_document_list_answer = SaveDocumentListAnswer().Field()
save_document_table_answer = SaveDocumentTableAnswer().Field()

Expand Down
24 changes: 23 additions & 1 deletion caluma/form/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from django.db import transaction
from rest_framework import exceptions
from rest_framework.serializers import CharField, FloatField, IntegerField, ListField
from rest_framework.serializers import (
CharField,
DateField,
FloatField,
IntegerField,
ListField,
)

from . import models, validators
from ..core import serializers
Expand Down Expand Up @@ -202,6 +208,15 @@ class Meta(SaveQuestionSerializer.Meta):
fields = SaveQuestionSerializer.Meta.fields + ("max_length",)


class SaveDateQuestionSerializer(SaveQuestionSerializer):
def validate(self, data):
data["type"] = models.Question.TYPE_DATE
return super().validate(data)

class Meta(SaveQuestionSerializer.Meta):
fields = SaveQuestionSerializer.Meta.fields


class SaveQuestionOptionsMixin(object):
def create_question_options(self, question, options):
user = self.context["request"].user
Expand Down Expand Up @@ -405,6 +420,13 @@ class Meta(SaveAnswerSerializer.Meta):
pass


class SaveDocumentDateAnswerSerializer(SaveAnswerSerializer):
value = DateField()

class Meta(SaveAnswerSerializer.Meta):
pass


class SaveDocumentTableAnswerSerializer(SaveAnswerSerializer):
value = serializers.GlobalIDPrimaryKeyRelatedField(
source="documents",
Expand Down
Loading

0 comments on commit 5e6e53e

Please sign in to comment.