Skip to content

Commit

Permalink
[#1524] Made contact form subjects orerable
Browse files Browse the repository at this point in the history
  • Loading branch information
vaszig committed Jun 21, 2023
1 parent 8c950d2 commit efa337d
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/open_inwoner/openklant/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from ordered_model.admin import OrderedInlineModelAdminMixin, OrderedTabularInline
from solo.admin import SingletonModelAdmin

from .models import ContactFormSubject, OpenKlantConfig


class ContactFormSubjectInlineAdmin(admin.TabularInline):
class ContactFormSubjectInlineAdmin(OrderedTabularInline):
model = ContactFormSubject
fields = [
"subject",
]
fields = ("subject", "order", "move_up_down_links")
readonly_fields = ("order", "move_up_down_links")
ordering = ("order",)
extra = 0


Expand All @@ -33,7 +34,7 @@ def clean(self, *args, **kwargs):


@admin.register(OpenKlantConfig)
class OpenKlantConfigAdmin(SingletonModelAdmin):
class OpenKlantConfigAdmin(OrderedInlineModelAdminMixin, SingletonModelAdmin):
form = OpenKlantConfigAdminForm
inlines = [
ContactFormSubjectInlineAdmin,
Expand Down
4 changes: 1 addition & 3 deletions src/open_inwoner/openklant/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def __init__(self, user, *args, **kwargs):
self.user = user

config = OpenKlantConfig.get_solo()
self.fields["subject"].queryset = config.contactformsubject_set.order_by(
"subject"
)
self.fields["subject"].queryset = config.contactformsubject_set.all()

if self.user.is_authenticated:
del self.fields["first_name"]
Expand Down
27 changes: 27 additions & 0 deletions src/open_inwoner/openklant/migrations/0004_auto_20230621_1142.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.15 on 2023-06-21 09:42

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("openklant", "0003_auto_20230526_1013"),
]

operations = [
migrations.AlterModelOptions(
name="contactformsubject",
options={
"ordering": ("order",),
"verbose_name": "Contact formulier onderwerp",
"verbose_name_plural": "Contact formulier onderwerpen",
},
),
migrations.AddField(
model_name="contactformsubject",
name="order",
field=models.PositiveIntegerField(
db_index=True, editable=False, null=True, verbose_name="order"
),
),
]
22 changes: 22 additions & 0 deletions src/open_inwoner/openklant/migrations/0005_auto_20230621_1143.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.15 on 2023-06-21 09:43

from django.db import migrations


def populate_order_of_subjects(apps, schema_editor):
Subject = apps.get_model("openklant", "ContactFormSubject")
for subject in Subject.objects.all():
subject.order = subject.pk
subject.save()


class Migration(migrations.Migration):
dependencies = [
("openklant", "0004_auto_20230621_1142"),
]

operations = [
migrations.RunPython(
populate_order_of_subjects, reverse_code=migrations.RunPython.noop
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.15 on 2023-06-21 09:44

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("openklant", "0005_auto_20230621_1143"),
]

operations = [
migrations.AlterField(
model_name="contactformsubject",
name="order",
field=models.PositiveIntegerField(
db_index=True, editable=False, verbose_name="order"
),
),
]
11 changes: 8 additions & 3 deletions src/open_inwoner/openklant/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from ordered_model.models import OrderedModel, OrderedModelManager
from solo.models import SingletonModel
from zgw_consumers.constants import APITypes

Expand Down Expand Up @@ -86,7 +87,7 @@ def has_api_configuration(self):
return all(getattr(self, f, "") for f in self.register_api_required_fields)


class ContactFormSubject(models.Model):
class ContactFormSubject(OrderedModel):
subject = models.CharField(
verbose_name=_("Onderwerp"),
max_length=255,
Expand All @@ -97,10 +98,14 @@ class ContactFormSubject(models.Model):
on_delete=models.CASCADE,
)

class Meta:
order_with_respect_to = "config"

objects = OrderedModelManager()

class Meta(OrderedModel.Meta):
verbose_name = _("Contact formulier onderwerp")
verbose_name_plural = _("Contact formulier onderwerpen")
ordering = ("subject",)
ordering = ("order",)

def __str__(self):
return self.subject
36 changes: 36 additions & 0 deletions src/open_inwoner/openklant/tests/test_contactform.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,42 @@ def test_regular_auth_form_fills_email_and_phonenumber(self, m):

response = form.submit(status=302)

def test_expected_ordered_subjects_are_shown(self, m):
config = OpenKlantConfig.get_solo()
config.register_email = "example@example.com"
config.save()
subject_1 = ContactFormSubjectFactory(config=config)
subject_2 = ContactFormSubjectFactory(config=config)

response = self.app.get(self.url)
form = response.forms["contactmoment-form"]
sub_options = form["subject"].options

self.assertEqual(
sub_options,
[
("", True, "---------"),
(str(subject_1.pk), False, subject_1.subject),
(str(subject_2.pk), False, subject_2.subject),
],
)

# swap positions and test the updated order
subject_1.swap(subject_2)

response = self.app.get(self.url)
form = response.forms["contactmoment-form"]
sub_options = form["subject"].options

self.assertEqual(
sub_options,
[
("", True, "---------"),
(str(subject_2.pk), False, subject_2.subject),
(str(subject_1.pk), False, subject_1.subject),
],
)

def test_submit_and_register_via_email(self, m):
config = OpenKlantConfig.get_solo()
config.register_email = "example@example.com"
Expand Down

0 comments on commit efa337d

Please sign in to comment.