Skip to content

Commit

Permalink
adding new what_were_the_goods field to model and cleaning imports.
Browse files Browse the repository at this point in the history
Cleaned up notifier code.
  • Loading branch information
chris-pettinga committed Feb 19, 2024
1 parent 6261471 commit b2c1ba9
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 49 deletions.
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@
# GOV NOTIFY
GOV_NOTIFY_API_KEY = env.str("GOV_NOTIFY_API_KEY")
EMAIL_VERIFY_CODE_TEMPLATE_ID = env.str("GOVUK_NOTIFY_TEMPLATE_EMAIL_VERIFICATION")
RESTRICT_SENDING = env.bool(
"RESTRICT_SENDING", default=True
) # if True, only send to whitelisted domains

# SENTRY
SENTRY_DSN = env.str("SENTRY_DSN", default="")
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ max-line-length = 100
exclude = """
/(
*/migrations/*
*/question_content.py
)/
"""

Expand Down
6 changes: 6 additions & 0 deletions report_a_breach/core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class Meta:
widget = forms.TextInput(attrs={"id": "reporter_full_name"})


class WhatWereTheGoodsForm(BaseModelForm):
class Meta:
model = Breach
fields = ["what_were_the_goods"]


class SummaryForm(BaseForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
12 changes: 8 additions & 4 deletions report_a_breach/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from django_countries.fields import CountryField

from report_a_breach.base_classes.models import BaseModel
from report_a_breach.question_content import ADDITIONAL_INFORMATION
from report_a_breach.question_content import EMAIL
from report_a_breach.question_content import FULL_NAME
from report_a_breach.question_content import RELATIONSHIP
from report_a_breach.question_content import (
ADDITIONAL_INFORMATION,
EMAIL,
FULL_NAME,
RELATIONSHIP,
WHAT_WERE_THE_GOODS,
)


class Breach(BaseModel):
Expand All @@ -26,6 +29,7 @@ class Breach(BaseModel):
"SanctionsRegime", through="SanctionsRegimeBreachThrough"
)
additional_information = models.TextField(verbose_name=ADDITIONAL_INFORMATION["text"])
what_were_the_goods = models.TextField(verbose_name=WHAT_WERE_THE_GOODS["text"])


class PersonOrCompany(BaseModel):
Expand Down
4 changes: 2 additions & 2 deletions report_a_breach/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from report_a_breach.base_classes.views import BaseWizardView
from report_a_breach.question_content import RELATIONSHIP
from report_a_breach.utils.notifier import send_mail
from report_a_breach.utils.notifier import send_email

from .forms import EmailForm, EmailVerifyForm, NameForm, StartForm, SummaryForm
from .models import Breach, SanctionsRegime, SanctionsRegimeBreachThrough
Expand Down Expand Up @@ -38,7 +38,7 @@ def process_email_step(self, form):
verify_code = get_random_string(6, allowed_chars="0123456789")
self.request.session["verify_code"] = verify_code
print(verify_code)
send_mail(
send_email(
email=reporter_email_address,
context={"verification_code": verify_code},
template_id=settings.EMAIL_VERIFY_CODE_TEMPLATE_ID,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.10 on 2024-02-19 15:17

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("report_a_breach", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="breach",
name="what_were_the_goods",
field=models.TextField(
default="",
verbose_name="What were the goods or services, or what was the technological assistance or technology?",
),
preserve_default=False,
),
]
4 changes: 4 additions & 0 deletions report_a_breach/question_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@
}

ADDITIONAL_INFORMATION = {"text": "Tell us about the suspected breach", "helper": ""}
WHAT_WERE_THE_GOODS = {
"text": "What were the goods or services, or what was the technological assistance or technology?",
"helper": "",
}
59 changes: 16 additions & 43 deletions report_a_breach/utils/notifier.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import re

from django.conf import settings
from notifications_python_client.notifications import NotificationsAPIClient


def get_client():
"""
Return a Notification client
"""
return NotificationsAPIClient(settings.GOV_NOTIFY_API_KEY)


def send_mail(email, context, template_id, reference=None):
def send_email(email, context, template_id, reference=None) -> dict | bool:
"""Send an email using the GOV.UK Notify API."""
if is_whitelisted(email):
client = get_client()
client = NotificationsAPIClient(settings.GOV_NOTIFY_API_KEY)
send_report = client.send_email_notification(
email_address=email,
template_id=template_id,
personalisation=get_context(context),
reference=reference,
)
return send_report
else:
send_report = {
"content": {},
"whitelist": False,
}
send_report["to_email"] = email
send_report["template_id"] = template_id
return send_report
return False


def get_context(extra_context=None):
Expand All @@ -40,33 +27,19 @@ def get_context(extra_context=None):
return context


def get_template(template_id):
client = get_client()
return client.get_template(template_id)


def get_preview(template_id, values):
client = get_client()
return client.post_template_preview(
template_id=template_id, personalisation=get_context(values)
)


def is_whitelisted(email):
"""
Temporary measure to restrict notify emails to certain domains.
disabled on production.
"""
# if (
# os.environ.get("DJANGO_SETTINGS_MODULE", "").endswith("prod")
# or settings.DISABLE_NOTIFY_WHITELIST
# ):
# return True

whitelist = {"gov.uk", "businessandtrade.gov.uk", "trade.gov.uk", "digital.trade.gov.uk"}
regex_whitelist = []
_, domain = email.split("@")
in_whitelist = domain in whitelist or email in whitelist
if not in_whitelist:
in_whitelist = any([re.match(pattern, email) for pattern in regex_whitelist])
return in_whitelist
if settings.RESTRICT_SENDING:
_, domain = email.split("@")
email_domain_whitelist = (
"gov.uk",
"businessandtrade.gov.uk",
"trade.gov.uk",
"digital.trade.gov.uk",
)
return domain in email_domain_whitelist
else:
return True

0 comments on commit b2c1ba9

Please sign in to comment.