mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 11:27:09 +00:00
refactor: allow admin config of static questions
Text and visibility set in admin panel are now respected everywhere
This commit is contained in:
@@ -61,6 +61,9 @@ class Organisation(models.Model):
|
||||
|
||||
class OrganisationAnswerSet(AnswerSet):
|
||||
"""The answers to the organisation questions at a particular point in time."""
|
||||
|
||||
question_model = OrganisationQuestion
|
||||
|
||||
#: Organisation to which this answer set belongs
|
||||
organisation = models.ForeignKey(Organisation,
|
||||
on_delete=models.CASCADE,
|
||||
|
||||
@@ -132,6 +132,8 @@ class Person(models.Model):
|
||||
|
||||
class PersonAnswerSet(AnswerSet):
|
||||
"""The answers to the person questions at a particular point in time."""
|
||||
question_model = PersonQuestion
|
||||
|
||||
#: Person to which this answer set belongs
|
||||
person = models.ForeignKey(Person,
|
||||
on_delete=models.CASCADE,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Base models for configurable questions and response sets."""
|
||||
import abc
|
||||
import typing
|
||||
|
||||
from django.db import models
|
||||
@@ -53,12 +54,16 @@ class Question(models.Model):
|
||||
blank=False,
|
||||
null=False)
|
||||
|
||||
#: Is this question hardcoded in an AnswerSet?
|
||||
is_hardcoded = models.BooleanField(
|
||||
help_text='Only the order field has any effect for a hardcoded question.',
|
||||
default=False,
|
||||
blank=False,
|
||||
null=False)
|
||||
@property
|
||||
def is_hardcoded(self) -> bool:
|
||||
return bool(self.hardcoded_field)
|
||||
|
||||
hardcoded_field = models.CharField(
|
||||
help_text='Which hardcoded field does this question represent?',
|
||||
max_length=255,
|
||||
blank=True,
|
||||
null=False
|
||||
)
|
||||
|
||||
#: Should people be able to add their own answers?
|
||||
allow_free_text = models.BooleanField(default=False,
|
||||
@@ -126,6 +131,12 @@ class AnswerSet(models.Model):
|
||||
]
|
||||
get_latest_by = 'timestamp'
|
||||
|
||||
@classmethod
|
||||
@abc.abstractproperty
|
||||
def question_model(cls) -> models.Model:
|
||||
"""Model representing questions to be answered in this AnswerSet."""
|
||||
raise NotImplementedError
|
||||
|
||||
#: Entity to which this answer set belongs
|
||||
#: This foreign key must be added to each concrete subclass
|
||||
# person = models.ForeignKey(Person,
|
||||
@@ -134,9 +145,14 @@ class AnswerSet(models.Model):
|
||||
# blank=False,
|
||||
# null=False)
|
||||
|
||||
#: Answers to :class:`Question`s
|
||||
#: This many to many relation must be added to each concrete subclass
|
||||
# question_answers = models.ManyToManyField(QuestionChoice)
|
||||
@abc.abstractproperty
|
||||
def question_answers(self) -> models.QuerySet:
|
||||
"""Answers to :class:`Question`s.
|
||||
|
||||
This many to many relation must be added to each concrete subclass
|
||||
question_answers = models.ManyToManyField(<X>QuestionChoice)
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
#: When were these answers collected?
|
||||
timestamp = models.DateTimeField(auto_now_add=True, editable=False)
|
||||
@@ -150,6 +166,31 @@ class AnswerSet(models.Model):
|
||||
def is_current(self) -> bool:
|
||||
return self.replaced_timestamp is None
|
||||
|
||||
def build_question_answers(self, show_all: bool = False) -> typing.Dict[str, str]:
|
||||
"""Collect answers to dynamic questions and join with commas."""
|
||||
questions = self.question_model.objects.all()
|
||||
if not show_all:
|
||||
questions = questions.filter(answer_is_public=True)
|
||||
|
||||
question_answers = {}
|
||||
try:
|
||||
for question in questions:
|
||||
if question.hardcoded_field:
|
||||
question_answers[question.text] = getattr(
|
||||
self, question.hardcoded_field)
|
||||
|
||||
else:
|
||||
answers = self.question_answers.filter(
|
||||
question=question)
|
||||
question_answers[question.text] = ', '.join(
|
||||
map(str, answers))
|
||||
|
||||
except AttributeError:
|
||||
# No AnswerSet yet
|
||||
pass
|
||||
|
||||
return question_answers
|
||||
|
||||
def as_dict(self, answers: typing.Optional[typing.Dict[str, typing.Any]] = None):
|
||||
"""Get the answers from this set as a dictionary for use in Form.initial."""
|
||||
if answers is None:
|
||||
|
||||
@@ -36,9 +36,7 @@ class RelationshipQuestionChoice(QuestionChoice):
|
||||
|
||||
|
||||
class Relationship(models.Model):
|
||||
"""
|
||||
A directional relationship between two people allowing linked questions.
|
||||
"""
|
||||
"""A directional relationship between two people allowing linked questions."""
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=['source', 'target'],
|
||||
@@ -95,6 +93,8 @@ class Relationship(models.Model):
|
||||
class RelationshipAnswerSet(AnswerSet):
|
||||
"""The answers to the relationship questions at a particular point in time."""
|
||||
|
||||
question_model = RelationshipQuestion
|
||||
|
||||
#: Relationship to which this answer set belongs
|
||||
relationship = models.ForeignKey(Relationship,
|
||||
on_delete=models.CASCADE,
|
||||
@@ -176,6 +176,8 @@ class OrganisationRelationship(models.Model):
|
||||
class OrganisationRelationshipAnswerSet(AnswerSet):
|
||||
"""The answers to the organisation relationship questions at a particular point in time."""
|
||||
|
||||
question_model = OrganisationRelationshipQuestion
|
||||
|
||||
#: OrganisationRelationship to which this answer set belongs
|
||||
relationship = models.ForeignKey(OrganisationRelationship,
|
||||
on_delete=models.CASCADE,
|
||||
|
||||
Reference in New Issue
Block a user