feat(people): Make relationship questions / choices sortable

resolve #7
This commit is contained in:
James Graham
2020-02-20 14:43:48 +00:00
parent 65f46a66f1
commit e1df999108
3 changed files with 71 additions and 5 deletions

View File

@@ -16,16 +16,22 @@ class PersonAdmin(admin.ModelAdmin):
pass pass
@admin.register(models.RelationshipQuestion)
class RelationshipQuestionAdmin(admin.ModelAdmin):
pass
@admin.register(models.RelationshipQuestionChoice) @admin.register(models.RelationshipQuestionChoice)
class RelationshipQuestionChoiceAdmin(admin.ModelAdmin): class RelationshipQuestionChoiceAdmin(admin.ModelAdmin):
pass pass
class RelationshipQuestionChoiceInline(admin.TabularInline):
model = models.RelationshipQuestionChoice
@admin.register(models.RelationshipQuestion)
class RelationshipQuestionAdmin(admin.ModelAdmin):
inlines = [
RelationshipQuestionChoiceInline,
]
@admin.register(models.Relationship) @admin.register(models.Relationship)
class RelationshipAdmin(admin.ModelAdmin): class RelationshipAdmin(admin.ModelAdmin):
pass pass

View File

@@ -0,0 +1,31 @@
# Generated by Django 2.2.10 on 2020-02-20 10:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('people', '0005_user_one_person'),
]
operations = [
migrations.AlterModelOptions(
name='relationshipquestion',
options={'ordering': ['order', 'text']},
),
migrations.AlterModelOptions(
name='relationshipquestionchoice',
options={'ordering': ['order', 'text']},
),
migrations.AddField(
model_name='relationshipquestion',
name='order',
field=models.SmallIntegerField(default=0),
),
migrations.AddField(
model_name='relationshipquestionchoice',
name='order',
field=models.SmallIntegerField(default=0),
),
]

View File

@@ -1,3 +1,5 @@
import typing
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.db import models from django.db import models
@@ -54,6 +56,12 @@ class RelationshipQuestion(models.Model):
""" """
Question which may be asked about a relationship. Question which may be asked about a relationship.
""" """
class Meta:
ordering = [
'order',
'text',
]
#: Version number of this question - to allow modification without invalidating existing data #: Version number of this question - to allow modification without invalidating existing data
version = models.PositiveSmallIntegerField(default=1, version = models.PositiveSmallIntegerField(default=1,
blank=False, null=False) blank=False, null=False)
@@ -62,6 +70,19 @@ class RelationshipQuestion(models.Model):
text = models.CharField(max_length=1023, text = models.CharField(max_length=1023,
blank=False, null=False) 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:`RelationshipQuestionChoices` for this question into Django choices.
"""
return [
[choice.pk, str(choice)] for choice in self.answers.all()
]
def __str__(self) -> str: def __str__(self) -> str:
return self.text return self.text
@@ -75,6 +96,10 @@ class RelationshipQuestionChoice(models.Model):
models.UniqueConstraint(fields=['question', 'text'], models.UniqueConstraint(fields=['question', 'text'],
name='unique_question_answer') name='unique_question_answer')
] ]
ordering = [
'order',
'text',
]
#: Question to which this answer belongs #: Question to which this answer belongs
question = models.ForeignKey(RelationshipQuestion, related_name='answers', question = models.ForeignKey(RelationshipQuestion, related_name='answers',
@@ -85,6 +110,10 @@ class RelationshipQuestionChoice(models.Model):
text = models.CharField(max_length=1023, text = models.CharField(max_length=1023,
blank=False, null=False) blank=False, null=False)
#: Position of this answer in the list
order = models.SmallIntegerField(default=0,
blank=False, null=False)
def __str__(self) -> str: def __str__(self) -> str:
return self.text return self.text