Skip to content

Commit

Permalink
allow copy overrides (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Cameron Lamb <[email protected]>
  • Loading branch information
marcelkornblum and CamLamb authored Apr 11, 2023
1 parent d909282 commit b91c297
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 124 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ DJANGO_FEEDBACK_GOVUK = {
"SERVICE_NAME": "<your-service>",
"FEEDBACK_NOTIFICATION_EMAIL_TEMPLATE_ID": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"FEEDBACK_NOTIFICATION_EMAIL_RECIPIENTS": ["[email protected]"],
"COPY": {
#...add any copy tags to override here
}
}

The copy dict contains string IDs for all user-facing copy, defaulting to the following (override
just the fields you want to, using the `{{ service_name }}` variable if necessary for _title and _body strings):

```py
{
"SUBMIT_TITLE": "Give feedback on {{ service_name }}",
"CONFIRM_TITLE": "Feedback submitted",
"CONFIRM_BODY": "Thank you for submitting your feedback.",
"FIELD_SATISFACTION_LEGEND": "Overall, how did you feel about the service you received today?",
"FIELD_COMMENT_LEGEND": "How could we improve this service?",
"FIELD_COMMENT_HINT": "Do not include any personal or financial information, for example your National Insurance or credit card numbers.",
}
```

Expand Down
14 changes: 9 additions & 5 deletions django_feedback_govuk/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from crispy_forms_gds.helper import FormHelper
from crispy_forms_gds.layout import HTML, Field, Fieldset, Layout, Size, Submit
from django.conf import settings
from django.forms import HiddenInput, ModelForm, RadioSelect

from .models import Feedback, SatisfactionOptions
from .settings import dfg_settings


class FeedbackForm(ModelForm):
Expand Down Expand Up @@ -30,17 +32,19 @@ def __init__(self, *args, **kwargs):
"satisfaction",
template="django_feedback_govuk/widgets/star_rating/star_rating.html",
),
legend="Overall, how did you feel about the service you received today?",
legend=dfg_settings.COPY_FIELD_SATISFACTION_LEGEND,
legend_size=Size.MEDIUM,
),
Fieldset(
HTML(
"<p class='govuk-hint'>Do not include any personal or "
"financial information, for example your National "
"Insurance or credit card numbers.</p>"
(
"<p class='govuk-hint'>",
dfg_settings.COPY_FIELD_COMMENT_HINT,
"</p>",
)
),
Field("comment"),
legend="How could we improve this service?",
legend=dfg_settings.COPY_FIELD_COMMENT_LEGEND,
legend_size=Size.MEDIUM,
),
Submit("submit", "Send feedback"),
Expand Down
6 changes: 4 additions & 2 deletions django_feedback_govuk/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
from django.conf import settings
from notifications_python_client.notifications import NotificationsAPIClient

from .settings import dfg_settings


def email(personalisation: Dict):
notification_client = NotificationsAPIClient(
settings.GOVUK_NOTIFY_API_KEY,
)
email_addresses = settings.DJANGO_FEEDBACK_GOVUK['FEEDBACK_NOTIFICATION_EMAIL_RECIPIENTS']
email_addresses = dfg_settings.FEEDBACK_NOTIFICATION_EMAIL_RECIPIENTS

for email_address in email_addresses:
message_response = notification_client.send_email_notification(
email_address=email_address,
template_id=settings.DJANGO_FEEDBACK_GOVUK['FEEDBACK_NOTIFICATION_EMAIL_TEMPLATE_ID'],
template_id=dfg_settings.FEEDBACK_NOTIFICATION_EMAIL_TEMPLATE_ID,
personalisation=personalisation,
)

Expand Down
58 changes: 58 additions & 0 deletions django_feedback_govuk/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from typing import List

from django.conf import settings


DEFAULTS = {
"SERVICE_NAME": "Example service",
"FEEDBACK_NOTIFICATION_EMAIL_TEMPLATE_ID": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"FEEDBACK_NOTIFICATION_EMAIL_RECIPIENTS": [
"[email protected]",
],
"COPY": {
"SUBMIT_TITLE": "Give feedback on {{ service_name }}",
"CONFIRM_TITLE": "Feedback submitted",
"CONFIRM_BODY": "Thank you for submitting your feedback.",
"FIELD_SATISFACTION_LEGEND": "Overall, how did you feel about the service you received today?",
"FIELD_COMMENT_LEGEND": "How could we improve this service?",
"FIELD_COMMENT_HINT": "Do not include any personal or financial information, for example your National Insurance or credit card numbers.",
},
}


class DjangoFeedbackGovUKSettings:
SERVICE_NAME: str
FEEDBACK_NOTIFICATION_EMAIL_TEMPLATE_ID: str
FEEDBACK_NOTIFICATION_EMAIL_RECIPIENTS: List[str]
COPY_SUBMIT_TITLE: str
COPY_CONFIRM_TITLE: str
COPY_CONFIRM_BODY: str
COPY_FIELD_SATISFACTION_LEGEND: str
COPY_FIELD_COMMENT_LEGEND: str
COPY_FIELD_COMMENT_HINT: str

def __getattr__(self, attr):
django_settings = getattr(settings, "DJANGO_FEEDBACK_GOVUK", {})

# Get COPY values
if attr.startswith("COPY_"):
copy_key = attr[5:]
value = django_settings.get("COPY", {}).get(copy_key)
if value:
# Return the value from user settings
return value
# Return the value from defaults
return DEFAULTS["COPY"][copy_key]

if attr in django_settings:
# Return the value from user settings
return django_settings[attr]

default_value = DEFAULTS.get(attr, None)
if default_value is None and attr not in DEFAULTS:
raise AttributeError(f"No value set for DJANGO_FEEDBACK_GOVUK['{attr}']")
# Return the value from defaults
return default_value


dfg_settings = DjangoFeedbackGovUKSettings()
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div class="govuk-panel govuk-panel--confirmation">
<h1 class="govuk-panel__title">Feedback submitted</h1>
<div class="govuk-panel__body">Thank you for submitting your feedback.</div>
</div>
<div class="govuk-panel govuk-panel--confirmation">
<h1 class="govuk-panel__title">{{ confirm_title }}</h1>
<div class="govuk-panel__body">{{ confirm_body }}</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
{% error_summary form %}
<h1 class="govuk-heading-l">Give feedback on {{ service_name }}</h1>
<h1 class="govuk-heading-l">{{ submit_title }}</h1>
<form novalidate method="post" action="{% url 'feedback-submit' %}">
{% crispy form %}
</form>
Expand Down
34 changes: 25 additions & 9 deletions django_feedback_govuk/templatetags/feedback_tags.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
from django import template
from django.conf import settings
from django.template import Context, Template

from django_feedback_govuk.forms import FeedbackForm
from django_feedback_govuk.settings import dfg_settings


register = template.Library()


@register.inclusion_tag("django_feedback_govuk/partials/submit.html", takes_context=True)
@register.inclusion_tag(
"django_feedback_govuk/partials/submit.html", takes_context=True
)
def feedback_submit(context):
if 'form' in context:
form = context['form']
if "form" in context:
form = context["form"]
else:
initial = {}
initial["submitter"] = context.request.user
form = FeedbackForm(initial=initial)

return {
new_context = {
"form": form,
"service_name": settings.DJANGO_FEEDBACK_GOVUK["SERVICE_NAME"],
"service_name": dfg_settings.SERVICE_NAME,
"submit_title": dfg_settings.COPY_SUBMIT_TITLE,
}
return new_context


@register.inclusion_tag("django_feedback_govuk/partials/confirm.html")
def feedback_confirm():
return {}
@register.inclusion_tag(
"django_feedback_govuk/partials/confirm.html", takes_context=True
)
def feedback_confirm(context):
new_context = {
"service_name": dfg_settings.SERVICE_NAME,
"confirm_title": dfg_settings.COPY_CONFIRM_TITLE,
"confirm_body": dfg_settings.COPY_CONFIRM_BODY,
}
return new_context


@register.inclusion_tag("django_feedback_govuk/partials/listing.html", takes_context=True)
@register.inclusion_tag(
"django_feedback_govuk/partials/listing.html", takes_context=True
)
def feedback_listing(context):
return context

Expand Down
5 changes: 4 additions & 1 deletion example_project/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@

# Django Feedback GovUK
DJANGO_FEEDBACK_GOVUK = {
"SERVICE_NAME": "Example Service",
"SERVICE_NAME": "Example Project",
"FEEDBACK_NOTIFICATION_EMAIL_TEMPLATE_ID": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"FEEDBACK_NOTIFICATION_EMAIL_RECIPIENTS": ["[email protected]"],
"COPY": {
"submit_title": "Tell us what you thought about {{ service_name }}"
}
}
Loading

0 comments on commit b91c297

Please sign in to comment.