Skip to content

Commit

Permalink
[#2969] Create questions with OpenKlant2
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Schilling authored and pi-sigma committed Jan 29, 2025
1 parent a5e4938 commit 2c036ae
Show file tree
Hide file tree
Showing 13 changed files with 387 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/open_inwoner/cms/footer/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
class FooterPagesPlugin(CMSPluginBase):
name = _("Pages List")
render_template = "cms/footer/footer_pages_plugin.html"

# cache = False

def render(self, context, instance, placeholder):
# TODO move options to plugin model
config = SiteConfiguration.get_solo()
klant_config = KlantenSysteemConfig.get_solo()
context["flatpages"] = config.get_ordered_flatpages
context["has_form_configuration"] = klant_config.has_contactform_configuration
context["has_form_configuration"] = klant_config.has_contactform_configuration(
user=context["user"]
)
return context
11 changes: 7 additions & 4 deletions src/open_inwoner/cms/footer/tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import TestCase
from django.utils.translation import gettext as _

from open_inwoner.accounts.tests.factories import UserFactory
from open_inwoner.cms.footer.cms_plugins import FooterPagesPlugin
from open_inwoner.cms.tests import cms_tools
from open_inwoner.openklant.constants import KlantenServiceType
Expand All @@ -12,20 +13,22 @@
class ContactFormTestCase(ClearCachesMixin, TestCase):
def setUp(self):
super().setUp()

self.user = UserFactory()

# clear esuite_config
esuite_config = ESuiteKlantConfig.get_solo()
esuite_config.klanten_service = None
esuite_config.contactmomenten_service = None
esuite_config.register_bronorganisatie_rsin = ""
esuite_config.register_type = ""
esuite_config.register_employee_id = ""
esuite_config.send_email_confirmation = True
esuite_config.save()

def test_no_form_link_shown_in_footer_if_not_has_configuration(self):
# set nothing
klant_config = KlantenSysteemConfig.get_solo()
self.assertFalse(klant_config.has_contactform_configuration)
self.assertFalse(klant_config.has_contactform_configuration(user=self.user))

html, context = cms_tools.render_plugin(FooterPagesPlugin)

Expand All @@ -36,15 +39,15 @@ def test_form_link_is_shown_in_footer_when_has_configuration(self):
klant_config.primary_backend = KlantenServiceType.ESUITE.value
klant_config.save()

self.assertFalse(klant_config.has_contactform_configuration)
self.assertFalse(klant_config.has_contactform_configuration(user=self.user))

esuite_config = ESuiteKlantConfig.get_solo()
ContactFormSubjectFactory(config=esuite_config)

klant_config.register_contact_email = "example@example.com"
klant_config.save()

self.assertTrue(klant_config.has_contactform_configuration)
self.assertTrue(klant_config.has_contactform_configuration(user=self.user))

html, context = cms_tools.render_plugin(FooterPagesPlugin)

Expand Down
23 changes: 17 additions & 6 deletions src/open_inwoner/openklant/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,35 @@
class ContactFormSubjectForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["subject_code"].widget.attrs["placeholder"] = _(
"Must be configured if E-suite is used"
)
if esuite_subject_code := self.fields.get("esuite_subject_code", None):
esuite_subject_code.widget.attrs["placeholder"] = _(
"Must be configured if E-suite is used"
)

class Meta:
model = ContactFormSubject
fields = (
"subject",
"subject_code",
"esuite_subject_code",
)


class ContactFormSubjectInlineAdmin(OrderedTabularInline):
model = ContactFormSubject
form = ContactFormSubjectForm
fields = ("subject", "subject_code", "order", "move_up_down_links")
readonly_fields = ("order", "move_up_down_links")
ordering = ("order",)
extra = 0


class ContactFormSubjectInlineAdminESuite(ContactFormSubjectInlineAdmin):
fields = ("subject", "esuite_subject_code", "order", "move_up_down_links")


class ContactFormSubjectInlineAdminOpenKlant(ContactFormSubjectInlineAdmin):
fields = ("subject", "order", "move_up_down_links")


class ESuiteKlantConfigAdminForm(forms.ModelForm):
class Meta:
model = ESuiteKlantConfig
Expand All @@ -48,7 +56,7 @@ class Meta:
class ESuiteKlantConfigAdmin(OrderedInlineModelAdminMixin, SingletonModelAdmin):
form = ESuiteKlantConfigAdminForm
inlines = [
ContactFormSubjectInlineAdmin,
ContactFormSubjectInlineAdminESuite,
]
fieldsets = [
(
Expand Down Expand Up @@ -110,6 +118,9 @@ class Meta:
class OpenKlant2ConfigAdmin(SingletonModelAdmin):
model = OpenKlant2Config
form = OpenKlant2ConfigAdminForm
inlines = [
ContactFormSubjectInlineAdminOpenKlant,
]
fieldsets = [
(
_("API configuration"),
Expand Down
6 changes: 2 additions & 4 deletions src/open_inwoner/openklant/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.utils.translation import gettext_lazy as _

from open_inwoner.accounts.models import User
from open_inwoner.openklant.models import ContactFormSubject, ESuiteKlantConfig
from open_inwoner.openklant.models import ContactFormSubject
from open_inwoner.openklant.views.utils import generate_question_answer_pair
from open_inwoner.utils.validators import DutchPhoneNumberValidator

Expand Down Expand Up @@ -57,9 +57,7 @@ def __init__(self, user, request_session, *args, **kwargs):
self.user = user
self.request_session = request_session

# TODO: check for primary_backend? What in case of OK2?
config = ESuiteKlantConfig.get_solo()
self.fields["subject"].queryset = config.contactformsubject_set.all()
self.fields["subject"].queryset = ContactFormSubject.objects.all()

if self.user.is_authenticated:
del self.fields["first_name"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by Django 4.2.18 on 2025-01-29 09:48

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("openklant", "0023_remove_esuiteklantconfig_register_contact_moment_and_more"),
]

operations = [
migrations.RenameField(
model_name="contactformsubject",
old_name="subject_code",
new_name="esuite_subject_code",
),
migrations.RemoveField(
model_name="contactformsubject",
name="config",
),
migrations.AddField(
model_name="contactformsubject",
name="esuite_config",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="openklant.esuiteklantconfig",
),
),
migrations.AlterField(
model_name="contactformsubject",
name="esuite_subject_code",
field=models.CharField(
blank=True,
max_length=255,
null=True,
verbose_name="e-Suite 'onderwerp' code",
),
),
migrations.AddField(
model_name="contactformsubject",
name="openklant_config",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="openklant.openklant2config",
),
),
]
36 changes: 24 additions & 12 deletions src/open_inwoner/openklant/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from solo.models import SingletonModel
from zgw_consumers.constants import APITypes

from open_inwoner.accounts.models import User
from open_inwoner.utils.validators import validate_array_contents_non_empty

from .constants import KlantenServiceType
Expand Down Expand Up @@ -117,17 +118,24 @@ class ContactFormSubject(OrderedModel):
verbose_name=_("Onderwerp"),
max_length=255,
)
subject_code = models.CharField(
esuite_subject_code = models.CharField(
verbose_name=_("e-Suite 'onderwerp' code"),
max_length=255,
null=True,
blank=True,
)
config = models.ForeignKey(
esuite_config = models.ForeignKey(
"ESuiteKlantConfig",
null=True,
on_delete=models.CASCADE,
)
openklant_config = models.ForeignKey(
"OpenKlant2Config",
null=True,
on_delete=models.CASCADE,
)

order_with_respect_to = "config"
order_with_respect_to = "esuite_config"

objects = OrderedModelManager()

Expand Down Expand Up @@ -213,6 +221,11 @@ class OpenKlant2Config(SingletonModel):
),
)

register_api_required_fields = ("service",)

def has_api_configuration(self):
return all(getattr(self, f, "") for f in self.register_api_required_fields)

class Meta:
verbose_name = _("OpenKlant2 configuration")

Expand Down Expand Up @@ -285,24 +298,23 @@ def __str__(self):
@property
def has_api_configuration(self) -> bool:
if self.primary_backend == KlantenServiceType.ESUITE.value:
esuite_config = ESuiteKlantConfig.get_solo()
return esuite_config.has_api_configuration()

# TODO: support `has_api_configuration` check for OK2?
config = ESuiteKlantConfig.get_solo()
return config.has_api_configuration()
if self.primary_backend == KlantenServiceType.OPENKLANT2.value:
config = OpenKlant2Config.get_solo()
return config.has_api_configuration()
return False

@property
def contact_registration_enabled(self) -> bool:
return bool(self.register_contact_email or self.has_api_configuration)

@property
def has_contactform_configuration(self) -> bool:
def has_contactform_configuration(self, user: User) -> bool:
if not self.contact_registration_enabled:
return False

if self.primary_backend == KlantenServiceType.ESUITE.value:
esuite_config = ESuiteKlantConfig.get_solo()
return esuite_config.contactformsubject_set.exists()

# TODO: check conditions for OK2
if self.primary_backend == KlantenServiceType.OPENKLANT2.value:
return user.is_authenticated
return False
6 changes: 3 additions & 3 deletions src/open_inwoner/openklant/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,10 @@ def _get_kcm_subject(
e-suite code or
3. return the the e-suite subject code if no mapping exists
"""
e_suite_subject_code = getattr(kcm.contactmoment, "onderwerp", "")
esuite_subject_code = getattr(kcm.contactmoment, "onderwerp", "")

try:
subject = ContactFormSubject.objects.get(subject_code=e_suite_subject_code)
subject = ContactFormSubject.objects.get(esuite_subject_code=esuite_subject_code)
except ContactFormSubject.MultipleObjectsReturned as exc:
logger.warning(
"Multiple OIP subjects mapped to the same e-suite subject code for ",
Expand All @@ -593,7 +593,7 @@ def _get_kcm_subject(
kcm.contactmoment.url,
exc_info=exc,
)
return e_suite_subject_code
return esuite_subject_code

return subject.subject

Expand Down
Loading

0 comments on commit 2c036ae

Please sign in to comment.