mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 03:17:07 +00:00
refactor: migrate to question sets for person qs
This means we're starting to use the same system for person questions as for relationship questions
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import logging
|
||||
import typing
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.urls import reverse
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from django_countries.fields import CountryField
|
||||
@@ -18,9 +20,11 @@ logger = logging.getLogger(__name__) # pylint: disable=invalid-name
|
||||
__all__ = [
|
||||
'User',
|
||||
'Organisation',
|
||||
'Role',
|
||||
'Theme',
|
||||
'PersonQuestion',
|
||||
'PersonQuestionChoice',
|
||||
'Person',
|
||||
'PersonAnswerSet',
|
||||
]
|
||||
|
||||
|
||||
@@ -36,7 +40,7 @@ class User(AbstractUser):
|
||||
"""
|
||||
return hasattr(self, 'person')
|
||||
|
||||
def send_welcome_email(self):
|
||||
def send_welcome_email(self) -> None:
|
||||
"""Send a welcome email to a new user."""
|
||||
# Get exported data from settings.py first
|
||||
context = settings_export(None)
|
||||
@@ -71,16 +75,6 @@ class Organisation(models.Model):
|
||||
return self.name
|
||||
|
||||
|
||||
class Role(models.Model):
|
||||
"""
|
||||
Role which a :class:`Person` holds within the project.
|
||||
"""
|
||||
name = models.CharField(max_length=255, blank=False, null=False)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class Theme(models.Model):
|
||||
"""
|
||||
Project theme within which a :class:`Person` works.
|
||||
@@ -91,6 +85,79 @@ class Theme(models.Model):
|
||||
return self.name
|
||||
|
||||
|
||||
class PersonQuestion(models.Model):
|
||||
"""Question which may be asked about a person."""
|
||||
class Meta:
|
||||
ordering = [
|
||||
'order',
|
||||
'text',
|
||||
]
|
||||
|
||||
#: Version number of this question - to allow modification without invalidating existing data
|
||||
version = models.PositiveSmallIntegerField(default=1,
|
||||
blank=False, null=False)
|
||||
|
||||
#: Text of question
|
||||
text = models.CharField(max_length=255,
|
||||
blank=False, null=False)
|
||||
|
||||
#: Position of this question in the list
|
||||
order = models.SmallIntegerField(default=0,
|
||||
blank=False, null=False)
|
||||
|
||||
@property
|
||||
def choices(self) -> typing.List[typing.List[str]]:
|
||||
"""
|
||||
Convert the :class:`PersonQuestionChoice`s for this question into Django choices.
|
||||
"""
|
||||
return [
|
||||
[choice.pk, str(choice)] for choice in self.answers.all()
|
||||
]
|
||||
|
||||
@property
|
||||
def slug(self) -> str:
|
||||
return slugify(self.text)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.text
|
||||
|
||||
|
||||
class PersonQuestionChoice(models.Model):
|
||||
"""
|
||||
Allowed answer to a :class:`PersonQuestion`.
|
||||
"""
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=['question', 'text'],
|
||||
name='unique_question_answer')
|
||||
]
|
||||
ordering = [
|
||||
'question__order',
|
||||
'order',
|
||||
'text',
|
||||
]
|
||||
|
||||
#: Question to which this answer belongs
|
||||
question = models.ForeignKey(PersonQuestion, related_name='answers',
|
||||
on_delete=models.CASCADE,
|
||||
blank=False, null=False)
|
||||
|
||||
#: Text of answer
|
||||
text = models.CharField(max_length=255,
|
||||
blank=False, null=False)
|
||||
|
||||
#: Position of this answer in the list
|
||||
order = models.SmallIntegerField(default=0,
|
||||
blank=False, null=False)
|
||||
|
||||
@property
|
||||
def slug(self) -> str:
|
||||
return slugify(self.text)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.text
|
||||
|
||||
|
||||
class Person(models.Model):
|
||||
"""
|
||||
A person may be a member of the BRECcIA core team or an external stakeholder.
|
||||
@@ -168,13 +235,6 @@ class Person(models.Model):
|
||||
#: Discipline(s) within which this person works
|
||||
disciplines = models.CharField(max_length=255, blank=True, null=True)
|
||||
|
||||
#: Role this person holds within the project
|
||||
role = models.ForeignKey(Role,
|
||||
on_delete=models.PROTECT,
|
||||
related_name='holders',
|
||||
blank=True,
|
||||
null=True)
|
||||
|
||||
#: Project themes within this person works
|
||||
themes = models.ManyToManyField(Theme, related_name='people', blank=True)
|
||||
|
||||
@@ -183,8 +243,42 @@ class Person(models.Model):
|
||||
return self.relationships_as_source.all().union(
|
||||
self.relationships_as_target.all())
|
||||
|
||||
@property
|
||||
def current_answers(self) -> 'PersonAnswerSet':
|
||||
return self.answer_sets.last()
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('people:person.detail', kwargs={'pk': self.pk})
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class PersonAnswerSet(models.Model):
|
||||
"""
|
||||
The answers to the person questions at a particular point in time.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
ordering = [
|
||||
'timestamp',
|
||||
]
|
||||
|
||||
#: Person to which this answer set belongs
|
||||
person = models.ForeignKey(Person,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='answer_sets',
|
||||
blank=False, null=False)
|
||||
|
||||
#: Answers to :class:`PersonQuestion`s
|
||||
question_answers = models.ManyToManyField(PersonQuestionChoice)
|
||||
|
||||
#: When were these answers collected?
|
||||
timestamp = models.DateTimeField(auto_now_add=True,
|
||||
editable=False)
|
||||
|
||||
replaced_timestamp = models.DateTimeField(blank=True, null=True,
|
||||
editable=False)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return self.person.get_absolute_url()
|
||||
|
||||
Reference in New Issue
Block a user