Skip to content

Commit 6972624

Browse files
committed
fixup! New evidence_links_cleaned property on PlanQuestionScore
1 parent 9c9fe2c commit 6972624

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

caps/utils.py

+19
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,22 @@ def gen_natsort_lamda(keyfunc=None):
9797
return lambda q: [
9898
int(t) if t.isdigit() else t.lower() for t in re.split("(\d+)", keyfunc(q))
9999
]
100+
101+
102+
def clean_links(links: str):
103+
# Evidence "links" can be one of:
104+
# - an empty line
105+
# - a string like "Airports:"
106+
# - a string like "www.whatever.com/…"
107+
# - a string like "http://www…"
108+
# So attempt to tidy them up, before passing to
109+
# a template filter like domain_human or urlizetrunc.
110+
link_list = []
111+
for line in links.splitlines():
112+
if line == "":
113+
pass
114+
elif line.startswith("www."):
115+
link_list.append(f"http://{line}")
116+
else:
117+
link_list.append(line)
118+
return link_list

scoring/models.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.db.models import Avg, Count, F, Max, Q
66

77
from caps.models import Council
8+
from caps.utils import clean_links
89

910

1011
# define this here as mixins imports PlanScore so if we import that then we get
@@ -531,6 +532,13 @@ def pretty_code(self):
531532
def is_council_operations_only(self):
532533
return self.code in self.COUNCIL_OPS_QUESTION_CODES
533534

535+
# the questions answered methods of PlanScore return PlanQuestion objects that
536+
# are used like PlanQuestionScore objects in some cases so we need this in both
537+
# classes
538+
@property
539+
def evidence_links_cleaned(self):
540+
return clean_links(self.evidence_links)
541+
534542
@classmethod
535543
def get_average_scores(cls, section=None, council_group=None):
536544
qs = PlanQuestionScore.objects.all()
@@ -582,22 +590,7 @@ class PlanQuestionScore(ScoreFilterMixin, models.Model):
582590

583591
@property
584592
def evidence_links_cleaned(self):
585-
# Evidence "links" can be one of:
586-
# - an empty line
587-
# - a string like "Airports:"
588-
# - a string like "www.whatever.com/…"
589-
# - a string like "http://www…"
590-
# So attempt to tidy them up, before passing to
591-
# a template filter like domain_human or urlizetrunc.
592-
links = []
593-
for line in self.evidence_links.splitlines():
594-
if line == "":
595-
pass
596-
elif line.startswith("www."):
597-
links.append(f"http://{line}")
598-
else:
599-
links.append(line)
600-
return links
593+
return clean_links(self.evidence_links)
601594

602595
@classmethod
603596
def all_question_max_score_counts(

scoring/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def make_question_obj(self, question):
249249
"how_marked_display": question.get_how_marked_display(),
250250
"is_council_operations_only": question.is_council_operations_only,
251251
"weighting": question.get_weighting_display(),
252-
"evidence_links": question.evidence_links,
252+
"evidence_links": question.evidence_links_cleaned,
253253
}
254254
if question.question_type == "HEADER":
255255
q["max"] = question.header_max

0 commit comments

Comments
 (0)