From 19eb627975e46ea727b8484c26c563c09649007f Mon Sep 17 00:00:00 2001 From: Ashley Felton Date: Mon, 7 Oct 2024 11:37:32 +0800 Subject: [PATCH 1/7] Alter query_caddy and wfs_getfeature functions to catch exceptions on bad JSON response from external services. --- prs2/referral/utils.py | 754 +++++++++++++++++++++-------------------- 1 file changed, 378 insertions(+), 376 deletions(-) diff --git a/prs2/referral/utils.py b/prs2/referral/utils.py index 4cfadc6..fd7c5d7 100644 --- a/prs2/referral/utils.py +++ b/prs2/referral/utils.py @@ -1,376 +1,378 @@ -import json -import re -from datetime import date, datetime - -import requests -from dbca_utils.utils import env -from django.apps import apps -from django.conf import settings -from django.contrib import admin -from django.core.mail import EmailMultiAlternatives -from django.db.models import Q -from django.db.models.base import ModelBase -from django.utils.encoding import smart_str -from django.utils.safestring import mark_safe -from reversion.models import Version -from unidecode import unidecode - - -def is_model_or_string(model): - """This function checks if we passed in a Model, or the name of a model as - a case-insensitive string. The string may also be plural to some extent - (i.e. ending with "s"). If we passed in a string, return the named Model - instead using get_model(). - - Example:: - - from referral.util import is_model_or_string - is_model_or_string('region') - is_model_or_string(Region) - - >>> from referral.models import Region - >>> from django.db.models.base import ModelBase - >>> from referral.util import is_model_or_string - >>> isinstance(is_model_or_string('region'), ModelBase) - True - >>> isinstance(is_model_or_string(Region), ModelBase) - True - """ - if not isinstance(model, ModelBase): - # Hack: if the last character is "s", remove it before calling get_model - x = len(model) - 1 - if model[x] == "s": - model = model[0:x] - try: - model = apps.get_model("referral", model) - except LookupError: - model = None - return model - - -def smart_truncate(content, length=100, suffix="....(more)"): - """Small function to truncate a string in a sensible way, sourced from: - http://stackoverflow.com/questions/250357/smart-truncate-in-python - """ - content = smart_str(content) - if len(content) <= length: - return content - else: - return " ".join(content[: length + 1].split(" ")[0:-1]) + suffix - - -def dewordify_text(txt): - """Function to strip some of the crufty HTML that results from copy-pasting - MS Word documents/HTML emails into the RTF text fields in this application. - Should always return a unicode string. - - Source: - http://stackoverflow.com/questions/1175540/iterative-find-replace-from-a-list-of-tuples-in-python - """ - REPLACEMENTS = { - " ": " ", - "<": "<", - ">": ">", - ' class="MsoNormal"': "", - '': "", - "": "", - "": "", - } - - def replacer(m): - return REPLACEMENTS[m.group(0)] - - if txt: - # Whatever string encoding is passed in, - # use unidecode to replace non-ASCII characters. - txt = unidecode(txt) # Replaces odd characters. - r = re.compile("|".join(REPLACEMENTS.keys())) - r = r.sub(replacer, txt) - return r - else: - return "" - - -def breadcrumbs_li(links): - """Returns HTML: an unordered list of URLs (no surrounding