Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 11/1/2024 #616

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions configuration/management/commands/add_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ class Command(BaseCommand):
},
"sSI": {"_label": "incomeOptions.sSI", "_default_message": "Supplemental Security Income (SSI)"},
"childSupport": {"_label": "incomeOptions.childSupport", "_default_message": "Child Support (Received)"},
"pension": {"_label": "incomeOptions.pension", "_default_message": "Military, Government, or Private Pension"},
"pension": {
"_label": "incomeOptions.pension",
"_default_message": "Military, Government, or Private Pension (including PERA)",
},
"veteran": {"_label": "incomeOptions.veteran", "_default_message": "Veteran's Pension or Benefits"},
"sSSurvivor": {
"_label": "incomeOptions.sSSurvivor",
Expand Down Expand Up @@ -2747,10 +2750,7 @@ class Command(BaseCommand):
}

privacy_policy = {
"en-us": "https://co.myfriendben.org/en/data-privacy-policy",
"es": "https://co.myfriendben.org/es/data-privacy-policy",
"fr": "https://co.myfriendben.org/fr/data-privacy-policy",
"vi": "https://co.myfriendben.org/vi/data-privacy-policy",
"en-us": "https://co.myfriendben.org/privacy-policy/",
}

referrer_data = {
Expand Down Expand Up @@ -2828,7 +2828,7 @@ class Command(BaseCommand):
"state": "CO",
"zip_code": 80202,
"email": "[email protected]",
"privacy_policy_link": "https://co.myfriendben.org/en/data-privacy-policy",
"privacy_policy_link": "https://co.myfriendben.org/privacy-policy/",
}

@transaction.atomic
Expand Down
11 changes: 11 additions & 0 deletions integrations/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib import admin
from unfold.admin import ModelAdmin
from integrations.models import Link


class LinkAdmin(ModelAdmin):
search_fields = ("link",)
list_display = ["validated", "valid_status_code", "status_code", "in_use", "link"]


admin.site.register(Link, LinkAdmin)
139 changes: 139 additions & 0 deletions integrations/management/commands/health_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
from django.core.management.base import BaseCommand
from django.conf import settings
from decouple import config
from hubspot import HubSpot
from hubspot.crm.contacts.exceptions import ForbiddenException
from django.db.models import Q
from authentication.models import User
from integrations.models import Link
from programs.models import Navigator, Program, TranslationOverride, UrgentNeed
from translations.models import BLANK_TRANSLATION_PLACEHOLDER, Translation
from configuration.models import Configuration
import argparse
import json


class Command(BaseCommand):
help = "Check that we can't read from HubSpot, and there is no PII in the database"

HUB_SPOT_TEXT = "Can't read Hub Spot"
PII_IN_DB_TEXT = "PII not in the database"

def add_arguments(self, parser):
parser.add_argument(
"-s",
"--strict",
action=argparse.BooleanOptionalAction,
help="Compare the website hashes to the stored hashes",
)

def handle(self, *args, **options):
self.stdout.write("")

self._check_links(options["strict"])

self.stdout.write("")

self._output_condition(self._cant_read_hubspot(), self.HUB_SPOT_TEXT)
self._output_condition(self._no_pii_in_db(), self.PII_IN_DB_TEXT)

def _cant_read_hubspot(self) -> bool:
client = HubSpot(access_token=config("HUBSPOT"))

try:
client.crm.contacts.basic_api.get_page(limit=1, archived=False)
return False
except ForbiddenException as e:
return True
except Exception as e:
self.stdout.write(self.style.ERROR(f"Exception when calling basic_api->get_page: {e}"))
return False

def _no_pii_in_db(self) -> bool:
users = User.objects.filter(is_staff=False).filter(
Q(first_name__isnull=False)
| Q(last_name__isnull=False)
| Q(cell__isnull=False)
| Q(email__isnull=False)
| Q(external_id__isnull=True)
)

if len(users) > 0:
return False
return True

def _check_links(self, strict: bool = False) -> bool:
links = self._get_links()

Link.objects.all().update(in_use=False)

for link in links:
if link == BLANK_TRANSLATION_PLACEHOLDER or link == "":
continue

try:
link_model: Link = Link.objects.get(link=link)
link_model.in_use = True
link_model.save()
except Link.DoesNotExist:
link_model: Link = Link.objects.create(link=link, validated=False, in_use=True)

link_model.validate()

if strict:
valid = link_model.validated
else:
valid = Link.good_status_code(link_model.status_code)

self._output_condition(valid, f"{link} {link_model.status_code}")

def _get_links(self) -> list[str]:
program_links = [p.apply_button_link for p in Program.objects.filter(active=True)]
urgent_need_links = [u.link for u in UrgentNeed.objects.filter(active=True)]
navigator_links = [n.assistance_link for n in Navigator.objects.filter(programs__isnull=False)]
translation_override_links = [
o.translation
for o in TranslationOverride.objects.filter(field__in=["apply_button_link", "learn_more_link"])
]
config_links = [
json.loads(Configuration.objects.get(name="public_charge_rule").data)["link"],
*[
o["link"]
for o in json.loads(Configuration.objects.get(name="more_help_options").data)["moreHelpOptions"]
if "link" in o
],
*json.loads(Configuration.objects.get(name="privacy_policy").data).values(),
*json.loads(Configuration.objects.get(name="consent_to_contact").data).values(),
]

links = {
*self._get_translation_links(program_links),
*self._get_translation_links(urgent_need_links),
*self._get_translation_links(navigator_links),
*self._get_translation_links(translation_override_links),
*config_links,
}

links = list(links)
links.sort()

return links

def _get_translation_links(self, translations: list[Translation]) -> list[str]:
links = set()
for translation in translations:
for lang_setting in settings.LANGUAGES:
lang = lang_setting[0]
translation.set_current_language(lang)

links.add(translation.text)

return list(links)

def _output_condition(self, validated: bool, message: str):
text = f"{message}: {'VALIDATED' if validated else 'FAILED'}"

if validated:
self.stdout.write(self.style.SUCCESS(text))
else:
self.stdout.write(self.style.ERROR(text))
40 changes: 40 additions & 0 deletions integrations/migrations/0001_squashed_0004_alter_link_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.2.15 on 2024-10-28 22:27

from django.db import migrations, models


class Migration(migrations.Migration):

replaces = [
("integrations", "0001_initial"),
("integrations", "0002_alter_link_hash"),
("integrations", "0003_alter_link_hash"),
("integrations", "0004_alter_link_link"),
]

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Link",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("link", models.URLField(max_length=2048, unique=True)),
("validated", models.BooleanField(default=False)),
(
"hash",
models.CharField(blank=True, default=None, max_length=2048, null=True),
),
],
),
]
23 changes: 23 additions & 0 deletions integrations/migrations/0005_link_status_code_alter_link_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.15 on 2024-10-29 15:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("integrations", "0001_squashed_0004_alter_link_link"),
]

operations = [
migrations.AddField(
model_name="link",
name="status_code",
field=models.IntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name="link",
name="hash",
field=models.CharField(blank=True, max_length=2048, null=True),
),
]
18 changes: 18 additions & 0 deletions integrations/migrations/0006_link_valid_status_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.15 on 2024-10-29 16:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("integrations", "0005_link_status_code_alter_link_hash"),
]

operations = [
migrations.AddField(
model_name="link",
name="valid_status_code",
field=models.BooleanField(default=False),
),
]
18 changes: 18 additions & 0 deletions integrations/migrations/0007_link_in_use.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.15 on 2024-10-29 16:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("integrations", "0006_link_valid_status_code"),
]

operations = [
migrations.AddField(
model_name="link",
name="in_use",
field=models.BooleanField(default=False),
),
]
91 changes: 91 additions & 0 deletions integrations/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from django.db import models
import hashlib
import requests
import random


class LinkManager(models.Manager):
def create(self, *args, **kwargs):
instance = super().create(*args, **kwargs)
instance.fill_hash()
instance.save()

return instance


class Link(models.Model):
link = models.URLField(max_length=2_048, unique=True)
in_use = models.BooleanField(default=False)
validated = models.BooleanField(default=False)
status_code = models.IntegerField(blank=True, null=True)
valid_status_code = models.BooleanField(default=False)
hash = models.CharField(max_length=2_048, blank=True, null=True)

objects = LinkManager()

def __str__(self):
return self.link

@staticmethod
def hash_data(data: str) -> str:
return hashlib.sha224(data.encode()).hexdigest()

@staticmethod
def good_status_code(code: int) -> bool:
return 200 >= code < 300

def _get_request(self) -> requests.Response:
user_agents = [
"Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11",
"Opera/9.25 (Windows NT 5.1; U; en)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0.1) Gecko/20100101 Firefox/8.0.1",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19",
]
rand_agent_index = random.randint(0, len(user_agents) - 1)
header = {"User-agent": user_agents[rand_agent_index]}
req = requests.get(self.link, headers=header)

return req

def _get_request_parts(self) -> tuple[int, str]:
try:
req = self._get_request()
except requests.RequestException:
return 400, "error"

return req.status_code, req.text

def validate(self):
status_code, body = self._get_request_parts()

self.status_code = status_code
self.valid_status_code = self.good_status_code(status_code)
self.save()

if not self.valid_status_code:
self.validated = False
self.save()
return

new_hash = self.hash_data(body)

if self.hash != new_hash:
self.validated = False
self.save()

def fill_hash(self):
if self.hash is None:
status_code, body = self._get_request_parts()
self.status_code = status_code
self.valid_status_code = self.good_status_code(status_code)
self.hash = self.hash_data(body)
self.validated = False

def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
self.fill_hash()

return super().save(force_insert, force_update, using, update_fields)
Loading